时间:2017年4月10日星期一
说明:本文部分内容均来自慕课网。@慕课网:http://www.imooc.com
教学示例源码:https://github.com/zccodere/s...
个人学习源码:https://github.com/zccodere/s...

第一章:概述

1-1 Java实现消息摘要算法加密

消息摘要算法

MD(Message Digest)
SHA(Secure Hash Algorithm)
MAC(Message Authentication Code)
验证数据完整性
数字签名核心算法

第二章:消息摘要算法MD

2-1 MD算法概述

消息摘要算法:MD

MD5
MD家族(128位摘要信息)
--MD2、MD4
特点

算法实现:

clipboard.png

2-2 MD算法实现

代码演示:

package com.myimooc.security.md;

import org.apache.commons.codec.binary.Hex;
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 java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Security;

/**
 * MD消息摘要加密演示
 * Created by ZC on 2017/4/10.
 */
public class DemoMD {

    /** 待加密字符串 */
    private static String src="imooc security md";

    public static void main(String[] args){
        jdkMD5();
        jdkMD2();
        bcMD4();
        bcMD5();
        ccMD5();
        ccMD2();
    }

    /**
     * 通过 jdk 实现MD5加密
     */
    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 (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    /**
     * 通过 jdk 实现MD2加密
     */
    public static void jdkMD2(){
        try {
            MessageDigest md = MessageDigest.getInstance("MD2");
            byte[] md2Bytes = md.digest(src.getBytes());
            System.out.println("JDK MD2:"+Hex.encodeHexString(md2Bytes));

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    /**
     * 通过 bouncy castle 实现MD5加密
     */
    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));
    }

    /**
     * 通过 bouncy castle 实现MD4加密
     */
    public static void bcMD4(){
        try {
            Security.addProvider(new BouncyCastleProvider());
            MessageDigest md = MessageDigest.getInstance("MD4");
            byte[] md5Bytes = md.digest(src.getBytes());
            System.out.println("BC MD4:"+org.bouncycastle.util.encoders.Hex.toHexString(md5Bytes));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    /**
     * 通过 commons codec 实现MD5加密
     */
    public static void ccMD5(){
        System.out.println("CC MD5:"+DigestUtils.md5Hex(src.getBytes()));
    }

    /**
     * 通过 commons codec 实现MD5加密
     */
    public static void ccMD2(){
        System.out.println("CC MD2:"+DigestUtils.md2Hex(src.getBytes()));
    }

}

2-3 MD算法应用

流程演示:

clipboard.png

第三章:消息摘要算法SHA

3-1 SHA算法概述

消息摘要算法:SHA

安全散列算法
固定长度摘要信息
SHA-1、SHA-2(SHA-224、SHA-256、SHA-384、SHA-512)

算法实现:

clipboard.png

3-2 SHA算法实现

代码演示:

package com.myimooc.security.sha;

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;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

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

/**
 * SHA消息摘要加密演示
 * Created by ZC on 2017/4/10.
 */
public class DemoSHA {

    /** 待加密字符串 */
    private static String src="imooc security sha";

    public static void main(String[] args){
        jdkSHA1();
        bcSHA1();
        bcSHA224();
        bcSHA224_2();
        ccSHA1();
    }

    /**
     * 通过 JDK 实现SHA-1 加密
     */
    public static void jdkSHA1(){
        try {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(src.getBytes());
            System.out.println("jdk sha-1:"+ Hex.encodeHexString(md.digest()));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    /**
     * 通过 bouncy castle 实现SHA-1 加密
     */
    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 sha-1:"+ org.bouncycastle.util.encoders.Hex.toHexString(sha1Bytes));
    }

    /**
     * 通过 bouncy castle 实现SHA-224 加密
     */
    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 sha-224:"+ org.bouncycastle.util.encoders.Hex.toHexString(sha224Bytes));
    }

    /** 通过 provider 实现SHA224加密 */
    public static void bcSHA224_2(){
        Security.addProvider(new BouncyCastleProvider());
        try {
            MessageDigest md = MessageDigest.getInstance("SHA224");
            byte[] sha224Bytes = md.digest(src.getBytes());
            System.out.println("sha-224:"+ org.bouncycastle.util.encoders.Hex.toHexString(sha224Bytes));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    /** 通过 commons.codec 实现SHA-1加密 */
    public static void ccSHA1(){
        System.out.println("cc sha1-1:"+DigestUtils.sha1Hex(src.getBytes()));
        System.out.println("cc sha1-2:"+DigestUtils.sha1Hex(src));
    }

}

3-3 SHA算法应用

流程演示:

clipboard.png

应用例子:

clipboard.png

第四章:消息摘要算法MAC

4-1 消息摘要算法MAC实现与应用

MAC(Message Authentication Code)
HMAC(keyed-Hash Message Authentication Code)

    --含有密钥的散列函数算法
融合MD、SHA
    --MD系列:HmacMD2、HmacMD4、HmacMD5
应用如SecureCRT

算法实现:

clipboard.png

代码演示:

package com.myimooc.security.mac;

import org.apache.commons.codec.binary.Hex;
import org.bouncycastle.crypto.digests.MD5Digest;
import org.bouncycastle.crypto.macs.HMac;
import org.bouncycastle.crypto.params.KeyParameter;

import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;

/**
 * MAC消息摘要加密演示
 * Created by ZC on 2017/4/11.
 */
public class DemoMac {

    /** 待加密字符串 */
    private static String src="imooc security mac";

    public static void main(String[] args){
        jdkHmacDM5();
        bcHmacMD5();
    }

    /** jdk实现hmac MD5摘要算法 */
    public static void jdkHmacDM5(){
        try {
            // 初始化 KeyGenerator
            KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD5");
            // 产生密钥
            SecretKey secretKey = keyGenerator.generateKey();
            // 获得密钥
            //byte[] key = secretKey.getEncoded();

            // 自定义密钥
            byte[] key = Hex.decodeHex(new char[]{'a','a','a','a','a','a','a','a','a','a'});

            // 还原密钥
            SecretKey restoreSecretKey = new SecretKeySpec(key,"HmacMD5");
            // 实例化 MAC
            Mac mac = Mac.getInstance(restoreSecretKey.getAlgorithm());
            // 初始化 MAC
            mac.init(restoreSecretKey);
            // 执行摘要
            byte[] hmacMD5Bytes = mac.doFinal(src.getBytes());
            System.out.println("jdk hmacMD5:"+ Hex.encodeHexString(hmacMD5Bytes));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /** bouncycastle.crypto实现hmacMD5加密 */
    public static void bcHmacMD5(){
        HMac hmac = new HMac(new MD5Digest());
        hmac.init(new KeyParameter(org.bouncycastle.util.encoders.Hex.decode("aaaaaaaaaa")));
        hmac.update(src.getBytes(),0,src.getBytes().length);
        // 执行摘要
        byte[] hmacMD5Bytes = new byte[hmac.getMacSize()];
        hmac.doFinal(hmacMD5Bytes,0);

        System.out.println("bc hmacMD5:"+ org.bouncycastle.util.encoders.Hex.toHexString(hmacMD5Bytes));
    }

}

流程演示:

clipboard.png

消息摘要算法:其它

RipeMD
Tiger
Whirlpool
GOST3411
Bouncy Castle实现

妙手空空
1.3k 声望368 粉丝

博观而约取,厚积而薄发