最近在做app后台的服务器,使用到AES加密解密算法,无奈网上的都不符合要求,于是自己借鉴着写了一个AES加密解密工具。
密钥长度问题
默认 Java 中仅支持 128 位密钥,当使用 256 位密钥的时候,会报告密钥长度错误
Invalid AES key length
你需要下载一个支持更长密钥的包。这个包叫做 Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 6,可以从这里下载,下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jce-6-download-429243.html
下载之后,解压后,可以看到其中包含两个包:
local_policy.jar
US_export_policy.jar
看一下你的 JRE 环境,将 JRE 环境中 lib\lib\security 中的同名包替换掉。
废话不多说,上代码
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
/**
* 算法工具
* @author Babylon 214750838@qq.com
* @date 2014-8-15 上午8:41:49
*/
public class AlgorithmUtil {
public final static String ENCODING = "UTF-8";
/**将二进制转换成16进制
* @param buf
* @return
*/
public static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
/**将16进制转换为二进制
* @param hexStr
* @return
*/
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length()/2];
for (int i = 0;i< hexStr.length()/2; i++) {
int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
/**
* 生成密钥
* 自动生成base64 编码后的AES128位密钥
* @throws NoSuchAlgorithmException
* @throws UnsupportedEncodingException
*/
public static String getAESKey() throws Exception {
KeyGenerator kg = KeyGenerator.getInstance("AES");
kg.init(128);//要生成多少位,只需要修改这里即可128, 192或256
SecretKey sk = kg.generateKey();
byte[] b = sk.getEncoded();
return parseByte2HexStr(b);
}
/**
* AES 加密
* @param base64Key base64编码后的 AES key
* @param text 待加密的字符串
* @return 加密后的byte[] 数组
* @throws Exception
*/
public static byte[] getAESEncode(String base64Key, String text) throws Exception{
byte[] key = parseHexStr2Byte(base64Key);
SecretKeySpec sKeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, sKeySpec);
byte[] bjiamihou = cipher.doFinal(text.getBytes(ENCODING));
return bjiamihou;
}
/**
* AES解密
* @param base64Key base64编码后的 AES key
* @param text 待解密的字符串
* @return 解密后的byte[] 数组
* @throws Exception
*/
public static byte[] getAESDecode(String base64Key, byte[] text) throws Exception{
byte[] key = parseHexStr2Byte(base64Key);
SecretKeySpec sKeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, sKeySpec);
byte[] bjiemihou = cipher.doFinal(text);
return bjiemihou;
}
}
测试
import com.util.AlgorithmUtil;
/**
* 算法测试
*
* @author babylon
* 2016年5月16日
*/
public class AlgorithmTest {
public static void main(String[] args) {
try {
String hexKey = new AlgorithmUtil().getAESKey();
System.out.println("16进制秘钥:"+hexKey);
byte[] encoded = AlgorithmUtil.getAESEncode(hexKey, "我要把你嘿嘿嘿");
// 注意,这里的encoded是不能强转成string类型字符串的
byte[] decoded = AlgorithmUtil.getAESDecode(hexKey, encoded);
System.out.println(new String(decoded, "UTF-8"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
博主发现执行算法总有些延迟,还没非对称RSA快,希望有识之士能提供些见解。
RSA算法: http://blog.csdn.net/jack85986370/article/details/51423322