SecretKeyFactory 不可用 (Jasypt)

SecretKeyFactory Not Available (Jasypt)

我正在尝试使用 Jasypt 1.9.3 加密字符串,我的 JDK 版本是 1.8。0_281。

这是我写的代码:

Security.setProperty("crypto.policy", "unlimited");
            
if (pooledPBEStringEncryptor == null) {
    
    pooledPBEStringEncryptor = new PooledPBEStringEncryptor();
    
    pooledPBEStringEncryptor.setPassword(encryptionKey);
    pooledPBEStringEncryptor.setAlgorithm("PBEWITHHMACSHA512ANDAES256");
    pooledPBEStringEncryptor.setPoolSize(4);
    pooledPBEStringEncryptor.setSaltGenerator(new RandomSaltGenerator());
}       

encrypted = pooledPBEStringEncryptor.encrypt(cValue);

但是当我 运行 它时,我得到了错误

Exception in thread "main" java.lang.RuntimeException: Security Error in doEncrypt: org.jasypt.exceptions.EncryptionInitializationException: java.security.NoSuchAlgorithmException: PBEWITHHMACSHA512ANDAES256 SecretKeyFactory not available

我 运行 AlgorithmRegistry.getAllPBEAlgorithms() 我的输出是:

PBEWITHHMACSHA1ANDAES_128, PBEWITHHMACSHA1ANDAES_256, PBEWITHHMACSHA224ANDAES_128, PBEWITHHMACSHA224ANDAES_256, PBEWITHHMACSHA256ANDAES_128, PBEWITHHMACSHA256ANDAES_256, PBEWITHHMACSHA384ANDAES_128, PBEWITHHMACSHA384ANDAES_256, PBEWITHHMACSHA512ANDAES_128, PBEWITHHMACSHA512ANDAES_256, PBEWITHMD5ANDDES, PBEWITHMD5ANDTRIPLEDES, PBEWITHSHA1ANDDESEDE, PBEWITHSHA1ANDRC2_128, PBEWITHSHA1ANDRC2_40, PBEWITHSHA1ANDRC4_128, PBEWITHSHA1ANDRC4_40

当我使用算法 PBEWITHHMACSHA256ANDAES_256 时,我得到了一个不同的错误。

Exception in thread "main" java.lang.RuntimeException: Security Error in doEncrypt: org.jasypt.exceptions.EncryptionOperationNotPossibleException

我有点不知所措。

我已经从 Oracle 下载了无限策略 jar 并将它们保存在 JAVA_HOME\jre\lib\security\ 文件夹中。我在 Windows.

该代码缺少 setIvGenerator() 的 IV 生成器的规范,例如:

pooledPBEStringEncryptor.setIvGenerator(new RandomIvGenerator());

默认使用NoIvGenerator,由于算法采用CBC模式,需要IV,所以会出现异常。

顺便说一下,默认的盐生成器是 RandomSaltGenerator, so this would not necessarily need to be specified with setSaltGenerator()

PooledPBEStringEncryptor#encrypt()方法returns盐(16字节)、IV(16字节)和密文的Base64编码串联。


异常org.jasypt.exceptions.EncryptionOperationNotPossibleException是一般异常,在很多错误情况下都会产生,因此意义不大,见here。这包括例如缺少 JCE Unlimited Strength Jurisdiction Policy(但它似乎已安装在您的系统上)。

为了完整起见:该算法称为 PBEWITHHMACSHA512ANDAES_256(您自己已经弄明白了)。
PBEWITHHMACSHA512ANDAES_256 使用 PBKDF2 从密码和盐派生出 AES-256 的 32 字节密钥。 HMAC/SHA512 已应用。由于未明确指定,因此使用默认迭代计数 1000。该算法采用CBC模式进行加密(这就是为什么需要IV)。