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)
原文地址:https://www.cnblogs.com/anpeiyong/p/10406369.html
时间: 2024-10-31 07:48:12