gpt4 book ai didi

java - 需要帮助实现 CipherInputStream 和 CipherOutputStream

转载 作者:行者123 更新时间:2023-12-02 09:57:46 25 4
gpt4 key购买 nike

我在将 FileInputStream 和 FileOutputStream 转换为 3DES 加密算法的 CipherInputStream 和 CipherOutputStream 时遇到一些问题。有人可以指导我走上正确的道路吗?我最终收到错误:“CipherInputStream 无法解析为类型”。抱歉,如果这是显而易见的。

// author Alexander Matheakis
public static void main(String[] args) throws Exception {


// file to be encrypted
FileInputStream inputFile = new FileInputStream("plainfile.txt");

// encrypted file
FileOutputStream outputFile = new FileOutputStream("C:\\Users\\islan\\OneDrive\\Documents\\Encryptor\\plainfile.des");

// password to encrypt the file
String passKey = "tkfhkggovubm";


byte[] salt = new byte[8];
SecureRandom r = new SecureRandom();
r.nextBytes(salt);
PBEKeySpec pbeKeySpec = new PBEKeySpec(passKey.toCharArray());
SecretKeyFactory secretKeyFactory = SecretKeyFactory
.getInstance("PBEWithSHA1AndDESede");
SecretKey secretKey = secretKeyFactory.generateSecret(pbeKeySpec);


PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt, 99999);
Cipher cipher = Cipher.getInstance("PBEWithSHA1AndDESede");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, pbeParameterSpec);
outputFile.write(salt);

byte[] input = new byte[64];
int bytesRead;
while ((bytesRead = inputFile.read(input)) != -1) {
byte[] output = cipher.update(input, 0, bytesRead);
if (output != null)
outputFile.write(output);
}

byte[] output = cipher.doFinal();
if (output != null)
outputFile.write(output);

inputFile.close();
outputFile.flush();
outputFile.close();

// author Alexander Matheakis

最佳答案

这对我有用:

public class Cypher {

// password to encrypt the file
private static final String passKey = "tkfhkggovubm";

public static void main(String[] args) throws Exception {

try (FileInputStream inputFile = new FileInputStream("plainfile.txt");
FileOutputStream outputFile = new FileOutputStream("plainfile.des");) {

byte[] salt = new byte[8];
SecureRandom r = new SecureRandom();
r.nextBytes(salt);

outputFile.write(salt);

PBEKeySpec pbeKeySpec = new PBEKeySpec(passKey.toCharArray());
SecretKeyFactory secretKeyFactory = SecretKeyFactory
.getInstance("PBEWithSHA1AndDESede");
SecretKey secretKey = secretKeyFactory.generateSecret(pbeKeySpec);

PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt, 99999);
Cipher cipher = Cipher.getInstance("PBEWithSHA1AndDESede");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, pbeParameterSpec);

try (CipherOutputStream cis = new CipherOutputStream(outputFile, cipher);) {
IOUtils.copy(inputFile, cis);
}
}

try (FileInputStream fis = new FileInputStream("plainfile.des");
FileOutputStream outputFile = new FileOutputStream("plainfile.txt2");) {
byte[] salt = new byte[8];
int saltBytes = fis.read(salt);
if (saltBytes!=salt.length)
throw new Exception("Huh???");

PBEKeySpec pbeKeySpec = new PBEKeySpec(passKey.toCharArray());
SecretKeyFactory secretKeyFactory = SecretKeyFactory
.getInstance("PBEWithSHA1AndDESede");
SecretKey secretKey = secretKeyFactory.generateSecret(pbeKeySpec);

PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt, 99999);
Cipher cipher = Cipher.getInstance("PBEWithSHA1AndDESede");
cipher.init(Cipher.DECRYPT_MODE, secretKey, pbeParameterSpec);

try (CipherInputStream cis = new CipherInputStream(fis, cipher);) {
IOUtils.copy(cis, outputFile);
}
}
}
}

执行main后,plainfile.txtplainfile.txt2是相等的。 plainfile.des 已加密。

IOUtils.copyapache.commons-io 中的一个方法,它在内部保留一个缓冲区并将所有字节从一个流写入另一个流。它只是将 byte[] input = ... 提取到方法中,并使用 try-with-resources 。这使代码变得简单并具有更好的可读性。

关于java - 需要帮助实现 CipherInputStream 和 CipherOutputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55867506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com