我想在 Java8中使用SunJCE 提供程序PBEWITHHMACSHA256ANDAES_256
的算法。
看起来罐子和所有配置在 Java8 中都是开箱即用的,但我无法使用该PBEWITHHMACSHA256ANDAES_256
算法。
我有这两个罐子:
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;
};
我打电话时可以com.sun.crypto.provider.SunJCE
在数组中看到Security.getProviders()
但是下面的代码抛出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 的任何想法?