Introduction to Java Basic----> Digest Algorithms (GO)

Source: Internet
Author: User
Tags hmac

Data digest algorithm is a very important branch of cryptography algorithm, it can be used to encrypt all the data by extracting fingerprint information to realize data signature, data integrity checking and so on, because of its irreversibility. The Data digest algorithm is also known as hash algorithm and hash algorithm. Today, we begin to learn the abstract algorithm in Java.

The project structure is as follows:

SHA algorithm

Secure Hash algorithm is primarily intended for digital Signature algorithm DSA, which is defined in the digital Signature standard DSS. For messages that are less than 2^64 bits in length, SHA1 produces a 160-bit message digest. The algorithm has been developed and improved by cryptographic experts for many years and is widely used. The idea of the algorithm is to receive a piece of plaintext, and then convert it into a paragraph (usually smaller) ciphertext in an irreversible way, or simply to take a string of input codes (called Pre-mapping or information) and convert them to shorter lengths, A fixed number of bits of output sequence is the process of hashing values (also known as information digests or information authentication codes). The hash function value can be said to be a "fingerprint" or "digest" of the plaintext, so the digital signature of the hash value can be regarded as the digital signature of this plaintext.

First, Huhxsha.java

Package Com.huhx.md;import Java.security.messagedigest;import Org.apache.commons.codec.binary.hex;import Org.apache.commons.codec.digest.digestutils;import Org.bouncycastle.crypto.digest;import Org.bouncycastle.crypto.digests.sha1digest;import org.junit.test;/** * Wirter:huhx */public class HuhxSHA {private St    atic String src = "http://www.cnblogs.com/huhx"; The JDK version of the SHA algorithm @Test public void jdkSHA1 () {try {messagedigest messagedigest = Messagedigest.get            Instance ("SHA");            Messagedigest.update (Src.getbytes ());            byte[] shabytes = Messagedigest.digest ();        System.out.println ("JDK SHA 1:" + hex.encodehexstring (shabytes));        } catch (Exception e) {e.printstacktrace ();        }}//Commons-codec sha algorithm @Test public void bcSHA1 () {Digest Digest = new Sha1digest ();                Digest.update (Src.getbytes (), 0, Src.length ());        byte[] shabytes = new byte[digest.getdigestsize ()]; DIgest.dofinal (shabytes, 0);    System.out.println ("BC SHA 1:" + org.bouncycastle.util.encoders.Hex.toHexString (shabytes)); }//Bcprov sha algorithm @Test public void ccSHA1 () {System.out.println ("cc SHA 1:" + Digestutils.sha1hex (s    Rc.getbytes ())); }}

Second, the results of the operation are as follows:

JDK sha 1:14ED5F04B940042DF8DFCD5E60DC331DFDDAC16FBC sha 1:14ed5f04b940042df8dfcd5e60dc331dfddac16fcc sha 1: 14ed5f04b940042df8dfcd5e60dc331dfddac16f

MD algorithm

MD2
Rivest developed the MD2 algorithm in 1989. In this algorithm, the information is first used to complement the data, so that the byte length of the information is a multiple of 16. Then, a 16-bit test and append to the end of the information, and based on the newly generated information to calculate the hash value. Later, Rogier and Chauvaud found that if the tests were ignored, the MD2 would be conflicted. The results are unique after the MD2 algorithm is encrypted (that is, the results are different after the encryption of different information).

MD4
In order to enhance the security of the algorithm, Rivest developed the MD4 algorithm in 1990. The MD4 algorithm also needs to fill the information to ensure that the bits length of the information minus 448 can be divisible by 512 (information bit-length mod 512 = 448). Then, a 64-bit binary representation of the initial length of the information is added. The information is processed into chunks of the 512-bit damg?rd/merkle iteration structure, and each chunk is processed by three different steps. Den Boer and Bosselaers and others quickly discovered vulnerabilities in the first and third steps of the attack on the MD4 version. Dobbertin showed you how to use a normal PC to find a conflict in the full version of MD4 in a matter of minutes (this conflict is actually a vulnerability that would result in encryption of different content and possibly the same encrypted result). There is no doubt that MD4 was knocked out of this. Although the MD4 algorithm has such a large security vulnerability, it has no negligible guidance on the emergence of several kinds of information security encryption algorithms that have been developed since then.

MD5
In 1991, Rivest developed the MD5 algorithm, which is more and more mature in technology. It adds the concept of "safety-tape" (safety-belts) on the basis of MD4. Although MD5 is much more complex than MD4, it is more secure. The algorithm is clearly composed of four and MD4 designs with slightly different steps. In the MD5 algorithm, the information-digest size and the requirements for padding are exactly the same as the MD4. Den Boer and Bosselaers have found false collisions in the MD5 algorithm (pseudo-collisions), but there are no other results that have been found to be encrypted.

First, Huhxmd.java

Package Com.huhx.md;import Java.security.messagedigest;import Java.security.security;import Org.apache.commons.codec.digest.digestutils;import Org.bouncycastle.crypto.digest;import Org.bouncycastle.crypto.digests.md5digest;import Org.bouncycastle.jce.provider.bouncycastleprovider;import Org.bouncycastle.util.encoders.hex;import Org.junit.test;public class Huhxmd {private static String src = "http://www.    Cnblogs.com/huhx ";            @Test public void JdkMD5 () {try {messagedigest messagedigest = messagedigest.getinstance ("MD5");            byte[] mdbytes = Messagedigest.digest (Src.getbytes ());        SYSTEM.OUT.PRINTLN ("MD5 decode:" + hex.tohexstring (mdbytes));        } catch (Exception e) {e.printstacktrace (); }} @Test public void jdkMD2 () {try {messagedigest messagedigest = messagedigest.getinstance (            "MD2");            byte[] mdbytes = Messagedigest.digest (Src.getbytes ()); System.out.println ("MD2 decode: "+ hex.tohexstring (mdbytes));        } catch (Exception e) {e.printstacktrace ();        }} @Test public void BcmMD4 () {//Digest Digest = new Md4digest ();        Digest.update (Src.getbytes (), 0, Src.length ());        byte[] mdbytes = new byte[digest.getdigestsize ()];        Digest.dofinal (mdbytes, 0);        System.out.println ("MD4 decode:" + hex.tohexstring (mdbytes));        Security.addprovider (New Bouncycastleprovider ());            try {messagedigest messagedigest = messagedigest.getinstance ("MD4");            byte[] mdbytes = Messagedigest.digest (Src.getbytes ());        System.out.println ("MD4 decode:" + hex.tohexstring (mdbytes));        } catch (Exception e) {e.printstacktrace ();        }}//@Test public void bcmMD5 () {Digest Digest = new Md5digest ();        Digest.update (Src.getbytes (), 0, Src.length ());        byte[] mdbytes = new byte[digest.getdigestsize ()]; Digest.dofinal (mdbytES, 0);    SYSTEM.OUT.PRINTLN ("MD5 decode:" + hex.tohexstring (mdbytes));        }//@Test public void ccMD5 () {String md5string = Digestutils.md5hex (Src.getbytes ());    SYSTEM.OUT.PRINTLN ("Common MD5:" + md5string); }}

Second, the results of the operation are as follows:

MD4 decode:b402321e9a067da3df0c36c8315f8e38md5 DECODE:F121BF5F7491466AE75E056F686C4462MD2 Decode: 42B6B066B1273470F9AAD644CEDE7644MD5 Decode:f121bf5f7491466ae75e056f686c4462common MD5: f121bf5f7491466ae75e056f686c4462

Mac algorithm

MAC algorithm (message authentication Codes) hash function with secret key: the hash value of the message is controlled by the secret key K, which is known only by both parties to the communication. At this point the hash value is called Mac.

Mac algorithm principle (in the case of direct Union UnionPay POS and POS Center communication).

    • The portion of the message that will be sent to the POS center, from the message type (MTI) to the 63 domain, forms the Mac Elemement BLOCK (MAB).
    • For Mab, make an XOR of every 8 bytes (regardless of the character format in the message) and add "0x00" if the last one is less than 8 bytes.

First, Huhxmac.java

Package Com.huhx.md;import Javax.crypto.keygenerator;import Javax.crypto.mac;import javax.crypto.secretkey;import Javax.crypto.spec.secretkeyspec;import Org.bouncycastle.crypto.digests.md5digest;import Org.bouncycastle.crypto.macs.hmac;import Org.bouncycastle.crypto.params.keyparameter;import Org.bouncycastle.util.encoders.hex;import Org.junit.test;public class Huhxmac {private static String src = "http://www    . Cnblogs.com/huhx ";    private static String Decodekey = "BBBBBBBBBB"; @Test public void JdkHmacMD5 () {try {//initialize keygenerator keygenerator keygenerator = keyg            Enerator.getinstance ("HmacMD5");            Generate key Secretkey Secretkey = Keygenerator.generatekey ();            Get key//byte[] keybytes = secretkey.getencoded ();            byte[] keybytes = Org.apache.commons.codec.binary.Hex.decodeHex (Decodekey.tochararray ()); Restore key Secretkeyspec Secretkeyspec = new Secretkeyspec (keybytes, "HmacMD5");            Instantiate Mac Mac Mac = Mac.getinstance (Secretkeyspec.getalgorithm ());            Initialize Mac Mac.init (SECRETKEYSPEC);            Executive Summary byte[] result = Mac.dofinal (Src.getbytes ());        System.out.println ("JDK mac:" + hex.tohexstring (result));        } catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();        }} @Test public void BcHmacMD5 () {HMAC HMAC = new HMAC (new Md5digest ());        Hmac.init (New Keyparameter (Hex.decode (Decodekey)));        Hmac.update (Src.getbytes (), 0, Src.length ());        byte[] hmacbytes = new byte[hmac.getmacsize ()];        Hmac.dofinal (hmacbytes, 0);    SYSTEM.OUT.PRINTLN ("BC Mac:" + hex.tohexstring (hmacbytes)); }}

Second, the results of the operation are as follows:

BC MAC:3BF59D550B1E0D6CEE15E015870029F9JDK MAC:3BF59D550B1E0D6CEE15E015870029F9

Http://www.cnblogs.com/huhx/p/messageDigest.html

Introduction to Java Basic----> Digest Algorithms (GO)

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.