码迷,mamicode.com
首页 > 编程语言 > 详细

加解密---消息摘要算法

时间:2019-02-20 14:35:22      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:str   final   md5   ges   provider   exce   message   bsp   static   

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();
        }
    }
}

 

    1.2  SHA(Secure Hash Algorithm)

    1.3  MAC(Message Authentication Code)

加解密---消息摘要算法

标签:str   final   md5   ges   provider   exce   message   bsp   static   

原文地址:https://www.cnblogs.com/anpeiyong/p/10406369.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!