Java encryption and decryption Research 6, MD algorithm Family

Source: Internet
Author: User
Tags md5 encryption

First, brief

MD5 algorithm is a typical message digest algorithm, its predecessor is MD2, MD3 and MD4 algorithm, which is improved by MD4, MD3 and MD2 algorithm. Whichever MD algorithm, they all need to get a random length of information and generate a 123-bit message digest. If this 128-bit binary summary information is converted to 16, you can get a 32-bit string, so the majority of the MD5 algorithm we see the digital fingerprint is 32 16 binary string.

The history of MD algorithm family:

MD2 algorithm:

1989, one of the famous asymmetric algorithm RSA inventors----MIT professor Ronald Leevist developed the MD2 algorithm. This algorithm first makes a data complement to the information, so that the byte length of the information is a multiple of 16. A 16-bit test and additional information is appended to the end of the original information. Finally, a 128-bit hash value is computed based on the newly generated information, and the MD2 algorithm is born.

MD4 algorithm:

In 1990, Professor Ronald Leevist developed a MD4 algorithm with higher security than the MD2 algorithm. In this algorithm, we still need to make a data complement to the information. The difference is that this complement makes the byte length of its information add 448 bytes and becomes a multiple of 512 (information byte length mod 512 =448). In addition, there is a big difference between the processing of MD4 and the MD2 algorithm. Eventually, however, a hash value of 128 is still obtained. MD4 algorithm plays a driving role in the subsequent Message digest algorithm, and many more famous message digest algorithms are developed on the basis of the MD4 algorithm, such as MD5, SHA-1, RIPE-MD and Haval algorithms.

MD5 algorithm:

In 1991, following the MD4 algorithm, Professor Ronald Leevist developed the MD5 algorithm, which pushed the MD algorithm to maturity. The MD5 algorithm is developed by MD2, MD3 and MD4 algorithms, the complexity of the algorithm and the security intensity are improved, but the final result of the MD algorithm in western Zhejiang is to produce a 128-bit information digest. This is also characteristic of the MD series algorithm.



MD2 and MD5 algorithm applications provided by Java

 Packagecom.tao.test;Importjava.io.UnsupportedEncodingException;Importjava.security.MessageDigest;ImportJavax.xml.bind.annotation.adapters.HexBinaryAdapter;/*** MD Encryption Tool class*/classMdcoder {//MD2 Encryption     Public StaticString EncodeMd2 (byte[] data)throwsException {//Initialize MessageDigestMessageDigest MD = messagedigest.getinstance ("MD2"); //Executive Summary Information        byte[] Digest =md.digest (data); //Convert summary information to a 32-bit hexadecimal string        return NewString (NewHexbinaryadapter (). Marshal (Digest)); }    //MD5 Encryption     Public StaticString EncodeMd5 (byte[] data)throwsException {//Initialize MessageDigestMessageDigest MD = messagedigest.getinstance ("MD5"); //Executive Summary Information        byte[] Digest =md.digest (data); //Convert summary information to a 32-bit hexadecimal string        return NewString (NewHexbinaryadapter (). Marshal (Digest)); }} Public classMdtest { Public Static voidMain (string[] args)throwsunsupportedencodingexception, Exception {String teststring= "123456ASDASDFSDFSDFSDF";        System.out.println (MDCODER.ENCODEMD2 (Teststring.getbytes ()));    System.out.println (Mdcoder.encodemd5 (Teststring.getbytes ())); }}

Second, the application of the MD4 algorithm of Bouncycastle supplement

 PackageTest;Importjava.io.UnsupportedEncodingException;Importjava.security.MessageDigest;Importjava.security.Security;ImportOrg.bouncycastle.jce.provider.BouncyCastleProvider;ImportOrg.bouncycastle.util.encoders.Hex;/*** MD Encryption Tool class*/classMdcoder {//MD2 Encryption     Public StaticString EncodeMd4 (byte[] data)throwsException {//join Bouncycastle's supportSecurity.addprovider (NewBouncycastleprovider ()); //Initialize MessageDigestMessageDigest MD = messagedigest.getinstance ("MD4"); //Executive Summary Information        byte[] digest=md.digest (data); //Convert summary information to a 32-bit hexadecimal string        return NewString (Hex.encode (Digest)); }} Public classMdtest { Public Static voidMain (string[] args)throwsunsupportedencodingexception, Exception {String teststring= "123456ASDASDFSDFSDFSDF"; String digeststring=MDCODER.ENCODEMD4 (Teststring.getbytes ());    System.out.println (digeststring); }}

Iii. implementation provided by Commons codec

Commons codec provides a message digest for the tool class-----the Digestutils class. The Digestutils class is an encapsulation of the MessageDigest class provided by Sun, providing implementations of the MD5 and SHA Series message digest algorithms.

 PackageTest;Importjava.io.UnsupportedEncodingException;ImportOrg.apache.commons.codec.binary.Hex;Importorg.apache.commons.codec.digest.DigestUtils;/*** MD Encryption Tool class*/classMdcoder {//MD5 Encryption Returns a hexadecimal string     Public StaticString Encodemd5hex (byte[] data)throwsException {returnDigestutils.md5hex (data); }    //MD5 Encrypted, returns a decimal byte array     Public Static byte[] EncodeMd5 (byte[] data) {        returndigestutils.md5 (data); }} Public classMdtest { Public Static voidMain (string[] args)throwsunsupportedencodingexception, Exception {String teststring= "123456ASDASDFSDFSDFSDF"; //16 binary stringString digeststring =Mdcoder.encodemd5hex (Teststring.getbytes ());                System.out.println (digeststring); //byte array        byte[] digest=mdcoder.encodemd5 (Teststring.getbytes ()); String dstring=NewString (Hex.encodehex (Digest));    System.out.println (dstring); }}

In practical application, the MD message digest algorithm is implemented according to the choice of different methods.

Transferred from: http://blog.csdn.net/lonelyroamer/article/details/7652320

Java encryption and decryption Research 6, MD algorithm Family

Related Article

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.