Use Java to generate MD5 Encoding

Source: Internet
Author: User

MD5 is message-Digest algorithm 5 (Information-AbstractAlgorithm5) is a single hash algorithm used to generate digital signatures. In 1991, it was developed by MIT laboratories for computer science) and RSA Data Security Inc (RSA Data Security Company) Ronald L. professor Rivest developed and developed through md2, md3 and md4. You do not have to pay any copyright fees for using the MD5 algorithm. The function of this function is to compress large-capacity information into a confidential format before using the digital signature software to sign a private key (using an "Byte string" of any length through an irreversible string the transform algorithm is transformed into a big integer of bits, in other words, even if you see the sourceProgramAnd algorithm description, it is impossible to convert an MD5 value back to the original string, in terms of mathematical principle, because the original string has an infinite number, this is a bit like a mathematical function without an inverse function .)

In Java, java. Security. messagedigest defines the MD5 calculation. Therefore, we only need to call it to obtain the 128-bit integer of MD5. Then, convert the 16 bytes of the 128 bits into hexadecimal notation.

CodeAs follows:

Package com. Tsinghua;

/**

* The MD5 algorithm is defined in rfc1321.

* In RFC 1321, test suite is provided to verify whether your implementation is correct:

* MD5 ("") = d41d8cd98f00b204e9800998ecf8427e

* MD5 ("A") = 0cc175b9c0f1b6a831c399e269772661

* MD5 ("ABC") = 900150983cd24fb0d6963f7d28e17f72

* MD5 ("Message Digest") = f96b697d7cb7938d525a2f31aaf161d0

* MD5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b

*

* @ Author haogj

*

* Input parameter: A byte array

* Outgoing parameter: MD5 result string of the byte array

*/

Public class MD5 {

Public static string getmd5 (byte [] source ){

String S = NULL;

Char hexdigits [] = {// used to convert bytes into hexadecimal characters

'0', '1', '2', '3', '4', '5', '6', '7', '8', '9 ', 'A', 'B', 'C', 'D', 'E', 'E', 'F '};

Try

{

Java. Security. messagedigest MD = java. Security. messagedigest. getinstance ("MD5 ");

Md. Update (source );

Byte TMP [] = md. Digest (); // the MD5 calculation result is a 128-bit long integer,

// 16 bytes

Char STR [] = new char [16*2]; // if each byte is in hexadecimal notation, two characters are used,

// Therefore, it must be 32 characters in hexadecimal notation.

Int K = 0; // indicates the character position in the conversion result.

For (INT I = 0; I <16; I ++) {// each MD5 byte starting from the first byte

// Convert to hexadecimal character conversion

Byte byte0 = TMP [I]; // obtains the I-th byte.

STR [k ++] = hexdigits [byte0> 4 & 0xf]; // converts the numbers of the top 4 digits in a byte,

// >>> Shifts the right of the logic and shifts the symbol bit together to the right.

STR [k ++] = hexdigits [byte0 & 0xf]; // converts the numbers of the Middle and Lower digits in the byte.

}

S = new string (STR); // convert the result to a string

} Catch (exception E)

{

E. printstacktrace ();

}

Return S;

}

}

The test code is as follows:

Import com. Tsinghua .*;

Public class testmd5

{

Public static void main (string Xu [])

{// Calculate the MD5 code of "A", which should be: 0cc175b9c0f1b6a831c399e269772661

System. Out. println (md5.getmd5 ("A". getbytes ()));

}

}

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.