手记

消息摘要算法的java代码

消息摘要算法
1.MD5

package com.security3;

import java.security.MessageDigest;
import java.security.Security;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.MD4Digest;
import org.bouncycastle.crypto.digests.MD5Digest;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class LoveMD {
    private static String src="imooc security md";
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        jdkMD5();
        bcMD4();
        bcMD5();
        bcMD42();
        ccMD5();
        ccMD2();

    }
    public static void jdkMD5(){
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] md5Bytes=md.digest(src.getBytes());
            System.out.println("JDK MD5:"+Hex.encodeHexString(md5Bytes));
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    public static void bcMD4(){
        Digest digest=new MD4Digest();
        digest.update(src.getBytes(), 0,src.getBytes().length);
        byte[] md4Bytes=new byte[digest.getDigestSize()];
        digest.doFinal(md4Bytes, 0);
        System.out.println("BC MD4:"+org.bouncycastle.util.encoders.Hex.toHexString(md4Bytes));
    }
    public static void bcMD5(){
        Digest digest=new MD5Digest();
        digest.update(src.getBytes(), 0,src.getBytes().length);
        byte[] md5Bytes=new byte[digest.getDigestSize()];
        digest.doFinal(md5Bytes, 0);
        System.out.println("BC MD5:"+org.bouncycastle.util.encoders.Hex.toHexString(md5Bytes));
    }
    public static void bcMD42(){
        try {
            Security.addProvider(new BouncyCastleProvider());
            MessageDigest md = MessageDigest.getInstance("MD4");
            byte[] md4Bytes = md.digest(src.getBytes());
            System.out.println("BC MD42:" + org.bouncycastle.util.encoders.Hex.toHexString(md4Bytes));
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    public static void ccMD5(){
        System.out.println("CC MD5:"+DigestUtils.md5Hex(src.getBytes()));
    }
    public static void ccMD2(){
        System.out.println("CC MD2:"+DigestUtils.md2Hex(src.getBytes()));
    }
}

2.SHA

package com.security3;

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.bouncycastle.crypto.digests.SHA224Digest;

public class LoveSHA {
    private static String src="imooc security sha";
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        jdkSHA1();
        bcSHA1();
        bcSHA224();
        ccSHA1();
    }
    public static void jdkSHA1(){
        try {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(src.getBytes());
            System.out.println("jdk SHA1:"+Hex.encodeHexString(md.digest()));

        } catch (Exception e) {
            // TODO: handle exception
        }

    }
    public static void bcSHA1(){
        Digest digest=new SHA1Digest();
        digest.update(src.getBytes(), 0, src.getBytes().length);;
        byte[] sha1Bytes=new byte[digest.getDigestSize()];
        digest.doFinal(sha1Bytes, 0);
        System.out.println("bc SHA1:"+org.bouncycastle.util.encoders.Hex.toHexString(sha1Bytes));
    }
    public static void bcSHA224(){
        Digest digest=new SHA224Digest();
        digest.update(src.getBytes(),0, src.getBytes().length);
        byte[] sha224Bytes=new byte[digest.getDigestSize()];
        digest.doFinal(sha224Bytes, 0);
        System.out.println("bc SHA224:"+org.bouncycastle.util.encoders.Hex.toHexString(sha224Bytes));
    }
    public static void ccSHA1(){
        System.out.println("cc SHA1-1:"+DigestUtils.sha1Hex(src.getBytes()));
        System.out.println("cc SHA1-2:"+DigestUtils.sha1Hex(src));
    }
}

3.MAC

package com.security3;

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.Base64;
import org.bouncycastle.util.encoders.Hex;

public class LoveMASC {
    private static String src="imooc security mac";
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        jdkHmacMD5();
        bcHmacMD5();
    }
   public static void jdkHmacMD5(){
       try {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD5");
        SecretKey secretKey=keyGenerator.generateKey();//产生密钥
        byte[] key=secretKey.getEncoded();//获得密钥
        SecretKey restoreSecretKey=new SecretKeySpec(key, "HmacMD5");//还原密钥
        Mac mac=Mac.getInstance(restoreSecretKey.getAlgorithm());//实例化MAC
        mac.init(restoreSecretKey);//初始化MAC
        byte[] hmacMD5Bytes=mac.doFinal(src.getBytes());//执行摘要
        System.out.println("jdk hmacMD5:"+Hex.toHexString(hmacMD5Bytes));
    } catch (Exception e) {
        // TODO: handle exception
    }
   }

   public static void bcHmacMD5(){
       HMac hmac=new HMac(new MD5Digest());
       hmac.init(new KeyParameter(Hex.decode("aaaaaaaaaa")));
       hmac.update(src.getBytes(), 0, src.getBytes().length);
       byte[] hmacMD5Bytes=new byte[hmac.getMacSize()];
       hmac.doFinal(hmacMD5Bytes, 0);
       System.out.println("ba hmacMD5:"+Hex.toHexString(hmacMD5Bytes));
   }
}
4人推荐
随时随地看视频
慕课网APP