AES CBC对称加密(JAVA PKCS#7填充算法)
package cn.lettin.common.aes;
import cn.lettin.ecc.Base64Utils;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class AesUtils {
private static final String AES = "AES";
private static final String AES_CBC_NO = "AES/CBC/NoPadding";
public static String encrypt(String secretKey, String originalData, String iv) throws Exception {
Cipher cipher = Cipher.getInstance(AES_CBC_NO);
SecretKeySpec keySpec = new SecretKeySpec(Base64Utils.decode(secretKey), AES);
IvParameterSpec ivSpec = new IvParameterSpec(Base64Utils.decode(iv));
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
ByteGroup byteCollector = new ByteGroup();
byteCollector.addBytes(originalData.getBytes());
byteCollector.addBytes(PKCS7Encoder.encode(byteCollector.size()));
byte[] bytes = cipher.doFinal(byteCollector.toBytes());
return Base64Utils.encode(bytes);
}
public static String decrypt(String secretKey, String encryptedData, String iv) throws Exception {
Cipher cipher = Cipher.getInstance(AES_CBC_NO);
SecretKeySpec keySpec = new SecretKeySpec(Base64Utils.decode(secretKey), AES);
IvParameterSpec ivSpec = new IvParameterSpec(Base64Utils.decode(iv));
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
byte[] original = cipher.doFinal(Base64Utils.decode(encryptedData));
byte[] bytes = PKCS7Encoder.decode(original);
return new String(bytes);
}
}
package cn.lettin.common.aes;
import java.util.ArrayList;
public class ByteGroup {
ArrayList<Byte> byteContainer = new ArrayList<>();
public byte[] toBytes() {
byte[] bytes = new byte[byteContainer.size()];
for (int i = 0; i < byteContainer.size(); i++) {
bytes[i] = byteContainer.get(i);
}
return bytes;
}
public ByteGroup addBytes(byte[] bytes) {
for (byte b : bytes) {
byteContainer.add(b);
}
return this;
}
public int size() {
return byteContainer.size();
}
}
package cn.lettin.common.aes;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
public class PKCS7Encoder {
static Charset CHARSET = StandardCharsets.UTF_8;
static int BLOCK_SIZE = 32;
public static byte[] encode(int count) {
int amountToPad = BLOCK_SIZE - (count % BLOCK_SIZE);
if (amountToPad == 0) {
amountToPad = BLOCK_SIZE;
}
char padChr = chr(amountToPad);
String tmp = new String();
for (int index = 0; index < amountToPad; index++) {
tmp += padChr;
}
return tmp.getBytes(CHARSET);
}
public static byte[] decode(byte[] decrypted) {
int pad = (int) decrypted[decrypted.length - 1];
if (pad < 1 || pad > 32) {
pad = 0;
}
return Arrays.copyOfRange(decrypted, 0, decrypted.length - pad);
}
public static char chr(int a) {
byte target = (byte) (a & 0xFF);
return (char) target;
}
}
@Test
public void test3() throws Exception {
String sessionKey = "UL2bN04+47AOM5L1/Fvm9A==";
String encryptedData = "+nXMtl5NVHXIWiXaESesXk9h8+teKzFO/LDLbQaACLnBNJsxaKc8dX+FRxI6aoso7PwP4OaeGKiIHtlkkaKrx3WOdpqyN1Zhuq8Pd08JzJBXWasfzVXVnf8rdRvaz7T0bdX3Ci4E/kWAwGgejL9QIXJENyDBnEZOFr12rJxsFFtMwc1QR5rlFV9mKY6RxYU16pRR+3N+QbEU/4+JLPAun/8kdk5HPVzhiDclkbZTNa+qMzze88Ew1Ir/64G8PGAtyo7rojiUh4thioB/VSk0WWbBYa9BajdDqN8CJ/OIVUuIn/3JmrCnkvzmHmRZfTeU9t/+D4HN64m9As0h53D7kcyRQwQ2dkawCFJqDvLQvZ02bxE9cjHERGHtxacRnQ5rdXKku2zDiZTLbnNv1PAsiiJ/Hcr2tFXKrAyN4pS0cOc=";
String iv = "8Xp9zfn6NrwTINgRKKyoXw==";
String decrypt = AesUtils.decrypt(sessionKey, encryptedData, iv);
System.out.println("解密结果 " + decrypt);
String encrypt = AesUtils.encrypt(sessionKey, decrypt, iv);
System.out.println("加密结果 " + encrypt);
}