org.jasypt.exceptions.EncryptionOperationNotPossibleException

时间:2013-03-21 09:57:34

标签: spring hibernate password-encryption jasypt

我正在使用 Jasypt-1.9.0 Spring 3.1 Hibernate 4.0.1 。我的应用程序中要求连接到数据库,其密码(root)以加密形式存储在应用程序的属性文件中。

我在网上查找了以下链接的方式:

  1. http://www.jasypt.org/spring31.html

  2. http://www.jasypt.org/hibernate.html

  3. http://www.jasypt.org/encrypting-configuration.html

  4. 我已根据我的要求完成了以下步骤和配置:

    • 在构建路径中添加 jasypt-1.9.0 jasypt-hibernate4 -1.9.0。
    • 在我的 dispatcher-servlet 文件中添加了以下内容:
      

    < bean id =“propertyConfigurer”            类= “org.jasypt.spring31.properties.EncryptablePropertyPlaceholderConfigurer” >

       < constructor-arg ref="configurationEncryptor" />
       < property name="locations">
         < list>
           < value>classpath:database.properties< /value>
         < /list>
       < /property>
     < /bean>
    
     < bean id="configurationEncryptor"
         class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
       < property name="config" ref="environmentVariablesConfiguration" />
     < /bean>
    
     < bean id="environmentVariablesConfiguration"
    class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
    < property name="algorithm" value="PBEWithMD5AndDES" />
    < property name="passwordEnvName" value="APP_ENCRYPTION_PASSWORD" />
    </bean>
    
    • 使用 Jasypt 1.9.0的CLI工具,我已生成密码 下面(CLI的附加快照)

    enter image description here   - 添加了一个新的环境变量为 APP_ENCRYPTION_PASSWORD ,其值为 root

    • database.properties文件
    • 中添加了加密密码
    db.driverClassName=com.mysql.jdbc.Driver
    db.url=jdbc:mysql://localhost:3306/db1
    db.username=root
    db.password=ENC(bmfeQmgP/hJrh+mj6NANKA==)
    

    现在,如果我运行我的应用程序,则会出现以下异常:

    org.jasypt.exceptions.EncryptionOperationNotPossibleException
        at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.decrypt(StandardPBEByteEncryptor.java:981)
        at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.decrypt(StandardPBEStringEncryptor.java:725)
        at org.jasypt.properties.PropertyValueEncryptionUtils.decrypt(PropertyValueEncryptionUtils.java:72)
    

5 个答案:

答案 0 :(得分:8)

问题很可能是过时的,但对于未来的寻求者...... EncryptionOperationNotPossibleException是jasypt抛出的一般异常,用于掩盖其他可能的异常。在以下情况下可能会发生此异常:

  • 你的jdk没有安装JCE无限强度(最常见的情况)
  • 您之前使用其他密码
  • 对数据库中的某些数据进行了加密
  • 您之前在数据库中有一些未加密的数据,并且您在某些字段中添加了加密
  • 由于数据的一些奇怪损坏,jasypt无法解密db中的加密值
  • 许多其他人,你只需要调试以找出真正的原因..

答案 1 :(得分:2)

如果在加密过程中未指定所有参数,Jasypt将使用默认值。确保在解密过程中使用这些确切的默认值。否则您可能会遇到麻烦...

为我工作:

YourNammespace.YourClass
mvn jasypt:encrypt -Djasypt.encryptor.password='secret' -Djasypt.encryptor.algorithm=PBEWITHHMACSHA512ANDAES_256 -Djasypt.encryptor.iv-generator-classname=org.jasypt.iv.RandomIvGenerator -Djasypt.encryptor.salt-generator-classname=org.jasypt.salt.RandomSaltGenerator -Djasypt.encryptor.key-obtention-iterations=1000  -Djasypt.plugin.path='file:application.yml' 

答案 2 :(得分:0)

加密属性文件值时,我也遇到了类似的问题。我在本地Windows机器中加密了值并尝试在Linux机器中部署,但 JRE版本不同,因此无法解密加密值。但是我在Linux机器上加密了值,解密成功了。

答案 3 :(得分:0)

我遇到了类似的问题,但是我意识到在使用CLI工具并尝试解密密码时,您不必包括algorithm属性,并且password属性需要与CLI工具中使用的密码匹配。

在他们的http://www.jasypt.org/encrypting-configuration.html

他们的例子看起来像这样,但这不起作用。

encryptor.setPassword("jasypt"); // could be got from web, env variable... encryptor.setAlgorithm("PBEWithHMACSHA512AndAES_256"); encryptor.setIvGenerator(new RandomIvGenerator());


解决方案:

encryptor.setPassword("MYPAS_WORD"); // Like in the CLI Tool encryptor.setAlgorithm("PBEWithHMACSHA512AndAES_256"); //Remove this encryptor.setIvGenerator(new RandomIvGenerator()); //Remove this as well

它将正常工作。

在这种情况下,您可以删除算法属性,并且passwordEvnName需要与CLI工具中使用的匹配。

答案 4 :(得分:0)

  1. 删除所有上述 XML 配置并将以下 bean 添加到您的配置类:

    @Bean public DataSource dataSource() {
    DataSourceBuilder dataSourceBuilder = 
    DataSourceBuilder.create();
    dataSourceBuilder.url(dbUrl);
    dataSourceBuilder.username(username);
    dataSourceBuilder.password(password);
    return dataSourceBuilder.build(); 
    }
    
  2. 从属性中添加值,例如

    @Value("${db.driverclassname}")
    private String dbDriverClassName;
    
    @Value("${db.url}")
    private String dbUrl;
    
    @Value("${db.username}")
    private String dbUsername;
    
    @Value("${db.password}")
    private String dbPassword;
    

    并在数据源上方传递这些值。

  3. 在属性文件中配置您的加密密钥,如#

    db.driverclassname=com.mysql.jdbc.Driver
    db.url=jdbc:mysql://localhost:3306/contactsdb
    db.username=contactsuser
    db.password=ENC(XcBjfjDDjxeyFBoaEPhG14wEzc6Ja+Xx
    +hNPrJyQT888=
    
  4. 不要使用 cmd 和 jaspyt jar 创建您的加密密钥,我将分享使用您的密钥创建加密密钥的链接:

    Jasypt Online Encryption and Decryption

  1. 根据您的版本添加 jaspyat 依赖项。

    如果您必须在服务器上运行,并且遇到密码加密不匹配或无法加密等问题,请再添加一个 jdbc 模板 bean:

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource 
    dataSourcee)
    {
        return new JdbcTemplate(dataSource);
    }
    

它工作正常,没有发现任何问题。

使用该工具创建密钥。因为我用jaspyt命令行尝试了很多次,但是加密是错误的,不支持。您可以将使用上述工具生成的密钥与密钥进行交叉检查。