C # MD5 32-bit encryption UTF-8 encoding

Source: Internet
Author: User
Tags md5 encryption

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.