zoukankan      html  css  js  c++  java
  • c# 中對輸入的值用md5或Hash加密

            using System.Security.Cryptography;


            
    /// <summary>
            
    /// MD5加密方法
            
    /// </summary>
            
    /// <param name="asSource">源Md5值</param>
            
    /// <param name="asDestination">需對比的字符</param>
            
    /// <returns>是否正確</returns>

            private bool Md5EncryptJudge(string asSource, string asDestination)
            
    {
                
    bool bResult = false;
                
    string sDestination ="";
                
    //定義Md5密碼服務類
                MD5CryptoServiceProvider mdcpValu = new MD5CryptoServiceProvider();
                
    //將傳入的值轉換成UTF8格式。便於加密時的格式統一
                byte[] bDestination = System.Text.Encoding.UTF8.GetBytes(asDestination);
                
    //加密
                byte[] bDestinationMd5 = mdcpValu.ComputeHash(bDestination);
                
    //將加密后的值賦給字符串
                foreach (byte bVal in bDestinationMd5)
                
    {
                    sDestination 
    += bVal.ToString();
                }

                
    //判斷需對比的值加密成md5后與傳入的MD5值是否與傳入的相等
                if (asSource == sDestination)
                
    {
                    bResult 
    = true;
                }

                
    else
                
    {
                    bResult 
    = false;
                }

                
    return bResult;
            }




            /// <summary>
            
    /// Hash加密
            
    /// </summary>
            
    /// <param name="asSource">源加密后的值</param>
            
    /// <param name="asDestination">目標字符串</param>

            private void HashEncrypt(string sScouce, string asDestination)
            
    {
                
    byte[] bDestinationValue = System.Text.Encoding.UTF8.GetBytes(asDestination);
                HMACSHA1 hsVal 
    = new HMACSHA1();
                
    //加密
                byte[] bHmacshaValue = hsVal.ComputeHash(bDestinationValue);
                
    //將加密后的值轉換為字符
                string sDesHmaVal = Convert.ToBase64String(bHmacshaValue);
                
    if (sScouce == sDesHmaVal)
                
    {
                    MessageBox.Show(
    "Ok");
                }

                
    else
                
    {
                    MessageBox.Show(
    "False");
                }

            }

  • 相关阅读:
    武道之路-炼体期五重天中期
    武道之路-炼体期五重天
    武道之路-炼体期四重天巅峰
    修改Oracle监听端口
    完美解决IE(IE6/IE7/IE8)不兼容HTML5标签的方法
    Create Linked Server SQL Server 2008
    Oracle:ODP.NET Managed 小试牛刀
    jquery ajax跨域请求webservice webconfig配置
    oracle 11g ORA-12541: TNS: 无监听程序 (DBD ERROR: OCIServerAttach)
    oracle 11g 一直提示 严重: 监听程序未启动或数据库服务未注册到该监听程序
  • 原文地址:https://www.cnblogs.com/scottckt/p/1029553.html
Copyright © 2011-2022 走看看