C#类实现MD5的加密——另一高级应用

来源:互联网 发布:淘宝主图尺寸怎么修改 编辑:程序博客网 时间:2024/06/16 20:26


#region Code Descriptions V20061219
//------------------------------------------------------------------------------
// ClassName:   JKLib.Security.MyMD5
// Function :   MD5 加密算法algorithm implemented in C#
//              Computes the hash value for the specified byte array.
//                 
// Edit Date:   2006-12-21
//
// Reedit by:  
// Edit Note:  
// Edit Date:  
//------------------------------------------------------------------------------
#endregion

using System;
using System.Security.Cryptography;

namespace JKLib.Security
{
    /// <summary>
    /// MD5 加密算法 algorithm implemented in C#
    /// Computes the hash value for the specified byte array.
    /// </summary>
    public class MyMD5
    {
        //返回任意字符串,长度32
        //本程序的数据库中Salt字段长度32
        private static string GetSalt()
        {
            Random rnd = new Random();
            Byte[] b = new Byte[32];
            rnd.NextBytes(b);
            return MD5ToHexString(b);
        }

        /// <summary>
        /// 计算密码
        /// </summary>
        /// <param name="strPassword">用户输入的密码,可能空</param>
        /// <param name="salt">salt值</param>
        /// <returns>返回MD5加密后的密码</returns>
        /// <remarks>
        /// 这里主要定义了从salt值以什么方式什么次序计算密码
        /// </remarks>
        public static string Encrypt(string strPassword, string salt)
        {
            if (strPassword == null) strPassword = "";
            if (salt == null) salt = "";

            return MD5ToHexString(strPassword + salt);
        }
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="strPassword"></param>
        /// <returns></returns>
        public static string Encrypt(string strPassword)
        {
            return Encrypt(strPassword, null);
        }
        /// <summary>
        /// MD5 加密,byte[]型
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] MD5_Byte(byte[] data)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            return md5.ComputeHash(data);
        }
        /// <summary>
        /// MD5 加密,byte[]型加密为string
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string MD5ToHexString(byte[] data)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] result = md5.ComputeHash(data);
            string t = "";
            string tTemp = "";
            for (int i = 0; i < result.Length; i++)
            {
                tTemp = Convert.ToString(result[i], 16);
                if (tTemp.Length != 2)
                {
                    switch (tTemp.Length)
                    {
                        case 0: tTemp = "00"; break;
                        case 1: tTemp = "0" + tTemp; break;
                        default: tTemp = tTemp.Substring(0, 2); break;
                    }
                }
                t += tTemp;
            }
            return t;
        }
        /// <summary>
        /// 加密实现
        /// </summary>
        /// <param name="strText"></param>
        /// <returns></returns>
        private static string MD5ToHexString(string strText)
        {
            byte[] data = System.Text.ASCIIEncoding.Unicode.GetBytes(strText);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] result = md5.ComputeHash(data);
            string t = "";
            for (int i = 0; i < result.Length; i++)
            {
                t += Convert.ToString(result[i], 16);
            }
            return t;
        }
    }

}
 

原创粉丝点击