- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用 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.jar
和 US_export_policy.jar
。
现在,将下载到该文件夹的 zip 文件中的 local_policy.jar
和 US_export_policy.jar
文件解压缩,然后重新启动应用程序。您的应用程序现在可以访问无限强度的 JCE 功能。
如果出现任何问题,请将这两个文件恢复为其备份版本。
请注意,必须以这种方式“修补”每个必须运行此代码的 JVM。
关于java - 如何使用 JCE 中的 PBEWITHHMACSHA256ANDAES_256 算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40376435/
我是一名优秀的程序员,十分优秀!