Use Java to generate MD5 Encoding

Source: Internet
Author: User

Use Java to generate MD5 EncodingMD5 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 irreversible string for a "Byte string" of any length 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.

Code As follows:
Import Java. security. messagedigest; <br/> public class MD5 {<br/> Public static string getmd5 (string source) {<br/> string S = NULL; <br/> // used to convert bytes into hexadecimal characters <br/> char hexdigits [] = {'0', '1', '2 ', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C ', 'D', 'E', 'F'}; <br/> try {<br/> messagedigest MD = messagedigest. getinstance ("MD5"); <br/> Md. update (source. getbytes (); <br/> // the MD5 calculation result is a 128-bit long integer. <br/> byte TMP [] = md. digest (); </P> <p> // represents 16 bytes. <br/> // if each byte is in hexadecimal notation, two characters are used, <br/> // It must be 32 characters in hexadecimal notation <br/> char STR [] = new char [16*2]; <br/> // indicates the character position in the conversion result. <br/> int K = 0; <br/> // starting from the first byte, convert each MD5 byte <br/> // to a hexadecimal character <br/> for (INT I = 0; I <16; I ++) {<br/> // take the I-th byte <br/> byte byte0 = TMP; <br/> // convert the numbers of the 4-digit high in the byte, >>> logically shifted to the right, move the symbol bits together to the right <br/> STR [k ++] = hexdigits [byte0 >>> 4 & 0xf]; <br/> // convert the numbers of the Middle and Lower 4 digits in a byte <br/> STR [k ++] = hexdigits [byte0 & 0xf]; <br/>}< br/> // convert the result to a string <br/> S = new string (STR); <br/>} catch (exception E) {<br/> E. printstacktrace (); <br/>}< br/> return S; <br/>}</P> <p> // method 2 <br/> Public static string getmd5second (string Str) {<br/> stringbuilder sb = new stringbuilder (); <br/> try {<br/> messagedigest MD5 = messagedigest. getinstance ("MD5"); <br/> md5.update (Str. getbytes (); <br/> for (byte B: md5.digest () {<br/> Sb. append (string. format ("% 02x", B); <br/>}< br/>} catch (nosuchalgorithmexception e) {<br/> E. printstacktrace (); <br/>}< br/> return sb. tostring (); <br/>}</P> <p> // calculate the MD5 code of "A", which should be: 0cc175b9c0f1b6a831c399e269772661 <br/> Public static void main (string Xu []) {<br/> system. out. println (md5.getmd5 ("A"); <br/>}< br/>}

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.