MD5加密 1創建Md5 2.開始加密,需要將字元轉換為位元組數組 3.返回一個加密好的位元組數組 4.將位元組數組中每個元素按照指定的編碼格式解析成字元串 ...
MD5加密
1創建Md5
2.開始加密,需要將字元轉換為位元組數組
3.返回一個加密好的位元組數組
4.將位元組數組中每個元素按照指定的編碼格式解析成字元串
1 static void Main(string[] args) 2 { 3 4 string s = GetMD5("123"); 5 6 Console.WriteLine(s); 7 Console.ReadKey(); 8 9 10 } 11 12 13 14 public static string GetMD5(string str) 15 { 16 //創建MD5對象 17 MD5 md5 = MD5.Create(); 18 //開始加密 19 //需要將字元處轉換成位元組數組 20 byte[] buffer = Encoding.GetEncoding("GBK").GetBytes(str); 21 //返回一個加密好的位元組數組 22 byte[] MD5Buffer = md5.ComputeHash(buffer); 23 24 //將位元組數組轉換成字元串 25 //位元組數組---字元串 26 //將位元組數組中每個元素按照指定的編碼格式解析成字元串 27 //直接將數組ToString(); 28 //將位元組數組中的每個元素ToString() 29 // return Encoding.GetEncoding("GBK").GetString(MD5Buffer); 30 31 // 189 273 345 我愛你 32 // 189 273 345 33 string strNew = ""; 34 for (int i = 0; i < MD5Buffer.Length; i++) 35 { 36 strNew += MD5Buffer[i].ToString("x2"); 37 } 38 return strNew; 39 }