今天偶然的学习了一下几种关于消息摘要算法的知识。个人觉得很好。应着老话“好记性不如烂笔头”,我就码了几行代码咯。
算法嘛,没什么好说的了。毕竟是设计者智慧与汗水的结晶,也是时代进步的推动力。我们能做的就是将这种算法运用得当,造福全人类,就行了!
//格外需要注意的是采用CC(Commons codec)方式生成消息摘要时,一定要记得导入相关的jar包哦!
MD方式
package MD;
/**
* MD:MessageDigest----消息摘要算法
*/
import java.security.MessageDigest;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.digest.Md5Crypt;
import org.junit.Test;
/**
* 简单的使用MessageDigest实现消息摘要的小案例
* 收获:
* 使用摘要算法获得字节数组无法直接输出,需要转成相应的十六进制才能获得结果
* @author Summer
*
*/
public class Demo {
private static String targetString = "I am Summer!";
/**
* 测试MD5算法加密效果,转成了十六进制
* JDK实现
* @throws Exception
*/
@Test
public void test1() throws Exception {
MessageDigest md = MessageDigest.getInstance("MD5");
byte [] bytes = md.digest(targetString.getBytes());
String result = Hex.encodeHexString(bytes);
System.out.println("MD5加密后的串是:"+result);
//5b704caf20c179cdf61d7121e59dcd76
}
/**
* 测试MD2算法加密效果,转成了十六进制
* JDK实现
* @throws Exception
*/
@Test
public void test2() throws Exception {
MessageDigest md = MessageDigest.getInstance("MD2");
byte [] bytes = md.digest(targetString.getBytes());
String result = Hex.encodeHexString(bytes);
System.out.println("MD5加密后的串是:"+result);
//387992acc0a756abc7026d635383b0a7
}
/**
* 测试MD2算法加密效果,不转成十六进制
* JDK实现
* @throws Exception
*/
@Test
public void test3() throws Exception {
MessageDigest md = MessageDigest.getInstance("MD2");
byte [] bytes = md.digest(targetString.getBytes());
System.out.println(bytes);
}
/**
* 测试MD5算法加密效果,不转成十六进制
* JDK实现
* @throws Exception
*/
@Test
public void test4() throws Exception {
MessageDigest md = MessageDigest.getInstance("MD5");
byte [] bytes = md.digest(targetString.getBytes());
System.out.println(bytes.toString());
}
/**
* 使用commons codec的方式对目标字符串进行加密MD2,并使用十六进制进行输出
* @throws Exception
*/
@Test
public void test5() throws Exception {
MessageDigest md = DigestUtils.getMd2Digest();
byte[] bytes = md.digest(targetString.getBytes());
String result = Hex.encodeHexString(bytes);
System.out.println("Commons codec 方式加密MD2:"+ result);
//387992acc0a756abc7026d635383b0a7
}
/**
* 使用commons codec的方式对目标字符串进行加密MD5,并使用十六进制进行输出
* 对比方法发现,使用CC这个开源方式,可以大大的简化操作。但其底层仍是JDK实现的,cc知识做了一些简化,仅此而已
* @throws Exception
*/
@Test
public void test6() throws Exception {
MessageDigest md = DigestUtils.getMd5Digest();
byte[] bytes = md.digest(targetString.getBytes());
String result = Hex.encodeHexString(bytes);
System.out.println("Commons codec 方式加密MD5:"+ result);
//5b704caf20c179cdf61d7121e59dcd76
System.out.println(DigestUtils.md5Hex(targetString));
//5b704caf20c179cdf61d7121e59dcd76
}
}
SHA方式
package SHA;
/**
* SHA: Security Hash Algorithm -----安全哈希算法
*/
import java.security.MessageDigest;
import org.apache.commons.codec.binary.Hex;
import org.junit.Test;
public class Demo {
private static String targetString = "I am Summer!";
/**
* 使用JDK方式以SHA1的方式实现消息摘要算法
* @throws Exception
*/
@Test
public void testSHA1() throws Exception {
/*方式一:
* //算法名称对于SHA1方式: 可以是SHA也可以是SHA1
MessageDigest md = MessageDigest.getInstance("SHA");
byte[] bytes = md.digest(targetString.getBytes());
String result = Hex.encodeHexString(bytes);
System.out.println("JDK 方式实现SHA1消息摘要的加密:\t"+ result);
//a621ca634410c2521ef560736a6a9da048f42961
*/
/*
* 方式二:
*/
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(targetString.getBytes());
String result = Hex.encodeHexString(md.digest());
System.out.println(result);
}
/**
* 使用JDK方式以SHA-224的方式实现消息摘要算法
* @throws Exception
*/
@Test
public void testSHA224() throws Exception {
MessageDigest md = MessageDigest.getInstance("SHA-224");
md.update(targetString.getBytes());
String result = Hex.encodeHexString(md.digest());
System.out.println(result);
//6fb07533ef05a1f1b6a0fbd25f08ed3cbcf17807a507c0224756f06d
}
/**
* 使用JDK方式以SHA-384的方式实现消息摘要算法
* @throws Exception
*/
@Test
public void testSHA384() throws Exception {
MessageDigest md = MessageDigest.getInstance("SHA-384");
md.update(targetString.getBytes());
String result = Hex.encodeHexString(md.digest());
System.out.println(result);
//c640d1c73a8e2078b290f284fce59da103ecfc1c3c9442cc90ebd5f08900d8e6f19551da7b2f213d96dd055bd2759698
}
/**
* 使用JDK方式以SHA-256的方式实现消息摘要算法
* @throws Exception
*/
@Test
public void testSHA256() throws Exception {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(targetString.getBytes());
String result = Hex.encodeHexString(md.digest());
System.out.println(result);
//8305809696717b31c6765b7ba89cfd67c17ef62c79a700ae4e305fee5ebdf457
}
/**
* 使用JDK方式以SHA-512的方式实现消息摘要算法
* @throws Exception
*/
@Test
public void testSHA512() throws Exception {
MessageDigest md = MessageDigest.getInstance("SHA-512");
md.update(targetString.getBytes());
String result = Hex.encodeHexString(md.digest());
System.out.println(result);
//520abdfdc5e0ac43c795bee9da1cac3fa2f55b8e89f1d33f9a4b83367f4b74cd5d42bfa2c6d4f68c362e64d44ea664244c03c7fd2b7bc167a489fe7129c91156
}
}
MAC方式
package MAC;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Hex;
import org.junit.Test;
/**
* MAC: Message Authentication Code ------消息验证码
*/
public class Demo {
private static String targetString = "I am Summer!";
/**
* 使用MAC算法以MD5方式加密
* @throws Exception
*/
@Test
public void hmacMD5() throws Exception {
//初始化KeyGeerator
KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD5");
//产生密钥
SecretKey secretKey = keyGenerator.generateKey();
//获得密钥
byte[] key = secretKey.getEncoded();
key = Hex.decodeHex(new char[]{'S','u','m','m','e','r','!'});
//还原密钥
SecretKey restoreSecretKey = new SecretKeySpec(key, "HmacMD5");
//实例化Mac
Mac mac = Mac.getInstance(restoreSecretKey.getAlgorithm());
//初始化Mac
mac.init(restoreSecretKey);
byte[] hmacMD5Bytes = mac.doFinal(targetString.getBytes());
//转成十六进制并进行输出
String result = Hex.encodeHexString(hmacMD5Bytes);
System.out.println(result);
//8371828a9e53f04977b4d2ceb73ff506
}
}
小总结
对于MAC方式,其原理其实和前两个差不多,只不过是加入了key加密的权限。详见代码哦!
时间: 2024-10-07 13:13:44