MD5 encryption is required during the development of the project, using the encryption method at the very beginning:
Public Static stringGetMD5 (stringstr) { byte[] B =System.Text.Encoding.Default.GetBytes (str); b=NewSystem.Security.Cryptography.MD5CryptoServiceProvider (). ComputeHash (b); stringRET =""; for(inti =0; i < b.length; i++) {ret+ = B[i]. ToString ("x"). PadLeft (2,'0'); } returnret;}
There was no problem at the beginning of the test, and later the incoming parameter contained the Chinese, then there was a problem. after troubleshooting the rebate Encoding.default is using the default encoding: gb2312
So change the encryption method:
Public Static stringUSERMD5 (stringstr) { stringCL =str; stringPWD =""; MD5 MD5= MD5. Create ();//instantiate a MD5 pair of images
//after the encryption is an array of byte type, here should pay attention to the choice of coding utf8/unicode, etc. byte[] s =Md5.computehash (Encoding.UTF8.GetBytes (CL)); //converts an array of byte types to a string by using a loop, which is a regular character formatting the resulting for(inti =0; i < s.length; i++) { //The resulting string is formatted using the hexadecimal type. The formatted character is a lowercase letter, and if uppercase (X) is used, the character after the format is uppercase charactersPWD = pwd + s[i]. ToString ("x"); } returnpwd; }
but in and the other side of the test process, found my side of the MD5 encryption code, often appear less one or several problems; later analysis was found to be a string format character, x for uppercase, x for lowercase, X2 and X2 to not omit the first 0 hexadecimal digits;
For example: ox0a, using x== 0xA, using x2==0x0a
The final output format of the method is changed:
Public Static stringUSERMD5 (stringstr) { stringCL =str; stringPWD =""; MD5 MD5= MD5. Create ();//instantiate a MD5 pair of images//after the encryption is an array of byte type, here should pay attention to the choice of coding utf8/unicode, etc. byte[] s =Md5.computehash (Encoding.UTF8.GetBytes (CL)); //converts an array of byte types to a string by using a loop, which is a regular character formatting the resulting for(inti =0; i < s.length; i++) { //The resulting string is formatted using the hexadecimal type. The formatted character is a lowercase letter, and if uppercase (X) is used, the character after the format is uppercase charactersPWD = pwd + s[i]. ToString ("X2"); } returnpwd; }
C # MD5 32-bit encryption UTF-8 encoding