仅使用提供的密钥在java中加密Openssl aes256cbc
当我使用命令行工具openssl
对文件进行解密时,通过下面的步骤就可以了。
我没有关于加密的任何信息,客户端只提供以下命令和密钥
openssl enc -d -aes-256-cbc -in 8MP_2018_12_12.gz.enc -out 8MP_2018_12_12.gz.enc.gz -pass file:pass.txt
我已经在stackoverflow中检查了很多问题,测试并运行了很多程序。因为我只提供了密钥,所以不能使用ivparameterspec
顺便说一句。txt,有一个提供的密钥
xxxxxxx12354125222sdsf <- example
我的节目
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import org.apache.commons.ssl.OpenSSL;
public class OpenSSLTest {
public static void main(String[] args) throws Exception {
File inputFile = new File("D:\\temp\\8MP_2018_12_12.gz.enc");
File outputFile = new File("D:\\temp\\8MP_2018_12_12.gz");
FileInputStream inputStream = new FileInputStream(inputFile);
InputStream in = OpenSSL.decrypt("aes-256-cbc", "xxxxxxx12354125222sdsf".toCharArray(), inputStream);
FileOutputStream outputStream = new FileOutputStream(outputFile);
IOUtils.copy(in, outputStream);
outputStream.flush();
outputStream.close();
in.close();
}
}
当我运行上述程序时,我得到以下消息
Exception in thread "main" java.security.InvalidKeyException: Illegal key size
at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1039)
at javax.crypto.Cipher.implInit(Cipher.java:805)
at javax.crypto.Cipher.chooseProvider(Cipher.java:864)
at javax.crypto.Cipher.init(Cipher.java:1396)
at javax.crypto.Cipher.init(Cipher.java:1327)
at org.apache.commons.ssl.PKCS8Key.generateCipher(PKCS8Key.java:420)
at org.apache.commons.ssl.OpenSSL.decrypt(OpenSSL.java:165)
at OpenSSLTest.main(OpenSSLTest.java:15)
我已经尝试根据Java Security: Illegal key size or default parameters?修复该问题
我已经下载了jce_policy-8.zip
。我已经把local_policy.jar
和US_export_policy.jar
放到了我的....\jre1.8.0_66\lib\security
目录中
我仍然收到上面的错误消息。我的JDK版本是jdk1。8.0_66.
# 1 楼答案
从} algorithm 来确定要使用的实际(在本例中为AES)密钥。该密钥必须为16、24或32字节(分别为128、192或256位)
pass.txt
获得的“key”字符串是22个字符。当OpenSSL使用“密码”派生密钥时,它使用^{我不知道您正在使用的库是如何从提供的密码中派生密钥的,但它似乎没有正确地执行此操作,或者如果是,则在此JVM实例中未启用256位密钥。需要检查的一件事是,除非重命名
jre
目录,否则目录路径实际上应该看起来像jdk_1.8.0_66\jre\lib\security
。您可以使用此代码段测试可用的密钥长度: