Java MD5 Encryption

Source: Internet
Author: User
Tags md5 encryption

Today in the register login to remember to do a password to do an encryption, and then go online search with which aspects of technology, and finally determine the Java MD5 one-way encryption, the advantage is that encryption is not decrypted, in favor of the security of data, code is relatively small, but need to carefully understand why to write this, I'll post the code first.

I need to encrypt the password into a 32-bit string.

 Public classMD5 { Public StaticString Gethash (String password)throwsnosuchalgorithmexception {///Get MD5 algorithm instance get a MD5 message digestMessageDigest m = messagedigest.getinstance ("MD5"); //add information to calculate a summarym.update (Password.getbytes ()); //get the summary and get 16 characters        byte[]bytes =m.digest (); StringBuffer SB=NewStringBuffer ();  for(byteb:bytes) {           //a byte is 8 bits, and an int is 32 bits, so a byte height of 24 bits is random when 0XFF is not used .//This conversion is prone to error, and using 0xFF, his low 8 bits are 1, the height 24 bits are 0, so that the high 24 bits of byte are displaced bit 0, and the lower 8 bits can be the original numberString s = integer.tohexstring (0xff&b); if(S.length () ==1){               //if the 32-characterSb.append ("0" +s); }Else{sb.append (s); }       }        returnsb.tostring (); }}

First, in the following code, I encrypt my password into a 16-bit string.

// /Get MD5 algorithm instance get a MD5 message digest        MessageDigest m = messagedigest.getinstance ("MD5");         // add information to calculate a summary         m.update (Password.getbytes ());         // get the summary and get 16 characters        byte []bytes  = M.digest ();

My needs of 32 bits so continue to encrypt and here there will be more difficult to understand a sentence

String s = integer.tohexstring (0xff&b);
StringBuffer SB =NewStringBuffer ();  for(byteb:bytes) {           //a byte is 8 bits, and an int is 32 bits, so a byte height of 24 bits is random when 0XFF is not used .//This conversion is prone to error, and using 0xFF, his low 8 bits are 1, the height 24 bits are 0, so that the high 24 bits of byte are displaced bit 0, and the lower 8 bits can be the original numberString s = integer.tohexstring (0xff&b); if(S.length () ==1){               //if the 32-characterSb.append ("0" +s); }Else{sb.append (s); }       }
I do not understand the beginning of the meaning of the sentence, the first what is 0xFF, the second why to &b, the third why to use the int type to convert?

First to analyze the first question, why use 0xFF. The 0xff represents the decimal 255, the binary is the low eight bits are 1, the other 24 bits are 0.

The second question is why I want to &b, a byte is 8 bytes, except that the low eight bits are fixed and the rest are random. And the 0xFF eighth bit is 1, the rest is 0, and the result is that the eighth bit of B is still unchanged, the rest of the bits have become 0, so that the data can be unified, not the last encrypted data inconsistent.

The third question, why use int, because int is 32 bits, and I just want to encrypt 32 bits of data, so it is necessary to work with Int.

The above is my understanding of MD5 encryption code.

Java MD5 Encryption

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.