java实现文件加解密DES
时间: 2024-04-09 22:31:21 浏览: 13
可以使用Java的javax.crypto包来实现文件的加解密操作,下面是一个示例代码:
```java
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.io.*;
import java.nio.file.Files;
import java.security.spec.KeySpec;
public class FileEncryptDecrypt {
public static void encryptFile(String inputFile, String outputFile, String key) throws Exception {
FileInputStream fis = new FileInputStream(inputFile);
FileOutputStream fos = new FileOutputStream(outputFile);
encryptStream(fis, fos, key);
fis.close();
fos.close();
}
public static void decryptFile(String inputFile, String outputFile, String key) throws Exception {
FileInputStream fis = new FileInputStream(inputFile);
FileOutputStream fos = new FileOutputStream(outputFile);
decryptStream(fis, fos, key);
fis.close();
fos.close();
}
private static void encryptStream(InputStream is, OutputStream os, String key) throws Exception {
Cipher cipher = getCipher(Cipher.ENCRYPT_MODE, key);
processStream(cipher, is, os);
}
private static void decryptStream(InputStream is, OutputStream os, String key) throws Exception {
Cipher cipher = getCipher(Cipher.DECRYPT_MODE, key);
processStream(cipher, is, os);
}
private static Cipher getCipher(int cipherMode, String key) throws Exception {
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(cipherMode, secretKey);
return cipher;
}
private static void processStream(Cipher cipher, InputStream is, OutputStream os) throws Exception {
byte[] inputBytes = new byte[64];
byte[] outputBytes;
int bytesRead;
while ((bytesRead = is.read(inputBytes)) >= 0) {
outputBytes = cipher.update(inputBytes, 0, bytesRead);
if (outputBytes != null) {
os.write(outputBytes);
}
}
outputBytes = cipher.doFinal();
if (outputBytes != null) {
os.write(outputBytes);
}
}
public static void main(String[] args) {
String inputFile = "input.txt";
String encryptedFile = "encrypted.txt";
String decryptedFile = "decrypted.txt";
String key = "secretkey";
try {
// 加密文件
encryptFile(inputFile, encryptedFile, key);
System.out.println("File encrypted.");
// 解密文件
decryptFile(encryptedFile, decryptedFile, key);
System.out.println("File decrypted.");
// 验证解密后的文件内容是否与原始文件一致
byte[] originalFileContent = Files.readAllBytes(new File(inputFile).toPath());
byte[] decryptedFileContent = Files.readAllBytes(new File(decryptedFile).toPath());
boolean contentMatch = MessageDigest.isEqual(originalFileContent, decryptedFileContent);
System.out.println("Content match: " + contentMatch);
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
请注意,上述示例代码使用了DES算法进行加解密,使用了相同的密钥。在实际使用中,请确保密钥的安全性,并考虑使用更加安全的加密算法。此外,代码中的文件路径和密钥都是硬编码的,请根据实际需求进行修改。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)