消息摘要算法是一种单向加密算法
主要用于验证数据完整性,也是数字签名的核心算法
消息鉴别:指在接收方将原始信息进行摘要,然后与接收到的摘要信息进行对比
a.MD家族 – MD5(128位摘要信息)
MD算法的作用流程
public class MDFamily { private static final String str = "鲤鱼精"; public static void main(String[] args) { jdkMD("MD5"); ccMD5(); jdkMD("MD2"); jdkMD("MD4"); bcMD4(); } public static void jdkMD(String md){ try { if(md == "MD4"){ //在JDK中动态加入BC MD4 Security.addProvider(new BouncyCastlePQCProvider()); MessageDigest messageDigest = MessageDigest.getInstance(md); byte[] md4 = messageDigest.digest(str.getBytes()); System.out.println("JDK "+md+":"+Hex.encodeHexString(md4)); } //JDK中提供了MD家族中MD2和MD5的实现方式,并且他们的实现方式相同,所以转换为MD2格式只需将MD5改为MD2 MessageDigest messageDigest = MessageDigest.getInstance(md); byte[] md5 = messageDigest.digest(str.getBytes()); //java並沒有將MD5的加密轉換為16進制,輸出的還是一個byte數組,需要手動轉換 System.out.println("JDK "+md+":"+Hex.encodeHexString(md5)); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 同样的,BC同时为我们提供了MD2和MD5的实现,如果需要实现其他两种方式,只需将MD4改为MD2、MD5即可 */ private static void bcMD4(){ Digest digest = new MD4Digest(); //要读取的字符数组,从哪里开始读取,读取的长度 digest.update(str.getBytes(),0,str.getBytes().length); byte[] md4Bytes = new byte[digest.getDigestSize()]; //进行摘要处理后输出的内容,输出偏移量 digest.doFinal(md4Bytes, 0); System.out.println("BC MD4 :" +org.bouncycastle.util.encoders.Hex.toHexString(md4Bytes)); } private static void ccMD5(){ //Commons-code只对JDK进行优化,并没有自己去实现MD家族的算法,所以它并没有MD4加密 System.out.println("ccMD5:"+DigestUtils.md5Hex(str.getBytes())); } }
b.SHA 安全散列算法 安全散列算法 从MD算法演变而来
系列 SHA1(SHA-1 160) SHA-2(224 256 384 512)其中SHA-2
![这里写图片描述](http://img.blog.csdn.net/20170307233218733?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvSmFjWHVhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast) public class SHAArithmeti { private final static String str = "ÀðÓ㾫"; public static void main(String[] args) { MDFamily.jdkMD("SHA"); jdkSHA1(); } private static void jdkSHA1(){ MessageDigest digest; try { digest = MessageDigest.getInstance("SHA"); digest.update(str.getBytes()); System.out.println(Hex.encodeHexString(digest.digest())); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
c.MAC消息认证码算法:Apple的数据算法
HMAC:含有密钥的散列函数算法
public class HMACArithmeti { private final static String str = "鲤鱼精"; public static void main(String[] args) { jdkHMACMD5(); bcHMacMD5(); } //由于HMAC算法带有密钥,所以算法规则和MD5 SHA相比有些变化 private static void jdkHMACMD5(){ try { //初始化KeyGenerator KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD5"); //产生密钥 SecretKey secretKey = keyGenerator.generateKey(); //获得密钥 //byte[] key = secretKey.getEncoded(); //与BC方法同步 byte[] key = org.apache.commons.codec.binary.Hex.decodeHex(new char[]{'1','2','3','4','5','6'}); //还原密钥 SecretKey restoreSecretKey = new SecretKeySpec(key,"HmacMD5"); //实例化MAC Mac mac = Mac.getInstance(restoreSecretKey.getAlgorithm()); //初始化MAC mac.init(restoreSecretKey); //执行摘要 byte[] hMacMD5 = mac.doFinal(str.getBytes()); System.out.println("JDK HMACMD5:"+Hex.toHexString(hMacMD5)); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvalidKeyException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (DecoderException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static void bcHMacMD5(){ HMac hMac = new HMac(new MD5Digest()); hMac.init(new KeyParameter(Hex.decode("123456"))); hMac.update(str.getBytes(),0,str.getBytes().length); byte[] hMacMD5 = new byte[hMac.getMacSize()]; hMac.doFinal(hMacMD5, 0); System.out.println("bc HMACMD5:" + Hex.toHexString(hMacMD5)); } }
d.其他 RipeMD Tiger Whirlpool GOST3411都是由BC提供
JAVA加解密案例源码地址:http://download.csdn.net/detail/jacxuan/9774737
来源:https://www.cnblogs.com/qwop/p/6637287.html