gpt4 book ai didi

java - Java代码AES加密,OpenSSL解密(终端)

转载 作者:太空宇宙 更新时间:2023-11-04 10:02:09 26 4
gpt4 key购买 nike

我可以使用下面提到的 Java 代码加密文件中的数据。但是当我尝试从命令行使用 OpenSSL 解密加密文件时,我无法做到这一点。

我试过这个命令

openssl enc -aes-256-cbc -d -in file_05.encrypt.txt -out file_05.decrypt.txt

它要求输入密码 -输入aes-256-cbc解密密码:我输入的密码是“helloworld”,

然后在终端中显示“错误的魔数(Magic Number)”错误消息。

String pwd  = "helloworld";
String SALT_VALUE = "12345@salt";
private String algorithm = "PBEWITHSHA256AND256BITAES-CBC-BC";


String finalTxt = "Hi, Goof After noon All.";

char[] pwd = Crypt.password.toCharArray();

SecretKey originalKey = Crypt.generateSK(pwd);

byte[] cipherText = Crypt.encrypt(finalTxt.getBytes(),SALT_VALUE.getBytes(), originalKey);

public static SecretKey generateSK(char[] passPhrase) throws NoSuchAlgorithmException,
InvalidKeySpecException,
NoSuchPaddingException,
InvalidAlgorithmParameterException,
InvalidKeyException {

PBEKeySpec pbeKeySpec = new PBEKeySpec(passPhrase);
SecretKeyFactory secretKeyFactory;
secretKeyFactory = SecretKeyFactory.getInstance(algorithm);
return secretKeyFactory.generateSecret(pbeKeySpec);
}



public static byte[] encrypt(byte[] image, byte[] salt, SecretKey sKey) throws InvalidKeyException,
IllegalBlockSizeException,
BadPaddingException,
InvalidKeySpecException,
UnsupportedEncodingException,
InvalidAlgorithmParameterException {
Cipher cipher;
try {
cipher = getCipher(Cipher.ENCRYPT_MODE, salt, sKey);
return cipher.doFinal(image);
} catch (Exception e) {
e.printStackTrace();

}

return null;
}

private static Cipher getCipher(int mode, @NonNull byte[] salt, @NonNull SecretKey secretKey) throws Exception {
PBEParameterSpec pbeParamSpecKey = new PBEParameterSpec(salt, 1000);
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(mode, secretKey, pbeParamSpecKey);
return cipher;
}

最佳答案

It asked for a password - enter aes-256-cbc decryption password: I entered the password as "helloworld",
Then in the terminal it shows a "bad magic number" error message

Openssl 默认使用其内部 EVP_BytesToKey从提供的密码和 salt 生成 key 和 IV 的函数。如果需要,只需在互联网上搜索即可找到 Java 实现。

默认 Openssl 期望格式 Salted__<8bit_salt><ciphertext>如果您不直接提供 key 和 IV。

I try to decrypt the encrypted file using OpenSSL from the command line then I am not be able to do that

我不确定你的 Crypt 类实现了什么,你可以尝试打印十六进制编码的 key 和 iv。使用带有参数 -iv <hex_IV> -K <hex_key>openssl可以直接提供IV和Key值来解密密文

关于java - Java代码AES加密,OpenSSL解密(终端),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55356861/

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