直接上代码:
package com.macroflag.common.util;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.*;
public class AESUtil {
static StringENCRYPT_CHARSET ="UTF-8";
static Stringmode ="AES/CBC/PKCS7Padding";
private static final IvParameterSpecDEFAULT_IV =new IvParameterSpec(new byte[]{0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0});
public static void main(String[] args)throws Exception {
String content ="熬鹰";
String password ="123456";
System.out.println("原内容:\n"+content);
String tempContent = AESUtil.encrypt(content,password);
System.err.println("加密后:\n"+tempContent);
tempContent = AESUtil.decrypt("t182n7BQVRF1uuCKrCfPjtWFHwCFo7crpIDxY2EMaEHwVBEDNpXVNzmgDsEVS33NjpAY13iUHlTU2iYONqSDWS6i3ypoTcCKj5Lp7hVR8ecL9mtLD0aRsOOzL0s4O4SvarQQKIokBgTTff0kxNGAawOx9c8hqo2/zdwy+tPAsxffCvX0wei3ojm3Dqpn17WmpdBFs3J1dHDLptGhr4f0tGLwUcZn1VZ0PqJPdN7jWwO/Yg8e+aCahVg8yLaFqaoUcjcHsrVxAeKRqsZmqMBr8c3RfC06L6ROK2ubV5p1p2BvrZXQsDuEAFZgdX20lNrn0CLSiu3WCk0te6C/ep6TwaJ0DI1X8ctH1EP86In8mr+CdOIsqSdxV7NFMXzPWKFTaY2Va3fKJv/3Sr0PdVQsHG77mkLOvta8hSvXZofw9f43hO/z3WgJGLX8ePLK13gmrXldJM4r6FSlUJ++Wc6NWQ==", password );
System.out.println("解密后:\n"+tempContent);
}
public static String encrypt(String content, String password) {
try {
// SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
// KeyGenerator kgen = KeyGenerator.getInstance("AES");
// secureRandom.setSeed(password.getBytes());
// kgen.init(128, secureRandom);
// SecretKey secretKey = kgen.generateKey();
// byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key =new SecretKeySpec(md5(password),"AES");
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
Cipher cipher = Cipher.getInstance(mode);// 创建密码器
byte[] byteContent = content.getBytes(ENCRYPT_CHARSET);
try {
cipher.init(Cipher.ENCRYPT_MODE, key,DEFAULT_IV);// 初始化
}catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
byte[] result = cipher.doFinal(byteContent);
if (result !=null && result.length >0) {
/*
* 注意: return new String(result,ENCRYPT_CHARSET);
* 会出现javax.crypto.IllegalBlockSizeException: Input length must be multiple of
* 16 when decrypting with padded cipher 加密后的byte数组是不能强制转换成字符串的,
* 字符串和byte数组在这种情况下不是互逆的; 需要将二进制数据转换成十六进制表示
*/
return Base64.encodeBase64String(result);
}
}catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}catch (NoSuchPaddingException e) {
e.printStackTrace();
}catch (InvalidKeyException e) {
e.printStackTrace();
}catch (UnsupportedEncodingException e) {
e.printStackTrace();
}catch (IllegalBlockSizeException e) {
e.printStackTrace();
}catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
/**
* 解密
*
* @param content 待解密内容
* @param password 解密密钥
* @return
*/
public static String decrypt(String content, String password) {
try {
if (content ==null) {
return null;
}
byte[] newContent = Base64.decodeBase64(content);// 将16进制转换为二进制
/* SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
KeyGenerator kgen = KeyGenerator.getInstance("AES");
secureRandom.setSeed(password.getBytes());
kgen.init(128, secureRandom);
// kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");*/
SecretKeySpec key =new SecretKeySpec(md5(password),"AES");
Cipher cipher = Cipher.getInstance(mode);// 创建密码器
try {
cipher.init(Cipher.DECRYPT_MODE, key,DEFAULT_IV);// 初始化
}catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
byte[] result = cipher.doFinal(newContent);
if (result !=null && result.length >0) {
return new String(result, StandardCharsets.UTF_8);
}
}catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}catch (NoSuchPaddingException e) {
e.printStackTrace();
}catch (InvalidKeyException e) {
e.printStackTrace();
}catch (IllegalBlockSizeException e) {
e.printStackTrace();
}catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
private static byte[] md5(String key){
try{
key=key+ConfigUtil.get("home.secretKey");
MessageDigest md=MessageDigest.getInstance("MD5");
md.update(key.getBytes(StandardCharsets.UTF_8));
return md.digest();
}catch (Exception e){
e.printStackTrace();
}
return null;
}
}
添加第三方依赖
1.在jdk中的jre\lib\security修改java.security文件,替换security.provider.7=org.bouncycastle.jce.provider.BouncyCastleProvider。
2./jdk/jre/lib/ext下添加jar包bcprov-jdk15on-1.58.jar