C # often involves encrypting the user's password to decrypt the algorithm, where using MD5 encryption is the most common way to implement it. This paper summarizes the general algorithms and combines their own little experience to share to everyone.
I. Encrypt user names using the 16-bit, 32-bit, 64-bit MD5 method
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 (); C4/>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 array of byte types after MD5//encryption, note the choice of encoding utf8/unicode, etc. byte[] s = MD5. ComputeHash (Encoding.UTF8.GetBytes (CL)); By using loops, an array of byte types is converted to a string that is a regular character formatted for (int i = 0; i < s.length; i++) { //The resulting string is formatted with hexadecimal type. The formatted character is a lowercase letter, and if uppercase (X) is used, the character after the format is uppercase characters 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 array of byte types after MD5//encryption, note the choice of encoding utf8/unicode, etc. byte[] s = MD5. ComputeHash (Encoding.UTF8.GetBytes (CL)); return convert.tobase64string (s);}
4) Encrypt the user password using MD5
<summary>///Encrypt user password///</summary>///<param name= "password" > password </param>///<param name= "Codelength" > Encryption bits </param>///<returns> encrypt password </returns>public static string md5 (string password, int codelength) { if (!string. IsNullOrEmpty (password)) { //16-bit MD5 encryption (take 32-bit encrypted 9~25 characters) if (codelength = =) { return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (password, "MD5"). ToLower (). Substring (8, +); } 32-bit Encryption if (codelength = =) { return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (password, "MD5"). ToLower (); } } return string. Empty;}
Because the MD5 is irreversible, so after the encryption can not be decrypted, take the user name and password, you need to encrypt the user input data and the database encrypted data. If the result is the same, you can determine the success of the landing! The code looks like this:
<summary>///Login///</summary>public model.userinfo Userlogon (String USERID, String pwd, out string status Code) { //Assume that the model object model.userinfo model = Getmodel (USERID) has been obtained through the user ID to UserInfo; if (model = null) { if (model. PASSWORD = = Md5encrypt64 (pwd) { StatusCode = "Login successful"; } else { StatusCode = "Password error"; } } else { StatusCode = "user does not exist! "; model = null; } return model;}
5) Encrypt and decrypt strings by DESCryptoServiceProvider objects
<summary>///des data encryption///</summary>///<param name= "Targetvalue" > Target value </param>///<param Name= "key" > key </param>///<returns> Encrypted 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); The initialization vector des of the symmetric algorithm is set by a two-time hash cipher. Key = Encoding.ASCII.GetBytes (formsauthentication.hashpasswordforstoringinconfigfile (FormsAuthentication.HashPasswordForStoringInConfigFile (Key, "MD5"). Substring (0, 8), "SHA1"). Substring (0, 8)); The secret key of the algorithm is set by a two-time hash cipher 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 by the encryption key, and the method of decryption 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; }//Define 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; }//The initialization vector des of the symmetric algorithm is set by a two-time hash cipher. Key = Encoding.ASCII.GetBytes (formsauthentication.hashpasswordforstoringinconfigfile (FormsAuthentication.HashPasswordForStoringInConfigFile (Key, "MD5"). Substring (0, 8), "SHA1"). Substring (0, 8)); The secret key of the algorithm is set by a two-time hash cipher Des.iv = Encoding.ASCII.GetBytes (FOrmsauthentication.hashpasswordforstoringinconfigfile (Formsauthentication.hash Passwordforstoringinconfigfile (Key, "MD5"). Substring (0, 8), "MD5"). Substring (0, 8)); Define memory flow var ms = new MemoryStream (); Defines the cryptographic stream var cs = new CryptoStream (MS, Des. CreateDecryptor (), cryptostreammode.write); Cs. Write (Inputbytearray, 0, inputbytearray.length); Cs. FlushFinalBlock (); Return Encoding.Default.GetString (Ms. ToArray ());}
Source Address: http://www.cnblogs.com/healer007/p/5062189.html
C #: Encrypt and decrypt user passwords using MD5