gpt4 book ai didi

java - 如何使用 JCE 中的 PBEWITHHMACSHA256ANDAES_256 算法

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

我想使用 SunJCE provider 中的 PBEWITHHMACSHA256ANDAES_256 算法在Java8中。

看起来 jar 和所有配置都在 Java8 中开箱即用,但我无法使用 PBEWITHHMACSHA256ANDAES_256 算法。

我有这两个 jar :

jdk1.8.0_40\jre\lib\jce.jar
jdk1.8.0_40\jre\lib\ext\sunjce_provider.jar

jdk1.8.0_40\jre\lib\security\java.security中有此条目

security.provider.5=com.sun.crypto.provider.SunJCE

jdk1.8.0_40\jre\lib\security\java.policy中有此条目

grant codeBase "file:${{java.ext.dirs}}/*" {
permission java.security.AllPermission;
};

当我调用 Security.getProviders() 时,我可以在数组中看到 com.sun.crypto.provider.SunJCE

但是以下代码抛出EncryptionOperationNotPossibleException

import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.jasypt.exceptions.EncryptionOperationNotPossibleException;
import org.junit.Assert;
import org.junit.Test;

public class EncryptionTest {
@Test
public void test() {
SimpleStringPBEConfig pbeConfig = new SimpleStringPBEConfig();
pbeConfig.setAlgorithm("PBEWITHHMACSHA256ANDAES_256");
pbeConfig.setPassword("changeme");
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
encryptor.setConfig(pbeConfig);

String encrypted = encryptor.encrypt("foo");
String decrypted = encryptor.decrypt(encrypted);
Assert.assertEquals("foo", decrypted);
}
}

异常

org.jasypt.exceptions.EncryptionOperationNotPossibleException: Encryption raised an exception. A possible cause is you are using strong encryption algorithms and you have not installed the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files in this Java Virtual Machine
at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.handleInvalidKeyException(StandardPBEByteEncryptor.java:999)
at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.encrypt(StandardPBEByteEncryptor.java:868)
at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.encrypt(StandardPBEStringEncryptor.java:642)
at foo.bar.EncryptionTest.test(EncryptionTest.java:40)

知道为什么 PBEWITHHMACSHA256ANDAES_256 抛出 EncryptionOperationNotPossibleException 吗?

最佳答案

异常提到:

Encryption raised an exception. A possible cause is you are using strong encryption algorithms and you have not installed the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files in this Java Virtual Machine

此答案仅尝试解决问题。我写过another answer来帮助解决以下问题,因为它们完全不同。

如果您居住的国家/地区允许,您可以从Oracle's website下载它。 .

然后,要安装这些无限强度的软件包,请进入您的 $JAVA_HOME/jre/lib/security/ 文件夹(假设您有 JDK)。

在那里,备份您的 local_policy.jarUS_export_policy.jar

现在,将下载到该文件夹​​的 zip 文件中的 local_policy.jarUS_export_policy.jar 文件解压缩,然后重新启动应用程序。您的应用程序现在可以访问无限强度的 JCE 功能。

如果出现任何问题,请将这两个文件恢复为其备份版本。

请注意,必须以这种方式“修补”每个必须运行此代码的 JVM。

关于java - 如何使用 JCE 中的 PBEWITHHMACSHA256ANDAES_256 算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40376435/

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