在 Java 中使用 AES 加密文件可以通过多种方式来实现,我将为您介绍两种常见的方式:使用 Java 标准库和使用 Bouncy Castle 库。以下是它们的详细步骤流程和示例代码。
Maven 依赖:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-crypto</artifactId>
<version>1.1.0</version>
</dependency>
Gradle 依赖:
implementation group: 'org.apache.commons', name: 'commons-crypto', version: '1.1.0'
示例代码:
import org.apache.commons.crypto.Crypto;
import org.apache.commons.crypto.cipher.CryptoCipher;
import org.apache.commons.crypto.cipher.CryptoCipherFactory;
import org.apache.commons.crypto.cipher.CryptoCipherFactory.CipherProvider;
import org.apache.commons.crypto.utils.Utils;
import javax.crypto.spec.SecretKeySpec;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.SecureRandom;
public class AESEncryptionExample {
public static void main(String[] args) throws Exception {
// 生成AES密钥
SecureRandom random = new SecureRandom();
byte[] keyData = new byte[16]; // 128位密钥
random.nextBytes(keyData);
SecretKeySpec key = new SecretKeySpec(keyData, "AES");
// 初始化AES加密器
CryptoCipher cipher = Utils.getCipherInstance("AES/CBC/PKCS5Padding", null);
cipher.init(Cipher.ENCRYPT_MODE, key);
// 读取要加密的文件
InputStream input = new FileInputStream("input.txt");
OutputStream output = new FileOutputStream("encrypted_output.bin");
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buffer)) != -1) {
byte[] encryptedBytes = new byte[cipher.getOutputSize(bytesRead)];
int encryptedLength = cipher.update(buffer, 0, bytesRead, encryptedBytes, 0);
output.write(encryptedBytes, 0, encryptedLength);
}
// 最后处理可能剩余的数据
int encryptedLength = cipher.doFinal(new byte[0], 0);
if (encryptedLength > 0) {
byte[] finalBytes = new byte[encryptedLength];
output.write(finalBytes, 0, encryptedLength);
}
input.close();
output.close();
System.out.println("文件已成功加密。");
}
}
Maven 依赖:
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.68</version>
</dependency>
Gradle 依赖:
implementation group: 'org.bouncycastle', name: 'bcprov-jdk15on', version: '1.68'
示例代码:
import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.engines.AESEngine;
import org.bouncycastle.crypto.modes.CFBBlockCipher;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
import java.io.*;
import java.security.SecureRandom;
public class AESEncryptionWithBouncyCastle {
public static void main(String[] args) throws Exception {
// 生成AES密钥
SecureRandom random = new SecureRandom();
byte[] keyData = new byte[16]; // 128位密钥
random.nextBytes(keyData);
KeyParameter key = new KeyParameter(keyData);
// 初始化AES加密器
BlockCipher engine = new AESEngine();
CipherParameters parameters = new ParametersWithIV(key, new byte[16]); // 使用16字节的IV
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CFBBlockCipher(engine, 128), key);
// 读取要加密的文件
FileInputStream input = new FileInputStream("input.txt");
FileOutputStream output = new FileOutputStream("encrypted_output.bin");
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buffer)) != -1) {
int encryptedLength = cipher.processBytes(buffer, 0, bytesRead, buffer, 0);
output.write(buffer, 0, encryptedLength);
}
// 处理可能剩余的数据
int finalLength = cipher.doFinal(buffer, 0);
if (finalLength > 0) {
output.write(buffer, 0, finalLength);
}
input.close();
output.close();
System.out.println("文件已成功加密。");
}
}
上述示例中,两种方法都可以用来加密文件,您可以选择其中一种适合您的项目需求。请注意,文件解密需要相应的解密过程,以及密钥的管理和存储,这些步骤不在此示例中包含。