C #: use MD5 to encrypt and decrypt user passwords,
C # usually involves encryption and decryption algorithms for user passwords. using MD5 encryption is the most common implementation method. This article summarizes common algorithms and shares them with you based on your little experience.
1. Use the 16-bit, 32-bit, 64-bit MD5 method to encrypt the user name
1) 16-bit MD5 Encryption
/// <Summary> /// 16-bit MD5 encryption /// </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.Replace ("-", ""); return t2 ;}
2) 32-bit MD5 Encryption
/// <Summary> /// 32-bit MD5 encryption /// </summary> /// <param name = "password"> </param> /// <returns> </returns> public static string MD5Encrypt32 (string password) {string cl = password; string pwd = ""; MD5 md5 = MD5.Create (); // instantiate an md5 object. // The encrypted MD5 object is a byte array, here, you need to pay attention to Encoding UTF8/Unicode and so on. Select byte [] s = md5.ComputeHash (Encoding. UTF8.GetBytes (cl); // converts an array of the byte type to a string using a loop. This string is formatted by regular characters for (int I = 0; I <s. length; I ++ ){// Use the hexadecimal format of the obtained string. The characters in the format are lowercase letters. If uppercase letters (X) are used, the characters in the format are uppercase letters pwd = pwd + s [I]. toString ("X");} return pwd ;}
3) 64-bit MD5 Encryption
Public static string MD5Encrypt64 (string password) {string cl = password; // string pwd = ""; MD5 md5 = MD5.Create (); // instantiate an md5 object. // The encrypted md5 object is a byte array. Note that byte [] s = md5.ComputeHash (Encoding. UTF8.GetBytes (cl); return Convert. toBase64String (s );}
4) use MD5 to encrypt the User Password
/// <Summary> /// encrypt the User password /// </summary> /// <param name = "password"> password </param> /// <param name = "codeLength"> encrypted digits </param> // <returns> encrypted password </returns> public static string md5 (string password, int codeLength) {if (! String. IsNullOrEmpty (password) {// 16-bit MD5 encryption (get 32-bit encrypted 9 ~ 25 characters) if (codeLength = 16) {return System. web. security. formsAuthentication. hashPasswordForStoringInConfigFile (password, "MD5 "). toLower (). substring (8, 16);} // 32-bit encrypted if (codeLength = 32) {return System. web. security. formsAuthentication. hashPasswordForStoringInConfigFile (password, "MD5 "). toLower () ;}} return string. empty ;}
Since MD5 is irreversible, it cannot be decrypted after encryption. When getting the user name and password, you need to encrypt the data entered by one side and compare it with the encrypted data in the database. If the comparison results are consistent, you can determine that the login is successful! The Code is as follows:
/// <Summary> /// log on /// </summary> public Model. userInfo UserLogOn (string USERID, string pwd, out string statusCode) {// assume that the Model object Model of UserInfo has been obtained through the user ID. userInfo model = GetModel (USERID); if (model! = Null) {if (model. PASSWORD = MD5Encrypt64 (pwd) {statusCode = "Login successful";} else {statusCode = "PASSWORD error";} else {statusCode = "the user does not exist! "; Model = null;} return model ;}
5) use the DESCryptoServiceProvider object to encrypt and decrypt strings.
/// <Summary> // DES Data Encryption /// </summary> /// <param name = "targetValue"> target value </param> /// <param name = "key"> key </param> // <returns> encryption value </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); // uses two hash passwords to set the symmetric algorithm's initialization vector des. key = Encoding. ASCII. getBytes (FormsAuthentication. hashPasswordForStoringInConfigFile (FormsAuthentication. hashPasswordForStoringInConfigFile (key, "md5 "). substring (0, 8), "sha1 "). substring (0, 8); // uses two hash passwords to set the algorithm's secret key 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 ();}
This algorithm can be decrypted using an encryption key. The decryption method is as follows:
/// <Summary> // DES data decryption /// </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;} // defines the DES encryption object 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;} // uses two hash passwords to set the symmetric algorithm's initialization vector des. key = Encoding. ASCII. getBytes (FormsAuthentication. hashPasswordForStoringInConfigFile (FormsAuthentication. hashPasswordForStoringInConfigFile (key, "md5 "). substring (0, 8), "sha1 "). substring (0, 8); // uses two hash passwords to set the algorithm's secret key des. IV = Encoding. ASCII. getBytes (FormsAuthentication. hashPasswordForStoringInConfigFile (FormsAuthentication. hashPasswordForStoringInConfigFile (key, "md5 "). substring (0, 8), "md5 "). substring (0, 8); // defines the memory stream var MS = new MemoryStream (); // defines the encrypted stream var cs = new CryptoStream (MS, des. createDecryptor (), CryptoStreamMode. write); cs. write (inputByteArray, 0, inputByteArray. length); cs. flushFinalBlock (); return Encoding. default. getString (ms. toArray ());}
Note: This article is original in Healer007, with the name: radish! Some materials are from the Internet. If you need to reprint them, please indicate the source!