gpt4 book ai didi

java - java中解码加密文本文件时出错

转载 作者:行者123 更新时间:2023-11-30 04:06:07 26 4
gpt4 key购买 nike

我用 AES 算法加密了一个文本文件。我无法解密它。我使用相同的 key ,整个过程在相同的方法体中运行。首先,input.txt 文件被加密为encrypted.txt 文件。然后解码器,将加密的.txt解密为解密的.txt这是代码。感谢您的帮助。

public static void main(String[] args) throws IOException,
NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException {

Scanner sc = new Scanner(System.in);
String filename = sc.nextLine();
sc.close();

System.out.println("The file requested is " + filename);

File file = new File(filename);

if (file.exists())
System.out.println("File found");

File to_b_encf = new File("encrypted.txt");

if (!to_b_encf.exists())
to_b_encf.createNewFile();

System.out.println("encrypting");

Cipher encipher = Cipher.getInstance("AES");
KeyGenerator keygen = KeyGenerator.getInstance("AES");
SecretKey key = keygen.generateKey();

encipher.init(Cipher.ENCRYPT_MODE, key);

FileOutputStream output = new FileOutputStream(to_b_encf);
FileInputStream input = new FileInputStream(filename);
CipherInputStream cis = new CipherInputStream(input, encipher);

int read;

while ((read = cis.read()) != -1) {
output.write(read);
output.flush();
}

input.close();
output.close();

System.out.println("done");
System.out.println("decrypting");

Cipher decipher = Cipher.getInstance("AES");//initiate a cipher for decryption
decipher.init(Cipher.DECRYPT_MODE, key);//decrypt the file

File sourcefile = new File("encrypted.txt");
File destfile = new File("decrypted.txt");

if (!destfile.exists())
destfile.createNewFile();

FileInputStream decf = new FileInputStream(sourcefile);
CipherInputStream c_decf = new CipherInputStream(decf,decipher);
FileOutputStream destf = new FileOutputStream(destfile);

cout = new CipherOutputStream(destf,decipher);

while ((read = c_decf.read()) != -1) {
cout.write(read);
cout.flush();
}

c_decf.close();
destf.close();
cout.close();
decf.close();
System.out.println("done");
}

最佳答案

你搞乱了InputStreamOutputStream等等。我制作了代码的简化版本(没有文件,所有内存中 I/O)来说明主要概念:

public class EncDec {

public static void main(String[] args) throws IOException
, InvalidKeyException, NoSuchAlgorithmException
, NoSuchPaddingException {

final String MESSAGE = "I'm a secret message";
final Charset CHARSET = Charset.defaultCharset();

Cipher cipher = Cipher.getInstance("AES");
SecretKey key = KeyGenerator.getInstance("AES").generateKey();
cipher.init(Cipher.ENCRYPT_MODE, key);

// Encrypt the message
InputStream plainIn = new ByteArrayInputStream(
MESSAGE.getBytes(CHARSET));
ByteArrayOutputStream encryptedOut = new ByteArrayOutputStream();
copy(plainIn, new CipherOutputStream(encryptedOut, cipher));

// Decrypt the message
cipher.init(Cipher.DECRYPT_MODE, key);
InputStream encryptedIn = new CipherInputStream(
new ByteArrayInputStream(encryptedOut.toByteArray()), cipher);
ByteArrayOutputStream plainOut = new ByteArrayOutputStream();
copy(encryptedIn, plainOut);

System.out.println(new String(plainOut.toByteArray(), CHARSET));
}

private static void copy(InputStream in, OutputStream out)
throws IOException {
byte[] buffer = new byte[4096];
while ( in.read(buffer) > -1) {
out.write(buffer);
}
out.flush();
}
}

Java I/O API 的灵感来自装饰器模式。加密/解密库提供了一个用于读取加密内容的装饰器 CipherInputStream 和一个用于加密普通源并将其写入装饰后的输出目标的装饰器 CipherOutputStream

关于java - java中解码加密文本文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20682226/

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