package com.csdn.woniu.example;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Random;
public class AESExample {
private static final String AES_ECB = "AES/ECB/PKCS5Padding";
private static final String AES_CBC = "AES/CBC/PKCS5Padding";
private static final String AES_CFB = "AES/CFB/PKCS5Padding";
private static final Integer IV_LENGTH = 16;
public static boolean isEmpty(Object str) {
return null == str || "".equals(str);
}
public static byte[] getBytes(String str){
if (isEmpty(str)) {
return null;
}
try {
return str.getBytes(StandardCharsets.UTF_8);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static String getIV(){
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for(int i = 0 ; i < IV_LENGTH ; i++){
int number = random.nextInt(str.length());
sb.append(str.charAt(number));
}
return sb.toString();
}
public static SecretKeySpec getSecretKeySpec(String key){
SecretKeySpec secretKeySpec = new SecretKeySpec(getBytes(key), "AES");
return secretKeySpec;
}
public static String encrypt(String text, String key){
if (isEmpty(text) || isEmpty(key)) {
return null;
}
try {
Cipher cipher = Cipher.getInstance(AES_ECB);
SecretKeySpec secretKeySpec = getSecretKeySpec(key);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] encryptedBytes = cipher.doFinal(getBytes(text));
return Base64.getEncoder().encodeToString(encryptedBytes);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static String decrypt(String text, String key){
if (isEmpty(text) || isEmpty(key)) {
return null;
}
byte[] textBytes = Base64.getDecoder().decode(text);
try {
Cipher cipher = Cipher.getInstance(AES_ECB);
SecretKeySpec secretKeySpec = getSecretKeySpec(key);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
byte[] decryptedBytes = cipher.doFinal(textBytes);
return new String(decryptedBytes, StandardCharsets.UTF_8);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static String encrypt(String text, String key, String iv, String mode){
if (isEmpty(text) || isEmpty(key) || isEmpty(iv)) {
return null;
}
try {
Cipher cipher = Cipher.getInstance(mode);
SecretKeySpec secretKeySpec = getSecretKeySpec(key);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(getBytes(iv)));
byte[] encryptedBytes = cipher.doFinal(getBytes(text));
return Base64.getEncoder().encodeToString(encryptedBytes);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static String decrypt(String text, String key, String iv, String mode){
if (isEmpty(text) || isEmpty(key) || isEmpty(iv)) {
return null;
}
byte[] textBytes = Base64.getDecoder().decode(text);
try {
Cipher cipher = Cipher.getInstance(mode);
SecretKeySpec secretKeySpec = getSecretKeySpec(key);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(getBytes(iv)));
byte[] decryptedBytes = cipher.doFinal(textBytes);
return new String(decryptedBytes, StandardCharsets.UTF_8);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
String text = "123";
String key = "oltdqerpdwjbyugi";
String iv = "abcdefghijklmnok";
String encryptTextEBC = encrypt(text, key);
System.out.println("EBC 加密后内容:" + encryptTextEBC);
System.out.println("EBC 解密后内容:" + decrypt(encryptTextEBC, key));
System.out.println();
String encryptTextCBC = encrypt(text, key, iv, AES_CBC);
System.out.println("CBC 加密IV:" + iv);
System.out.println("CBC 加密后内容:" + encryptTextCBC);
System.out.println("CBC 解密后内容:" + decrypt(encryptTextCBC, key, iv, AES_CBC));
System.out.println();
String encryptTextCFB = encrypt(text, key, iv, AES_CFB);
System.out.println("CFB 加密IV:" + iv);
System.out.println("CFB 加密后内容:" + encryptTextCFB);
System.out.println("CFB 解密后内容:" + decrypt(encryptTextCFB, key, iv, AES_CFB));
}
}