gpt4 book ai didi

java - 我如何在 Java 中解密 openssl?

转载 作者:行者123 更新时间:2023-12-01 14:28:28 25 4
gpt4 key购买 nike

我需要通过传递 key 在 Java 中解密 openssl 加密文件。

我之前已经从下面的链接中检查过,但它不包括显式关键参数传递和逐行读取文件。 How to decrypt file in Java encrypted with openssl command using AES?

不同的是,我的文件是整体加密的,而不是行加密的,而且我有一个明确的 key 来解密文件。

另一个问题是我的文件太大了,我不确定在第一步中将文件作为一个整体保存在内存中的最佳方法。

提前致谢。

最佳答案

I need to decrypt openssl encrypted file in Java by passing key.

openssl enc -d -aes-256-cbc -in myfile.csv.enc -out myoutputfile.csv -pass key.bin

这里您提供的是密码文件,而不是 key 。 key 和 IV 是根据密码和随机盐计算的。

<删除>`openssl enc -K e849fb4e3779791a3ffe9f576b086bdf -iv 23acf784ff126ab52c90d15fd7ecb921 -e -aes-128-cbc -in notes.txt -out notes.enc`与示例不同,加密 key 和 IV 是根据密码(和随机盐)计算的,显式提供 key 和 IV 存储的数据是原始加密数据(没有任何内容)。

As differently, my files encrypted as a whole rather than line encryption and I have an explicit key to decrypt the file.

        Cipher cipher = Cipher.getInstance("AES/CBC/Pkcs5Padding");
byte[] passwordBytes = readPasswordBytes();
InputStream in = new BufferedInputStream(new FileInputStream("notes.enc"));
byte[] saltedString = new byte[8];
in.read(saltedString); // read the "Salted__" prefix
byte[] salt = new byte[8];
in.read(salt);
// see the EVP_BytesToKey and parameters from the linked question
byte[][] keyAndIv = EVP_BytesToKey(
KEY_SIZE_BITS / Byte.SIZE,
cipher.getBlockSize(),
md5,
salt,
passwordBytes,
ITERATIONS);
byte[] key = keyAndIv[0];
byte[] iv = keyAndIv[1];

SecretKeySpec secKeySpec = new SecretKeySpec(key, "AES");
IvParameterSpec ivSpec = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, secKeySpec, ivSpec);

// here we will use 4kB buffer to read and decrypt data
byte[] buffer = new byte[4096];

OutputStream out = new BufferedOutputStream(new FileOutputStream("notes2.txt"));
for(int readBytes = in.read(buffer); readBytes>-1; readBytes = in.read(buffer)) {
byte[] decrypted = cipher.update(buffer, 0, readBytes);
out.write(decrypted);
}
byte[] decrypted = cipher.doFinal();
out.write(decrypted);
out.flush();
out.close();
in.close();

关于java - 我如何在 Java 中解密 openssl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52038172/

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