一、原理
AES 加密算法的原理详解
二、pom
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.13</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk16</artifactId>
<version>1.46</version>
</dependency>
三、AES-128位-有向量-CBC/PKCS5Padding
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
public class AesUtils {
private static final Charset EncodeCharset = StandardCharsets.UTF_8;
private static final String EncryptAlg = "AES";
private static final String CipherMode = "AES/CBC/PKCS5Padding";
private static final String SecretKey = "XXXXXXXXXXXXXXXX";
private static final String Iv = "2019090317231022";
private static byte[] baseEncrypt(String secretKey, String iv, String message) throws Exception {
byte[] secretKeyBytes = secretKey.getBytes(EncodeCharset);
byte[] messageBytes = message.getBytes(EncodeCharset);
byte[] ivBytes = iv.getBytes(EncodeCharset);
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKeyBytes, EncryptAlg);
Cipher cipher = Cipher.getInstance(CipherMode);
IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
byte[] resBytes = cipher.doFinal(messageBytes);
return resBytes;
}
public static String encrypt(String secretKey, String iv, String message) throws Exception {
byte[] resultBytes = baseEncrypt(secretKey, iv, message);
try {
String result=Base64.encodeBase64String(resultBytes);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static byte[] baseDecrypt(String secretKey, String iv, String encryptText) throws Exception {
byte[] secretKeyBytes = secretKey.getBytes(EncodeCharset);
byte[] ivBytes = iv.getBytes(EncodeCharset);
byte[] encryptTextBytes = Base64.decodeBase64(encryptText);
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKeyBytes, EncryptAlg);
Cipher cipher = Cipher.getInstance(CipherMode);
IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
byte[] resBytes = cipher.doFinal(encryptTextBytes);
return resBytes;
}
public static String decrypt(String secretKey, String iv, String encryptText) throws Exception {
byte[] resultBytes = baseDecrypt(secretKey, iv, encryptText);
return new String(resultBytes, EncodeCharset);
}
public static void main(String[] args) throws Exception{
String content="{\"userId\": 10, \"message\": \"nice to meet you\"}";
String encryptText = encrypt(SecretKey, Iv, content);
String decryptText = decrypt(SecretKey, Iv, encryptText);
System.out.println(String.format("加密结果:%s\n解密结果:%s ",content,encryptText,decryptText));
}
}
四、AES-128位-无向量-ECB/PKCS7Padding
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.Security;
public class Aes7 {
private static final int Secret_Key_Size = 32;
private static final String EncryptAlg ="AES";
private static final String Cipher_Mode="AES/ECB/PKCS7Padding";
private static final Charset Encode_Charset = StandardCharsets.UTF_8;
static {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
}
public static String encrypt(String content, String key) throws Exception {
byte[] keyBytes = secretKeyBytes(key);
byte[] contentBytes = content.getBytes(Encode_Charset);
Cipher cipher = Cipher.getInstance(Cipher_Mode);
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyBytes, EncryptAlg));
byte[] encrayptBytes = cipher.doFinal(contentBytes);
String encrayptBase64Bytea = new Base64().encodeToString(encrayptBytes);
return encrayptBase64Bytea;
}
public static String decrypt(String content, String key) throws Exception {
byte[] keyBytes = secretKeyBytes(key);
byte[] decodeBytes = Base64.decodeBase64(content);
Cipher cipher = Cipher.getInstance(Cipher_Mode);
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyBytes, EncryptAlg));
byte[] realBytes=cipher.doFinal(decodeBytes);
return new String(realBytes, Encode_Charset);
}
public static byte[] secretKeyBytes(String key) throws Exception{
final byte paddingChar = '0';
byte[] resKeyBytes = new byte[Secret_Key_Size];
byte[] keyBytes = key.getBytes(Encode_Charset);
for (int i=0; i<Secret_Key_Size; i++){
if (i < keyBytes.length){
resKeyBytes[i] = keyBytes[i];
} else {
resKeyBytes[i] = paddingChar;
}
}
return resKeyBytes;
}
public static void main(String[] args) throws Exception{
String key="securitykey";
String content="{\"userId\": 10, \"message\": \"nice to meet you\"}";
String encryptRes = encrypt(content,key);
String decryptRes = decrypt(encryptRes,key);
System.out.println(String.format("加密结果:%s\n解密结果:%s",encryptRes, decryptRes));
}
}