无法使用StandardPBEStringEncryptor解密

时间:2019-03-27 10:49:26

标签: java spring-boot encryption secret-key jasypt

我正在尝试使用密码解密加密密钥。我正在从属性文件中读取这些内容。解密失败,原因是java.security.NoSuchAlgorithmException: PBEWithMD5AndDES SecretKeyFactory not available。尝试使用BasicTextEncryptor作为默认算法的PBEWithMD5AndDES。正如我看到的提到的异常一样,我尝试使用StandardPBEStringEncryptor。我也设置了算法,但仍然面临相同的问题。下面是代码

public class PasswordDecrypt {

    private static char[] password;

static {
    try (InputStream in = PasswordDecrypt.class.getClassLoader().getResourceAsStream("application-unitTest.properties");) {
        Properties props = new Properties();
        props.load(in);
        password = props.getProperty("jasypt.encryptor.password").toCharArray();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static String decrypt(String text) {
//    BasicTextEncryptor decryptor = new BasicTextEncryptor();
//    decryptor.setPasswordCharArray(password);
//    String decryptedText = decryptor.decrypt(text);
//    return decryptedText;
       StandardPBEStringEncryptor decryptor = new StandardPBEStringEncryptor();
       decryptor.setPasswordCharArray(password);
       decryptor.setAlgorithm("PBEWithMD5AndDES");  String 
       decryptedText = decryptor.decrypt(text);
       return decryptedText;
}
}

我不确定如何解决此问题。尝试更改Maven依赖项。以下尝试。

<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>2.0.0</version>
</dependency>

<dependency>
  <groupId>com.melloware</groupId>
  <artifactId>jasypt</artifactId>
  <version>1.9.4</version>
</dependency>

<dependency>
    <groupId>org.jasypt</groupId>
    <artifactId>jasypt</artifactId>
    <version>1.9.2</version>
</dependency>

1 个答案:

答案 0 :(得分:0)

好吧,很可能您是通过在其中的自定义模块中调用SecretKeyFactory对象实例(其中StandardPBEStringEncryptor类所在的模块缺少jce.jar)来依赖于这种自定义模块中的算法在您的模块中还是该模块中)。

此外,尝试将提供程序SunJCE添加到您的模块代码中,例如:

java.security.Security.addProvider(new com.sun.crypto.provider.SunJCE());