使用分组加密算法CBC,用Java编程实现对任意大小文件的加解密中的InputStream inputStream怎么写。完整代码
时间: 2024-05-08 08:15:15 浏览: 124
以下是一个使用CBC分组加密算法对任意大小文件进行加解密的Java代码,包括了InputStream inputStream的实现:
```
import java.io.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class FileEncryptor {
private static final int BUFFER_SIZE = 4096;
public static void encryptFile(String inputFile, String outputFile, String keyString, String ivString)
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
IOException {
Key secretKey = new SecretKeySpec(keyString.getBytes(), "AES");
IvParameterSpec iv = new IvParameterSpec(ivString.getBytes());
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
FileInputStream inputStream = new FileInputStream(inputFile);
FileOutputStream outputStream = new FileOutputStream(outputFile);
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[] encryptedBytes = cipher.doFinal();
outputStream.write(encryptedBytes);
inputStream.close();
outputStream.close();
}
public static void decryptFile(String inputFile, String outputFile, String keyString, String ivString)
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
IOException {
Key secretKey = new SecretKeySpec(keyString.getBytes(), "AES");
IvParameterSpec iv = new IvParameterSpec(ivString.getBytes());
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
FileInputStream inputStream = new FileInputStream(inputFile);
FileOutputStream outputStream = new FileOutputStream(outputFile);
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[] decryptedBytes = cipher.doFinal();
outputStream.write(decryptedBytes);
inputStream.close();
outputStream.close();
}
public static void main(String[] args) {
try {
String inputFile = "input.txt";
String encryptedFile = "encrypted.bin";
String decryptedFile = "decrypted.txt";
String keyString = "0123456789abcdef";
String ivString = "fedcba9876543210";
encryptFile(inputFile, encryptedFile, keyString, ivString);
decryptFile(encryptedFile, decryptedFile, keyString, ivString);
System.out.println("Encryption and decryption completed.");
} catch (Exception ex) {
System.out.println("Error: " + ex.getMessage());
}
}
}
```
在encryptFile和decryptFile方法中,我们首先创建了一个SecretKeySpec对象和一个IvParameterSpec对象,它们分别用于指定加密密钥和初始化向量。然后我们创建了一个Cipher对象,它用于进行加密或解密操作。接下来我们创建了一个FileInputStream对象和一个FileOutputStream对象,分别用于读取和写入文件内容。我们使用一个while循环来循环读取文件内容并进行加密或解密操作,并将加密或解密后的结果写入到输出文件中。最后我们关闭了输入输出流。
在main方法中,我们测试了加密和解密操作,并打印了“Encryption and decryption completed.”消息。注意,我们使用的密钥和初始化向量是硬编码的,这是为了方便演示。在实际使用中,建议将密钥和初始化向量存储在安全的地方,例如在加密密钥管理系统中。
阅读全文