您的位置:首页 > 编程语言 > Java开发

Java基础加密之HMAC加密

2016-07-01 14:41 423 查看
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位。




package com.soap.util;

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;

/**
* Hash Message Authentication Code,散列消息鉴别码 基于密钥的Hash算法的认证协议。
*
* @author Roger
*/
public class HMACUtil {

/**
* 构建HmacMD5密钥
*
* @return
* @throws Exception
*/
public static byte[] initHmacMD5Key() throws Exception {
// 初始化HmacMD5摘要算法的密钥产生器
KeyGenerator generator = KeyGenerator.getInstance("HmacMD5");
// 产生密钥
SecretKey secretKey = generator.generateKey();
// 获得密钥
byte[] key = secretKey.getEncoded();
return key;
}

/**
* 生成32位HmacMD5加密数据
* @param data
* @param key
* @return
* @throws Exception
*/
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密钥
* @return
* @throws NoSuchAlgorithmException
*/
public static byte[] initHmacSHAKey() throws NoSuchAlgorithmException {
// 初始化HmacMD5摘要算法的密钥产生器
KeyGenerator generator = KeyGenerator.getInstance("HmacSHA1");
// 产生密钥
SecretKey secretKey = generator.generateKey();
// 获得密钥
byte[] key = secretKey.getEncoded();
return key;
}

/**
* 生成40位HmacSHA1加密数据
* @param data
* @param key
* @return
* @throws Exception
*/
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密钥
* @return
* @throws NoSuchAlgorithmException
*/
public static byte[] initHmacSHA256Key() throws NoSuchAlgorithmException {
// 初始化HmacMD5摘要算法的密钥产生器
KeyGenerator generator = KeyGenerator.getInstance("HmacSHA256");
// 产生密钥
SecretKey secretKey = generator.generateKey();
// 获得密钥
byte[] key = secretKey.getEncoded();
return key;
}

/**
* 生成64位HmacSHA256加密数据
* @param data
* @param key
* @return
* @throws Exception
*/
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);// 转为十六进制的字符串
}

/**
* 构建HmacSHA384密钥
* @return
* @throws NoSuchAlgorithmException
*/
public static byte[] initHmacSHA384Key() throws NoSuchAlgorithmException {
// 初始化HmacMD5摘要算法的密钥产生器
KeyGenerator generator = KeyGenerator.getInstance("HmacSHA384");
// 产生密钥
SecretKey secretKey = generator.generateKey();
// 获得密钥
byte[] key = secretKey.getEncoded();
return key;
}

/**
* 生成96位HmacSHA384加密数据
* @param data
* @param key
* @return
* @throws Exception
*/
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);// 转为十六进制的字符串
}

/**
* 构建HmacSHA512密钥
* @return
* @throws NoSuchAlgorithmException
*/
public static byte[] initHmacSHA512Key() throws NoSuchAlgorithmException {
// 初始化HmacMD5摘要算法的密钥产生器
KeyGenerator generator = KeyGenerator.getInstance("HmacSHA512");
// 产生密钥
SecretKey secretKey = generator.generateKey();
// 获得密钥
byte[] key = secretKey.getEncoded();
return key;
}

/**
* 生成128位HmacSHA512加密信息
* @param data
* @param key
* @return
* @throws Exception
*/
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 static void main(String[] args) throws Exception {
String testString = "asdasd";

byte[] keyHmacMD5 = initHmacMD5Key();
System.out.println(encodeHmacMD5(testString.getBytes(), keyHmacMD5));
System.out.println(encodeHmacMD5(testString.getBytes(), keyHmacMD5)
.length());

byte[] keyHmacSHA1 = initHmacSHAKey();
System.out.println(encodeHmacSHA(testString.getBytes(), keyHmacSHA1));
System.out.println(encodeHmacSHA(testString.getBytes(), keyHmacSHA1)
.length());

byte[] keyHmacSHA256 = initHmacSHA256Key();
System.out.println(encodeHmacSHA256(testString.getBytes(),
keyHmacSHA256));
System.out.println(encodeHmacSHA256(testString.getBytes(),
keyHmacSHA256).length());

byte[] keyHmacSHA384 = initHmacSHA384Key();
System.out.println(encodeHmacSHA384(testString.getBytes(),
keyHmacSHA384));
System.out.println(encodeHmacSHA384(testString.getBytes(),
keyHmacSHA384).length());

byte[] keyHmacSHA512 = initHmacSHA512Key();
System.out.println(encodeHmacSHA512(testString.getBytes(),
keyHmacSHA512));
System.out.println(encodeHmacSHA512(testString.getBytes(),
keyHmacSHA512).length());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java HMAC SHA MD5