Investigation and Management System-(2) basic process and implementation of MD5 Encryption

Source: Internet
Author: User

The basic process of MD5 encryption:

1. encode a string into a byte array byte [] bytes through the getbytes () method;

2. After the bytes array is encrypted by the MD5 algorithm, a new 16-byte array md5bytes is obtained;

3. Convert the md5bytes array to a string for saving (generally, this process is to convert the encrypted md5bytes to the hexadecimal [0 ~ F] The saved 32-Bit String)

Specific implementation:

 1   Package  Com. atguigu. surveypark. util;  2   Import Java. Security. messagedigest;  3   Public   Class  Datautil {  4       /**  5   * Use the MD5 Algorithm for encryption  6        */  7       Public   Static  String MD5 (string SRC ){  8           Try {  9 Stringbuffer buffer = New  Stringbuffer ();  10               Char [] Chars = {'0', '1', '2', '3', '4', '5', '6', '7 ', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };  11               Byte [] Bytes = SRC. getbytes ();  12 Messagedigest MD = messagedigest. getinstance ("MD5" );  13              Byte [] Targ = Md. Digest (bytes );  14               For ( Byte  B: targ ){  15 Buffer. append (chars [(B> 4) & 0x0f ]);// The Current byte moves four digits to the right and then performs the "and operation" with (0x00001111), that is, the operation on the top four digits.  16 Buffer. append (chars [B & 0x0f ]);// Perform and operate the lower four bits.  17   } 18               Return  Buffer. tostring ();  19 } Catch  (Exception e ){  20   E. printstacktrace ();  21   }  22           Return   Null  ;  23   } 24 }

In addition to converting encrypted byte arrays, you can also directly useCommons-codec.jarThe method provided by the package to encrypt the string.

For example, string md5digest = digestutils. md5hex ("12345 ");

For commons-codec.jar package reference: http://www.cnblogs.com/tingzi/archive/2012/08/17/2643742.html (Commons codec jar package details)

 

Investigation and Management System-(2) basic process and implementation of MD5 Encryption

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.