使用分组加密算法CBC,用Java编程实现对任意大小文件的加解密。完整代码
时间: 2024-06-12 07:05:43 浏览: 9
由于加解密涉及到文件读写操作,因此需要使用Java的IO流。CBC模式需要一个初始向量,可以随机生成,也可以使用固定值。以下是完整代码:
```java
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
public class CBCEncryptDecrypt {
private static final String ALGORITHM = "AES/CBC/PKCS5Padding";
private static final int BUFFER_SIZE = 1024 * 1024;
public static void encrypt(String inputFilePath, String outputFilePath, String key, String iv) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
try (InputStream inputStream = new FileInputStream(inputFilePath);
OutputStream outputStream = new FileOutputStream(outputFilePath)) {
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
byte[] encryptedBytes = cipher.update(buffer, 0, bytesRead);
outputStream.write(encryptedBytes);
}
byte[] finalBytes = cipher.doFinal();
outputStream.write(finalBytes);
}
}
public static void decrypt(String inputFilePath, String outputFilePath, String key, String iv) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
try (InputStream inputStream = new FileInputStream(inputFilePath);
OutputStream outputStream = new FileOutputStream(outputFilePath)) {
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
byte[] decryptedBytes = cipher.update(buffer, 0, bytesRead);
outputStream.write(decryptedBytes);
}
byte[] finalBytes = cipher.doFinal();
outputStream.write(finalBytes);
}
}
public static void main(String[] args) throws Exception {
String inputFilePath = "input.txt";
String encryptedFilePath = "encrypted.bin";
String decryptedFilePath = "decrypted.txt";
String key = "0123456789abcdef";
String iv = "fedcba9876543210";
encrypt(inputFilePath, encryptedFilePath, key, iv);
decrypt(encryptedFilePath, decryptedFilePath, key, iv);
}
}
```
在main方法中,我们指定了输入文件、加密后的输出文件、解密后的输出文件、密钥和初始向量。可以根据实际情况修改这些参数。在encrypt方法和decrypt方法中,我们使用了Java的try-with-resources语句来自动关闭文件流。加解密的过程中,我们使用了Cipher类和SecretKeySpec类来进行加解密操作。在加密过程中,我们使用了update方法来逐步加密数据,并在最后使用doFinal方法来处理最后一块数据。在解密过程中,我们也采用了类似的方法来逐步解密数据。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)