基於C#對用戶暗碼應用MD5加密與解密。本站提示廣大學習愛好者:(基於C#對用戶暗碼應用MD5加密與解密)文章只能為提供參考,不一定能成為您想要的結果。以下是基於C#對用戶暗碼應用MD5加密與解密正文
C#中常觸及到對用戶暗碼的加密於解密的算法,個中應用MD5加密是最多見的的完成方法。本文總結了通用的算法並聯合了本身的一點小經歷,分享給年夜家。
一.應用16位、32位、64位MD5辦法對用戶名加密
1)16位的MD5加密
/// <summary> /// 16位MD5加密 /// </summary> /// <param name="password"></param> /// <returns></returns> public static string MD5Encrypt16(string password) { var md5 = new MD5CryptoServiceProvider(); string t2 = BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(password)), 4, 8); t2 = t2.WordStr("-", ""); return t2; }
2)32位的MD5加密
/// <summary> /// 32位MD5加密 /// </summary> /// <param name="password"></param> /// <returns></returns> public static string MD5Encrypt32(string password) { string cl = password; string pwd = ""; MD5 md5 = MD5.Create(); //實例化一個md5對像 // 加密後是一個字節類型的數組,這裡要留意編碼UTF8/Unicode等的選擇 byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl)); // 經由過程應用輪回,將字節類型的數組轉換為字符串,此字符串是慣例字符格局化所得 for (int i = 0; i < s.Length; i++) { // 將獲得的字符串應用十六進制類型格局。格局後的字符是小寫的字母,假如應用年夜寫(X)則格局後的字符是年夜寫字符 pwd = pwd + s[i].ToString("X"); } return pwd; }
3)64位的MD5加密
public static string MD5Encrypt64(string password) { string cl = password; //string pwd = ""; MD5 md5 = MD5.Create(); //實例化一個md5對像 // 加密後是一個字節類型的數組,這裡要留意編碼UTF8/Unicode等的選擇 byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl)); return Convert.ToBase64String(s); }
4)應用MD5為用戶暗碼加密
/// <summary> /// 加密用戶暗碼 /// </summary> /// <param name="password">暗碼</param> /// <param name="codeLength">加密位數</param> /// <returns>加密暗碼</returns> public static string md5(string password, int codeLength) { if (!string.IsNullOrEmpty(password)) { // 16位MD5加密(取32位加密的9~25字符) if (codeLength == 16) { return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5").ToLower().Substring(8, 16); } // 32位加密 if (codeLength == 32) { return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5").ToLower(); } } return string.Empty; }
因為MD5是弗成逆的,所以加密以後就沒法解密,取用戶名和暗碼時刻,須要再加密一邊用戶輸出的數據與數據庫中已加密的數據停止比對。假如比對成果分歧,則可以剖斷上岸勝利!代碼以下所示:
/// <summary> /// 上岸 /// </summary> public Model.UserInfo UserLogOn(string USERID, string pwd, out string statusCode) { //假定曾經經由過程用戶ID獲得到UserInfo的Model對象 Model.UserInfo model = GetModel(USERID); if (model != null) { if (model.PASSWORD == MD5Encrypt64(pwd)) { statusCode = "上岸勝利"; } else { statusCode = “暗碼毛病”; } } else { statusCode = "用戶不存在!"; model = null; } return model; }
5)經由過程DESCryptoServiceProvider對象對字符串停止加密解密
/// <summary> /// DES數據加密 /// </summary> /// <param name="targetValue">目的值</param> /// <param name="key">密鑰</param> /// <returns>加密值</returns> public static string Encrypt(string targetValue, string key) { if (string.IsNullOrEmpty(targetValue)) { return string.Empty; } var returnValue = new StringBuilder(); var des = new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.Default.GetBytes(targetValue); // 經由過程兩次哈希暗碼設置對稱算法的初始化向量 des.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile (FormsAuthentication.HashPasswordForStoringInConfigFile(key, "md5"). Substring(0, 8), "sha1").Substring(0, 8)); // 經由過程兩次哈希暗碼設置算法的秘密密鑰 des.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile (FormsAuthentication.HashPasswordForStoringInConfigFile(key, "md5") .Substring(0, 8), "md5").Substring(0, 8)); var ms = new MemoryStream(); var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); foreach (byte b in ms.ToArray()) { returnValue.AppendFormat("{0:X2}", b); } return returnValue.ToString(); }
此種算法可以經由過程加密密鑰停止解密,解密辦法以下:
/// <summary> /// DES數據解密 /// </summary> /// <param name="targetValue"></param> /// <param name="key"></param> /// <returns></returns> public static string Decrypt(string targetValue, string key) { if (string.IsNullOrEmpty(targetValue)) { return string.Empty; } // 界說DES加密對象 var des = new DESCryptoServiceProvider(); int len = targetValue.Length / 2; var inputByteArray = new byte[len]; int x, i; for (x = 0; x < len; x++) { i = Convert.ToInt32(targetValue.Substring(x * 2, 2), 16); inputByteArray[x] = (byte)i; } // 經由過程兩次哈希暗碼設置對稱算法的初始化向量 des.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile (FormsAuthentication.HashPasswordForStoringInConfigFile(key, "md5"). Substring(0, 8), "sha1").Substring(0, 8)); // 經由過程兩次哈希暗碼設置算法的秘密密鑰 des.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile (FormsAuthentication.HashPasswordForStoringInConfigFile(key, "md5") .Substring(0, 8), "md5").Substring(0, 8)); // 界說內存流 var ms = new MemoryStream(); // 界說加密流 var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Encoding.Default.GetString(ms.ToArray()); }
以上內容是基於C#對用戶暗碼應用MD5加密與解密的全體論述,願望年夜家愛好。