定义常量
//定义AES_128常量
public static final Integer AES_SIZE_128=128;
public static final String LAGORITHM_AES="AES";
public static final String CIPHER_ALGORITHM_CBC = "AES/CBC/PKCS5Padding";
public static final String BC_PROVIDER = "BC";
public static final String DEFAULT_ROOT_IV="000000";
public static final String DEFAULT_CHARSET="UTF-8";
加密解码实现
package com.wd.demo;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import com.web.crypty.AESConstants;
import com.web.crypty.WandaCryptoAESException;
public final class AES128Realize {
private final byte[] passwordKey;
private final Integer keySize;
public Integer getKeySize() {
return keySize;
}
public AES128Realize(byte[] passwordKey,Integer keySize) {
this.passwordKey=passwordKey;
this.keySize=keySize;
}
public AES128Realize(byte[] passwordKey) {
this(passwordKey,AES128Constant.AES_SIZE_128);
}
//创建AES_128密码规则
public Cipher getCipher(Integer a) throws WandaCryptoAESException{
//定义密钥规则
try {
SecretKeySpec key = new SecretKeySpec(passwordKey, AESConstants.ALGORITHM_AES);
Cipher cipher = Cipher.getInstance(AESConstants.CIPHER_ALGORITHM_CBC,AESConstants.BC_PROVIDER);// 创建密码器
cipher.init(a, key, new IvParameterSpec(AESConstants.DEFAULT_ROOT_IV.getBytes()));// 初始化
//返回cipher
return cipher;
}catch (Exception e) {
throw new WandaCryptoAESException("Failed to get the cipher with passwordBytes [" + passwordKey + "] key size + [" + keySize + "] mode [" + a + "]", e);
}
}
//调用创建AES_128规则方法
protected Cipher getCipherEncrypt() throws WandaCryptoAESException{
return getCipher(Cipher.ENCRYPT_MODE);
}
//调用创建AES_128规则方法
protected Cipher getCipherDecode() throws WandaCryptoAESException{
return getCipher(Cipher.DECRYPT_MODE);
}
//加密
public byte[] getEncrypt(String content) throws WandaCryptoAESException{
try{
byte[] byteContent=content.getBytes(AES128Constant.DEFAULT_CHARSET);
return getCipherEncrypt().doFinal(byteContent);
}catch (Exception e) {
throw new WandaCryptoAESException("Failed to get the cipher with passwordBytes [" + passwordKey + "] key size + [" + keySize + "] mode [" + content + "]", e);
}
}
//解密
public byte[] getDecode(String content) throws WandaCryptoAESException{
try{
byte[] byteContent=content.getBytes(AES128Constant.DEFAULT_CHARSET);
return getCipherDecode().doFinal(byteContent);
}catch (Exception e) {
throw new WandaCryptoAESException("Failed to get the cipher with passwordBytes [" + passwordKey + "] key size + [" + keySize + "] mode [" + content + "]", e);
}
}
}
调用接口
WandaAES wandaAES = new WandaAES(Hex.decodeHex(Main.HEX_AES_128_PASSWORD.toCharArray()));
// 1 将参数转换成JSON对象
ObjectMapper om = Jackson2ObjectMapperBuilder.json().build();
String jsonString = om.writeValueAsString(productParam);
// 2 加密
// 2.1 AES 128
byte[] encryptBytes = wandaAES.encrypt(jsonString);
// 2.2 BASE64
String base64String = Base64Utils.encodeToString(encryptBytes);
System.out.println(base64String);