Java. security. MessageDigest

Source: Internet
Author: User

We know that data transmission and storage in programming requires encryption to consider security issues. let's take the database as an example. if the database of a user registration system does not save the user information, for example, when I register the database on the page, enter "Vicky", "123456 ". registration. if the web server does not encrypt the data and directly writes it to the database, the user information in the database is a directly available data! Once the server is hacked ~ Then the user's information will be unretained in front of hackers... to solve this problem, most of the information is now encrypted using MD5. for example, after "Vicky" and "123456" are encrypted, a 16-bit or 32-Bit String is generated. hackers cannot even use the data...

MD5 is a common encryption method. The java. security. MessageDigest encryption method in JDK is described here!

[Java]View plaincopyprint?
  1. @ Test
  2. Public void testMD (){
  3. Try {
  4. String username = "Vicky ";
  5. MessageDigest messageDigest = MessageDigest. getInstance ("MD5 ");
  6. MessageDigest. update (username. getBytes ());
  7. String usernameMD5 = messageDigest. digest (). toString ();
  8. System. out. println (usernameMD5 );
  9. } Catch (Exception e ){
  10. E. printStackTrace ();
  11. }
  12. }

@ Test <br/> public void testMD () {<br/> try {<br/> String username = "Vicky"; <br/> MessageDigest messageDigest = MessageDigest. getInstance ("MD5"); <br/> messageDigest. update (username. getBytes (); <br/> String usernameMD5 = messageDigest. digest (). toString (); <br/> System. out. println (usernameMD5); <br/>} catch (Exception e) {<br/> e. printStackTrace (); <br/>}< br/>}Printed: [B @ 107077e, because the output is byte [] (messageDigest. digest () is a binary byte array. Some bytes may be non-printable characters .)... We can use Base64 to process byte []

 

[Java]View plaincopyprint?
  1. @ Test
  2. Public void testMD (){
  3. Try {
  4. String username = "Vicky ";
  5. MessageDigest messageDigest = MessageDigest. getInstance ("MD5 ");
  6. MessageDigest. update (username. getBytes ());
  7. System. out. println (Base64.encode (messageDigest. digest ()));
  8. } Catch (Exception e ){
  9. E. printStackTrace ();
  10. }
  11. }

@ Test <br/> public void testMD () {<br/> try {<br/> String username = "Vicky"; <br/> MessageDigest messageDigest = MessageDigest. getInstance ("MD5"); <br/> messageDigest. update (username. getBytes (); <br/> System. out. println (Base64.encode (messageDigest. digest (); <br/>} catch (Exception e) {<br/> e. printStackTrace (); <br/>}< br/>}

 

Printed: AgwpBZPO + ErqxOosJp0ybQ =

Of course, we can write functions to process binary to hex strings.

For example:

[Java]View plaincopyprint?
  1. /**
  2. * Convert a 16-bit byte [] to a 32-Bit String
  3. *
  4. * @ Param buffer
  5. * @ Return
  6. */
  7. Private String toHex (byte buffer []) {
  8. StringBuffer sb = new StringBuffer (buffer. length * 2 );
  9. For (int I = 0; I <buffer. length; I ++ ){
  10. Sb. append (Character. forDigit (buffer [I] & 240)> 4, 16 ));
  11. Sb. append (Character. forDigit (buffer [I] & 15, 16 ));
  12. }
  13. Return sb. toString ();
  14. }

/** <Br/> * convert 16-bit byte [] to 32-Bit String <br/> * @ param buffer <br/> * @ return <br/> */<br/> private String toHex (byte buffer []) {<br/> StringBuffer sb = new StringBuffer (buffer. length * 2); <br/> for (int I = 0; I <buffer. length; I ++) {<br/> sb. append (Character. forDigit (buffer [I] & 240)> 4, 16); <br/> sb. append (Character. forDigit (buffer [I] & 15, 16); <br/>}</p> <p> return sb. toString (); <br/>}

 

Write test statements

[Java]View plaincopyprint?
  1. @ Test
  2. Public void testMD (){
  3. Try {
  4. String username = "Vicky ";
  5. MessageDigest messageDigest = MessageDigest. getInstance ("MD5 ");
  6. MessageDigest. update (username. getBytes ());
  7. System. out. println (toHex (messageDigest. digest ()));
  8. } Catch (Exception e ){
  9. E. printStackTrace ();
  10. }
  11. }

@ Test <br/> public void testMD () {<br/> try {</p> <p> String username = "Vicky "; <br/> MessageDigest messageDigest = MessageDigest. getInstance ("MD5"); <br/> messageDigest. update (username. getBytes (); <br/> System. out. println (toHex (messageDigest. digest (); <br/>} catch (Exception e) {<br/> e. printStackTrace (); <br/>}< br/>}

 

Print: 020c290593cef84aeac4ea2c269d326d. A 32-Bit String is returned !!!

In this way, we can directly use the encryption classes and functions that JDK provides for us!

 

MessageDigest not only provides "MD5" encryption for us, but also provides "SHA-1"

Only MessageDigest messageDigest = MessageDigest. getInstance ("SHA-1 ");

The difference between MD5 and SHA-1 is: MD5 is 16 bits, SHA is 20 bits (these are two packet digest algorithms)

 

Can MessageDigest be used only for data encryption? How to Use MessageDigest to generate a security token !!!

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.