Java implements MD5 Encryption Through java. security. MessageDigest

Source: Internet
Author: User

 

Here we only write out the main method, and call the main method. Don't forget to package it.

 

 
  1. /**
  2. * Java. security. MessageDigest class implements encryption algorithms such as MD5 SHA.
  3. */
  4. Public static void MD5Test (){
  5. MessageDigest md = null;
  6. Try {
  7. Md = MessageDigest. getInstance ("MD5 ");
  8. } Catch (NoSuchAlgorithmException e ){
  9. E. printStackTrace ();
  10. }
  11. String s = "df9f"; // String to be encrypted
  12. System. out. println ("encrypted string:" + s );
  13. Md. update (s. getBytes (); // the MD5 encryption algorithm only encrypts the character array rather than the string to obtain the object to be encrypted.
  14. Byte [] bs = md. digest (); // performs encryption and returns a String Array
  15. For (byte B: bs ){
  16. System. out. print (B + "|"); // print out the encrypted character array
  17. }
  18. System. out. println ();
  19. StringBuffer sb = new StringBuffer ();
  20. For (int I = 0; I <bs. length; I ++) {// The byte array is converted into a hexadecimal string to form the final ciphertext.
  21. Int v = bs [I] & 0xff;
  22. If (v <16 ){
  23. Sb. append (0 );
  24. }
  25. Sb. append (Integer. toHexString (v ));
  26. }
  27. System. out. println ("encrypted string:" + sb );
  28. }

This is:

650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1233212519-0.png "/>

 

This article is from the "orangle lzz notebook" blog, please be sure to keep this source http://lzzjava.blog.51cto.com/2554001/911086

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.