1、在作項目的時候,常常會遇到上傳的文件和保存的數據須要加密,咱們最常常用到的就是MD5加密。數組
2、MD5加密代碼展現ide
1 public class EncryptMD5 2 { 3 /// <summary> 4 /// 加密 5 /// </summary> 6 /// <returns></returns> 7 public string Encrypt(string content) 8 { 9 //初始化MD5對象 10 MD5CryptoServiceProvider serviceProvider = new MD5CryptoServiceProvider(); 11 //把要加密的內容裝換成字節數組 12 byte[] inputBytes = Encoding.UTF8.GetBytes(content); 13 //轉換指定字節的哈希值 14 byte[] outPutBytes = serviceProvider.ComputeHash(inputBytes); 15 //轉換成64位的字符串 16 var resultStr = Convert.ToBase64String(outPutBytes); 17 return resultStr; 18 } 19 /// <summary> 20 /// 封裝實例化對象 21 /// </summary> 22 /// <returns></returns> 23 public static EncryptMD5 GetEncryptMD5() 24 { 25 return new EncryptMD5(); 26 } 27 }
3、調用和顯示結果加密
static void Main(string[] args) { var result = EncryptMD5.GetEncryptMD5().Encrypt("123456"); Console.WriteLine(result); }