1. 程式人生 > 實用技巧 >MD5(加密)

MD5(加密)

使用MD5加密:

MD5(12位)

password為需要加密的

public  string MD512(string password)
        {
            var md5 = new MD5CryptoServiceProvider();
            string t2 = BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(password)), 4, 8);
            t2 = t2.Replace("-", "");
            return t2;
        }

MD5(32位)

public string MD532(string password)
        {
            MD5 md5 = MD5.Create();//例項化一個md5物件
            //加密後是一個位元組型別的陣列,這裡要注意編碼UTF8/Unicode等的選擇
            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(password));
            //通過迴圈,將位元組型別轉化為字串,此字串是常規字元格式化所得
            string str = "";
            
for (int i = 0; i < s.Length; i++) { //將得到的字串使用十六進位制型別格式。格式後的字元是小寫的字母, //如果使用大寫(X)則格式後的字元是大寫字元 str += s[i].ToString("x"); } return str; }

MD5(64位)

        public string MD564(string password)
        {
            MD5 md5 
= MD5.Create(); byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(password)); return Convert.ToBase64String(s); }