一、概述
MAC算法结合了MD5和SHA算法的优势,并加入密钥的支持,是一种更为安全的消息摘要算法。
MAC(Message Authentication Code,消息认证码算法)是含有密钥的散列函数算法,兼容了MD和SHA算法的特性,并在此基础上加入了密钥。日次,我们也常把MAC称为HMAC(keyed-Hash Message Authentication Code)。
MAC算法主要集合了MD和SHA两大系列消息摘要算法。MD系列的算法有HmacMD2、HmacMD4、HmacMD5三种算法;SHA系列的算法有HmacSHA1、HmacSHA224、HmacSHA256、HmacSHA384.HmacSHA512五种算法。
经过MAC算法得到的摘要值也可以使用十六进制编码表示,其摘要值长度与参与实现的摘要值长度相同。例如,HmacSHA1算法得到的摘要长度就是SHA1算法得到的摘要长度,都是160位二进制码,换算成十六进制编码为40位。
二、实现和应用
1、Sun的实现和应用
在java6中,MAC系列算法需要通过Mac类提供支持。java6中仅仅提供HmacMD5、HmacSHA1、HmacSHA256、HmacSHA384和HmacSHA512四种算法。
Mac算法是带有密钥的消息摘要算法,所以实现起来要分为两步:
1)、构建密钥
2)、执行消息摘要
- package com.tao.test;
- import java.security.NoSuchAlgorithmException;
- import javax.crypto.KeyGenerator;
- import javax.crypto.Mac;
- import javax.crypto.SecretKey;
- import javax.crypto.spec.SecretKeySpec;
- import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
- /**
- * MAC算法工具类
- * 对于HmacMD5、HmacSHA1、HmacSHA256、HmacSHA384、HmacSHA512应用的步骤都是一模一样的。具体看下面的代码
- */
- class MACCoder {
- /**
- * 产生HmacMD5摘要算法的密钥
- */
- public static byte[] initHmacMD5Key() throws NoSuchAlgorithmException {
- // 初始化HmacMD5摘要算法的密钥产生器
- KeyGenerator generator = KeyGenerator.getInstance("HmacMD5");
- // 产生密钥
- SecretKey secretKey = generator.generateKey();
- // 获得密钥
- byte[] key = secretKey.getEncoded();
- return key;
- }
- /**
- * HmacMd5摘要算法
- * 对于给定生成的不同密钥,得到的摘要消息会不同,所以在实际应用中,要保存我们的密钥
- */
- public static String encodeHmacMD5(byte[] data, byte[] key) throws Exception {
- // 还原密钥
- SecretKey secretKey = new SecretKeySpec(key, "HmacMD5");
- // 实例化Mac
- Mac mac = Mac.getInstance(secretKey.getAlgorithm());
- //初始化mac
- mac.init(secretKey);
- //执行消息摘要
- byte[] digest = mac.doFinal(data);
- return new HexBinaryAdapter().marshal(digest);//转为十六进制的字符串
- }
- /**
- * 产生HmacSHA1摘要算法的密钥
- */
- public static byte[] initHmacSHAKey() throws NoSuchAlgorithmException {
- // 初始化HmacMD5摘要算法的密钥产生器
- KeyGenerator generator = KeyGenerator.getInstance("HmacSHA1");
- // 产生密钥
- SecretKey secretKey = generator.generateKey();
- // 获得密钥
- byte[] key = secretKey.getEncoded();
- return key;
- }
- /**
- * HmacSHA1摘要算法
- * 对于给定生成的不同密钥,得到的摘要消息会不同,所以在实际应用中,要保存我们的密钥
- */
- public static String encodeHmacSHA(byte[] data, byte[] key) throws Exception {
- // 还原密钥
- SecretKey secretKey = new SecretKeySpec(key, "HmacSHA1");
- // 实例化Mac
- Mac mac = Mac.getInstance(secretKey.getAlgorithm());
- //初始化mac
- mac.init(secretKey);
- //执行消息摘要
- byte[] digest = mac.doFinal(data);
- return new HexBinaryAdapter().marshal(digest);//转为十六进制的字符串
- }
- /**
- * 产生HmacSHA256摘要算法的密钥
- */
- public static byte[] initHmacSHA256Key() throws NoSuchAlgorithmException {
- // 初始化HmacMD5摘要算法的密钥产生器
- KeyGenerator generator = KeyGenerator.getInstance("HmacSHA256");
- // 产生密钥
- SecretKey secretKey = generator.generateKey();
- // 获得密钥
- byte[] key = secretKey.getEncoded();
- return key;
- }
- /**
- * HmacSHA1摘要算法
- * 对于给定生成的不同密钥,得到的摘要消息会不同,所以在实际应用中,要保存我们的密钥
- */
- public static String encodeHmacSHA256(byte[] data, byte[] key) throws Exception {
- // 还原密钥
- SecretKey secretKey = new SecretKeySpec(key, "HmacSHA256");
- // 实例化Mac
- Mac mac = Mac.getInstance(secretKey.getAlgorithm());
- //初始化mac
- mac.init(secretKey);
- //执行消息摘要
- byte[] digest = mac.doFinal(data);
- return new HexBinaryAdapter().marshal(digest);//转为十六进制的字符串
- }
- /**
- * 产生HmacSHA256摘要算法的密钥
- */
- public static byte[] initHmacSHA384Key() throws NoSuchAlgorithmException {
- // 初始化HmacMD5摘要算法的密钥产生器
- KeyGenerator generator = KeyGenerator.getInstance("HmacSHA384");
- // 产生密钥
- SecretKey secretKey = generator.generateKey();
- // 获得密钥
- byte[] key = secretKey.getEncoded();
- return key;
- }
- /**
- * HmacSHA1摘要算法
- * 对于给定生成的不同密钥,得到的摘要消息会不同,所以在实际应用中,要保存我们的密钥
- */
- public static String encodeHmacSHA384(byte[] data, byte[] key) throws Exception {
- // 还原密钥
- SecretKey secretKey = new SecretKeySpec(key, "HmacSHA384");
- // 实例化Mac
- Mac mac = Mac.getInstance(secretKey.getAlgorithm());
- //初始化mac
- mac.init(secretKey);
- //执行消息摘要
- byte[] digest = mac.doFinal(data);
- return new HexBinaryAdapter().marshal(digest);//转为十六进制的字符串
- }
- /**
- * 产生HmacSHA256摘要算法的密钥
- */
- public static byte[] initHmacSHA512Key() throws NoSuchAlgorithmException {
- // 初始化HmacMD5摘要算法的密钥产生器
- KeyGenerator generator = KeyGenerator.getInstance("HmacSHA512");
- // 产生密钥
- SecretKey secretKey = generator.generateKey();
- // 获得密钥
- byte[] key = secretKey.getEncoded();
- return key;
- }
- /**
- * HmacSHA1摘要算法
- * 对于给定生成的不同密钥,得到的摘要消息会不同,所以在实际应用中,要保存我们的密钥
- */
- public static String encodeHmacSHA512(byte[] data, byte[] key) throws Exception {
- // 还原密钥
- SecretKey secretKey = new SecretKeySpec(key, "HmacSHA512");
- // 实例化Mac
- Mac mac = Mac.getInstance(secretKey.getAlgorithm());
- //初始化mac
- mac.init(secretKey);
- //执行消息摘要
- byte[] digest = mac.doFinal(data);
- return new HexBinaryAdapter().marshal(digest);//转为十六进制的字符串
- }
- }
- public class MACTest {
- public static void main(String[] args) throws Exception {
- String testString = "asdasd";
- byte[] keyHmacMD5=MACCoder.initHmacMD5Key();
- System.out.println(MACCoder.encodeHmacMD5(testString.getBytes(),keyHmacMD5));
- byte[] keyHmacSHA1=MACCoder.initHmacSHAKey();
- System.out.println(MACCoder.encodeHmacSHA(testString.getBytes(),keyHmacSHA1));
- byte[] keyHmacSHA256=MACCoder.initHmacSHA256Key();
- System.out.println(MACCoder.encodeHmacSHA256(testString.getBytes(),keyHmacSHA256));
- byte[] keyHmacSHA384=MACCoder.initHmacSHA384Key();
- System.out.println(MACCoder.encodeHmacSHA384(testString.getBytes(),keyHmacSHA384));
- byte[] keyHmacSHA512=MACCoder.initHmacSHA512Key();
- System.out.println(MACCoder.encodeHmacSHA512(testString.getBytes(),keyHmacSHA512));
- }
- }
2、BouncyCastle的实现和应用
第三方加密组件包Bouncy Castle作为补充,提供了HmacMD2.HmacMD4、HmacSHA224三种算法的支持。
- package Test;
- import java.security.NoSuchAlgorithmException;
- import java.security.Security;
- import javax.crypto.KeyGenerator;
- import javax.crypto.Mac;
- import javax.crypto.SecretKey;
- import javax.crypto.spec.SecretKeySpec;
- import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
- import org.bouncycastle.jce.provider.BouncyCastleProvider;
- /**
- * MAC算法工具类
- * 对于HmacMD5、HmacSHA1、HmacSHA256、HmacSHA384、HmacSHA512应用的步骤都是一模一样的。具体看下面的代码
- */
- class MACCoder {
- /**
- * 产生HmacMD2摘要算法的密钥
- */
- public static byte[] initHmacMD2Key() throws NoSuchAlgorithmException {
- // 添加BouncyCastle的支持
- Security.addProvider(new BouncyCastleProvider());
- // 初始化HmacMD5摘要算法的密钥产生器
- KeyGenerator generator = KeyGenerator.getInstance("HmacMD2");
- // 产生密钥
- SecretKey secretKey = generator.generateKey();
- // 获得密钥
- byte[] key = secretKey.getEncoded();
- return key;
- }
- /**
- * HmacMd2摘要算法 对于给定生成的不同密钥,得到的摘要消息会不同,所以在实际应用中,要保存我们的密钥
- */
- public static String encodeHmacMD2(byte[] data, byte[] key) throws Exception {
- // 添加BouncyCastle的支持
- Security.addProvider(new BouncyCastleProvider());
- // 还原密钥
- SecretKey secretKey = new SecretKeySpec(key, "HmacMD2");
- // 实例化Mac
- Mac mac = Mac.getInstance(secretKey.getAlgorithm());
- // 初始化mac
- mac.init(secretKey);
- // 执行消息摘要
- byte[] digest = mac.doFinal(data);
- return new HexBinaryAdapter().marshal(digest);// 转为十六进制的字符串
- }
- /**
- * 产生HmacMD4摘要算法的密钥
- */
- public static byte[] initHmacMD4Key() throws NoSuchAlgorithmException {
- // 添加BouncyCastle的支持
- Security.addProvider(new BouncyCastleProvider());
- // 添加BouncyCastle的支持
- Security.addProvider(new BouncyCastleProvider());
- // 初始化HmacMD5摘要算法的密钥产生器
- KeyGenerator generator = KeyGenerator.getInstance("HmacMD4");
- // 产生密钥
- SecretKey secretKey = generator.generateKey();
- // 获得密钥
- byte[] key = secretKey.getEncoded();
- return key;
- }
- /**
- * HmacMD4摘要算法 对于给定生成的不同密钥,得到的摘要消息会不同,所以在实际应用中,要保存我们的密钥
- */
- public static String encodeHmacMD4(byte[] data, byte[] key) throws Exception {
- // 添加BouncyCastle的支持
- Security.addProvider(new BouncyCastleProvider());
- // 还原密钥
- SecretKey secretKey = new SecretKeySpec(key, "HmacMD4");
- // 实例化Mac
- Mac mac = Mac.getInstance(secretKey.getAlgorithm());
- // 初始化mac
- mac.init(secretKey);
- // 执行消息摘要
- byte[] digest = mac.doFinal(data);
- return new HexBinaryAdapter().marshal(digest);// 转为十六进制的字符串
- }
- /**
- * 产生HmacSHA224摘要算法的密钥
- */
- public static byte[] initHmacSHA224Key() throws NoSuchAlgorithmException {
- // 添加BouncyCastle的支持
- Security.addProvider(new BouncyCastleProvider());
- // 添加BouncyCastle的支持
- Security.addProvider(new BouncyCastleProvider());
- // 初始化HmacMD5摘要算法的密钥产生器
- KeyGenerator generator = KeyGenerator.getInstance("HmacSHA224");
- // 产生密钥
- SecretKey secretKey = generator.generateKey();
- // 获得密钥
- byte[] key = secretKey.getEncoded();
- return key;
- }
- /**
- * HmacSHA224摘要算法 对于给定生成的不同密钥,得到的摘要消息会不同,所以在实际应用中,要保存我们的密钥
- */
- public static String encodeHmacSHA224(byte[] data, byte[] key) throws Exception {
- // 添加BouncyCastle的支持
- Security.addProvider(new BouncyCastleProvider());
- // 还原密钥
- SecretKey secretKey = new SecretKeySpec(key, "HmacSHA224");
- // 实例化Mac
- Mac mac = Mac.getInstance(secretKey.getAlgorithm());
- // 初始化mac
- mac.init(secretKey);
- // 执行消息摘要
- byte[] digest = mac.doFinal(data);
- return new HexBinaryAdapter().marshal(digest);// 转为十六进制的字符串
- }
- }
- public class MACTest {
- public static void main(String[] args) throws Exception {
- String testString = "asdasd";
- byte[] keyHmacMD2 = MACCoder.initHmacMD2Key();
- System.out.println(MACCoder.encodeHmacMD2(testString.getBytes(), keyHmacMD2));
- byte[] keyHmacMD4 = MACCoder.initHmacMD4Key();
- System.out.println(MACCoder.encodeHmacMD4(testString.getBytes(), keyHmacMD4));
- byte[] keyHmacSHA224 = MACCoder.initHmacSHA224Key();
- System.out.println(MACCoder.encodeHmacSHA224(testString.getBytes(), keyHmacSHA224));
- }
- }