加解密---消息摘要算法(未完待续)

1、概述

    消息摘要算法特征:加密过程不需要秘钥、加密后的数据无法被解密

2、消息摘要算法

    1.1  MD(Message Digest)

        MD家族(128位摘要信息)

        

package com.exiuge.mytest;

import org.apache.commons.codec.binary.Hex;
import java.security.MessageDigest;

public class JdkMD5 {

    private static String src="hello,rose";

    public static void main(String[] args){
        enDeCode(src);
    }

    public static void enDeCode(String src){
        try {
            MessageDigest messageDigest=MessageDigest.getInstance("MD5");
            byte[] desBytes=messageDigest.digest(src.getBytes());
            System.out.println(Hex.encodeHexString(desBytes));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

package com.exiuge.mytest;

import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.MD4Digest;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;
import java.security.MessageDigest;
import java.security.Security;

public class BouncyCastleMD4 {
private static String src="hello,rose";

public static void main(String[] args){
enDeCode(src);
}

public static void enDeCode(String src){
try {
//第一种方式
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(Hex.toHexString(md4Bytes));

//第二种方式
Security.addProvider(new BouncyCastleProvider());
MessageDigest messageDigest=MessageDigest.getInstance("MD4");
byte[] md44Bytes=messageDigest.digest(src.getBytes());
System.out.println(Hex.toHexString(md44Bytes));
}catch (Exception e){
e.printStackTrace();
}
}
}
package com.exiuge.mytest;

import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.MD5Digest;
import org.bouncycastle.util.encoders.Hex;

public class BouncyCastleMD5 {
    private static String src="hello,rose";

    public static void main(String[] args){
        enDeCode(src);
    }

    public static void enDeCode(String src){
        try {
            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(Hex.toHexString(md5Bytes));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
package com.exiuge.mytest;

import org.apache.commons.codec.digest.DigestUtils;

public class CommonsCodecMD5 {

    private static String src="hello,rose";

    public static void main(String[] args){
        enDeCode(src);
    }

    public static void enDeCode(String src){
        System.out.println(DigestUtils.md5Hex(src.getBytes()));
    }
}

      MD应用场景:

        注册

       

        登录

       

    1.2  SHA(Secure Hash Algorithm)

          SHA1、SHA2(SHA224、SHA256、SHA384、SHA512)

          

 

package com.exiuge.mytest;

import org.apache.commons.codec.binary.Hex;
import java.security.MessageDigest;

public class JdkSHA1 {

    private static String src="hello,rose";

    public static void main(String[] args){
        enDeCode(src);
    }

    public static void enDeCode(String src){
        try {
            MessageDigest messageDigest=MessageDigest.getInstance("SHA");
            messageDigest.update(src.getBytes());
            System.out.println(Hex.encodeHexString(messageDigest.digest()));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
package com.exiuge.mytest;

import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle.util.encoders.Hex;

public class BouncyCastleSHA1 {
    private static String src="hello,rose";

    public static void main(String[] args){
        enDeCode(src);
    }

    public static void enDeCode(String src){
        try {
            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(Hex.toHexString(sha1Bytes));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
package com.exiuge.mytest;

import org.apache.commons.codec.digest.DigestUtils;

public class CommonsCastleSHA1 {
    private static String src="hello,rose";

    public static void main(String[] args){
        enDeCode(src);
    }

    public static void enDeCode(String src){
        try {
            System.out.println(DigestUtils.sha1Hex(src));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

      SHA应用场景:

      

    1.3  MAC(Message Authentication Code)

          融合MD、SHA;    

posted on 2019-02-20 14:22  anpeiyong  阅读(499)  评论(0编辑  收藏  举报

导航