加密工具类,直接copy 使用:
1. jdk7 需要更换 C:\Program Files\Java\jdk1.7.0_79\jre\lib\security 目录下的 local_policy.jar 和 US_export_policy.jar
linux 上可以使用命令 which java 查看 jdk目录 如:/app/jdk/jdk1.7.0_75/jre/lib/security
local_policy.jar 和 US_export_policy.jar 免积分下载地址://download.csdn.net/download/javaee_kekai/12037687
import java.security.Provider;
import java.security.SecureRandom;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class AESUtils {
//默认向量常量
public static final String IV = "1234567890123456";
//秘钥必须要到一点长度
private static String pkey="1111111111@11111111@111111@11111";
/**
* 使用PKCS7Padding填充必须添加一个支持PKCS7Padding的Provider
* 类加载的时候就判断是否已经有支持256位的Provider,如果没有则添加进去
*/
static {
if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
Security.addProvider(new BouncyCastleProvider());
}
}
/**
* 加密 128位
*
* @param content 需要加密的原内容
* @param pkey 密匙
* @return
*/
public static byte[] aesEncrypt(String content, String pkey) {
try {
//SecretKey secretKey = generateKey(pkey);
//byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec skey = new SecretKeySpec(pkey.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");// "算法/加密/填充"
IvParameterSpec iv = new IvParameterSpec(IV.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, skey, iv);//初始化加密器
byte[] encrypted = cipher.doFinal(content.getBytes("UTF-8"));
return encrypted; // 加密
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 获得密钥
*
* @param secretKey
* @return
* @throws Exception
*/
private static SecretKey generateKey(String secretKey) throws Exception {
//防止linux下 随机生成key
Provider p = Security.getProvider("SUN");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG", p);
secureRandom.setSeed(secretKey.getBytes());
KeyGenerator kg = KeyGenerator.getInstance("AES");
kg.init(secureRandom);
// 生成密钥
return kg.generateKey();
}
/**
* @param content 加密前原内容
* @param pkey 长度为16个字符
* @return base64EncodeStr aes加密完成后内容
* @throws
* @Title: aesEncryptStr
* @Description: aes对称加密
*/
public static String aesEncryptStr(String content) {
byte[] aesEncrypt = aesEncrypt(content, pkey);
String base64EncodeStr = Base64.encodeBase64String(aesEncrypt);
return base64EncodeStr;
}
/**
* @param content base64处理过的字符串
* @param pkey 密匙
* @return String 返回类型
* @throws Exception
* @throws
* @Title: aesDecodeStr
* @Description: 解密 失败将返回NULL
*/
public static String aesDecodeStr(String content) throws Exception {
try {
byte[] base64DecodeStr = Base64.decodeBase64(content);
byte[] aesDecode = aesDecode(base64DecodeStr, pkey);
if (aesDecode == null || aesDecode.length == 0) {
return content;
}
String result;
result = new String(aesDecode, "UTF-8");
return result;
} catch (Exception e) {
e.printStackTrace();
return content;
}
}
/**
* 解密
* @param content 解密前的byte数组
* @param pkey 密匙
* @return result 解密后的byte数组
* @throws Exception
*/
public static byte[] aesDecode(byte[] content, String pkey) throws Exception {
SecretKeySpec skey = new SecretKeySpec(pkey.getBytes(), "AES");
IvParameterSpec iv = new IvParameterSpec(IV.getBytes("UTF-8"));
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");// 创建密码器
cipher.init(Cipher.DECRYPT_MODE, skey, iv);// 初始化解密器
byte[] result = cipher.doFinal(content);
return result; // 解密
}
public static void main(String[] args) throws Exception {
//明文
String content = "hi,this is a pig";
//密匙
System.out.println("密匙:" + pkey);
//加密
String aesEncryptStr = aesEncryptStr(content);
System.out.println("加密报文:" + aesEncryptStr);
//解密
String aesDecodeStr = aesDecodeStr(aesEncryptStr);
System.out.println("解密报文:" + aesDecodeStr);
System.out.println("加解密前后内容是否相等:" + aesDecodeStr.equals(content));
}
}