使用jasypt和spring boot时无法解密错误

7

我正在使用Spring Boot 2.2.2.RELEASE,当我尝试添加Jasypt功能来隐藏我的密码时,出现了以下错误:

Unable to decrypt: ENC(MyEncryptedPass). Decryption of Properties failed, make sure encryption/decryption passwords match

我使用命令行加密和解密密码,一切都运作正常,所以我很确定我的加密和解密密码是正确的,但当我尝试启动我的Spring应用程序时却出现了以下错误。请帮帮我(•–•)

3个回答

42

3

我也遇到了同样的问题。最初,我使用jasypt CLI进行加密,并将相同的值放入属性文件中。但是,com.github.ulisesbocchio jar的默认属性与CLI不同。请尝试使用以下代码进行加密。

private static StringEncryptor stringEncryptor() {
    PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
    SimpleStringPBEConfig config = new SimpleStringPBEConfig();
    config.setPassword(password);
    config.setAlgorithm("PBEWithMD5AndDES");
    config.setKeyObtentionIterations("1000");
    config.setPoolSize("1");
    config.setProviderName("SunJCE");
    config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
    config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
    config.setStringOutputType("base64");
    encryptor.setConfig(config);
    return encryptor;
}

private static String encrypt(String text) {
    StringEncryptor textEncryptor = stringEncryptor();
    String encryptedText = textEncryptor.encrypt(text);
    return encryptedText;
}

private static String decrypt(String text) {
    StringEncryptor textEncryptor = stringEncryptor();
    String decryptedText = textEncryptor.decrypt(text);
    return decryptedText;
}

public static void main(String[] args) {
    System.out.println(encrypt("StackOverFlow"));
}

所以唯一的解决方案就是制作自定义加密:''D - AHM200

0

好的,解决方法是将这个配置添加到我的 project.properties 文件中

jasypt.encryptor.iv-generator-classname=org.jasypt.iv.NoIvGenerator

可能是因为我正在使用算法 = PBEWithMD5AndDES,它不需要初始化向量。但当然,这只是我的解释,没有任何意义:''D。


为了帮助他人:从AES256TextEncryptor类中使用Algoritm PBEWithHMACSHA512AndAES_256。 - Forcuti Alessandro

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接