加解密---消息摘要算法
Posted anpeiyong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了加解密---消息摘要算法相关的知识,希望对你有一定的参考价值。
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)
以上是关于加解密---消息摘要算法的主要内容,如果未能解决你的问题,请参考以下文章