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