Oded Kessler
Oded Kessler

Reputation: 163

JASYPT with Spring Boot problem encrypt strong password and using it in the app

I have a spring boot project (Running with Spring Boot v2.1.7.RELEASE, Spring v5.1.9.RELEASE ) and i'm trying to use Jasypt (2.1.2) to encrypt my database password.

I have an application.yml file that store my database user/password url & ect'

my pom.xml with the Jasypt looks like this:

 <!--Jasypt-->

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

        </dependency>

My Main has the following annotation :

@EnableEncryptableProperties

my db configuration class : Configuration

@PropertySource("classpath:application.yml")
@PropertySource("classpath:external.properties")
//@EncryptablePropertySource("classpath:external.properties")
//@EncryptablePropertySource("classpath:application.yml")
@EnableTransactionManagement
public class DBConfiguration {

    @Value("${spring.datasource.pps-db.driver-class-name}")
    private String driver;

    @Value("${spring.datasource.pps-db.password}")
    private String password;

I have generated the password using :

java -cp ~/.m2/repository/org/jasypt/jasypt/1.9.2/jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="Aa123456"  password=secret algorithm=PBEWithMD5AndDES

----ENVIRONMENT-----------------

Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 11.0.5+10-LTS 



----ARGUMENTS-------------------

input: Aa123456
password: secret
algorithm: PBEWithMD5AndDES

----OUTPUT----------------------

c+E+8syd2Y1Tp1oNkJ2Xqk/9Pqt9l92B

in my application.yaml , at the database password location I placed the generated password I genereted using Jasypt. ENC("myGeneratedPassword")

When i run my application everything is working OK , the password is decrypted and the application work ok .

my problem is when i have a strong password like : "A12$$T@@!" and i . want to encrypt it , i get :

java -cp ~/.m2/repository/org/jasypt/jasypt/1.9.2/jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="A12$$T@@!"  password=secret algorithm=PBEWithMD5AndDESA1
-**bash: !": event not found**

I think it is because of the "!"

I have created a class to use a stronger password :

import org.jasypt.util.text.AES256TextEncryptor;

import java.security.NoSuchAlgorithmException;

    public class JasyptPasswordEcryptor {
        public static void main(String[] args) throws NoSuchAlgorithmException {

            String password = "A12$$T@@!";


            AES256TextEncryptor encryptor = new AES256TextEncryptor();

            encryptor.setPassword("secret");
            String myEncryptedText = encryptor.encrypt(password);
            System.out.println("Encrypted: "+myEncryptedText);

            String plainText = encryptor.decrypt(myEncryptedText);
            System.out.println("Decrypted: "+plainText);
        }
    }

and the result is : Encrypted: QGPEnAN7MpkRC4opCHn8ztSMoiX8Imx0PT/HI7X6yVDtE/bIs/pTaAID76teJ6TG Decrypted: A12$$T@@!

when I apply my new encrypted password in my to application.yml : password: ENC(QGPEnAN7MpkRC4opCHn8ztSMoiX8Imx0PT/HI7X6yVDtE/bIs/pTaAID76teJ6TG)

and then i try to run my application (mvn spring-boot:run) : i get an exception .. and i don't understand what is the problem .. (i think it has something to do with the java.security ,,, doesn't have a AES256TextEncryptor algorithm or something like that )

i get a null from the decrypt process : here is the stack-trace:

Caused by: org.springframework.cache.Cache$ValueRetrievalException: Value for key 'spring.datasource.pps-db.password' could not be loaded using 'com.ulisesbocchio.jasyptspringboot.caching.CachingDelegateEncryptablePropertySource$$Lambda$400/0x0000000800721440@2dec0f40'
    at org.springframework.cache.concurrent.ConcurrentMapCache.lambda$get$0 (ConcurrentMapCache.java:149)
    at java.util.concurrent.ConcurrentHashMap.computeIfAbsent (ConcurrentHashMap.java:1705)
    at org.springframework.cache.concurrent.ConcurrentMapCache.get (ConcurrentMapCache.java:144)
    at com.ulisesbocchio.jasyptspringboot.caching.CachingDelegateEncryptablePropertySource.getProperty (CachingDelegateEncryptablePropertySource.java:34)
    at com.ulisesbocchio.jasyptspringboot.wrapper.EncryptableMapPropertySourceWrapper.getProperty (EncryptableMapPropertySourceWrapper.java:31)
    at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty (PropertySourcesPropertyResolver.java:85)
    at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty (PropertySourcesPropertyResolver.java:62)
    at org.springframework.core.env.AbstractEnvironment.getProperty (AbstractEnvironment.java:539)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$1.getProperty (PropertySourcesPlaceholderConfigurer.java:137)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$1.getProperty (PropertySourcesPlaceholderConfigurer.java:133)
    at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty (PropertySourcesPropertyResolver.java:85)
    at org.springframework.core.env.PropertySourcesPropertyResolver.getPropertyAsRawString (PropertySourcesPropertyResolver.java:74)
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue (PropertyPlaceholderHelper.java:151)
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders (PropertyPlaceholderHelper.java:124)
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders (AbstractPropertyResolver.java:237)
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders (AbstractPropertyResolver.java:211)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties$0 (PropertySourcesPlaceholderConfigurer.java:175)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue (AbstractBeanFactory.java:851)

when i try again mvn apring-boot:run i get :

  at java.lang.Thread.run (Thread.java:834)
Caused by: org.springframework.cache.Cache$ValueRetrievalException: Value for key 'spring.datasource.pps-db.password' could not be loaded using 'com.ulisesbocchio.jasyptspringboot.caching.CachingDelegateEncryptablePropertySource$$Lambda$401/0x000000080071f840@618d748'
    at org.springframework.cache.concurrent.ConcurrentMapCache.lambda$get$0 (ConcurrentMapCache.java:149)

  at java.lang.reflect.Method.invoke (Method.java:566)
    at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run (AbstractRunMojo.java:543)
    at java.lang.Thread.run (Thread.java:834)
Caused by: java.lang.IllegalStateException: either 'jasypt.encryptor.password' or one of ['jasypt.encryptor.private-key-string', 'jasypt.encryptor.private-key-location'] must be provided for Password-based or Asymmetric encryption
    at com.ulisesbocchio.jasyptspringboot.encryptor.DefaultLazyEncryptor.lambda$null$5 (DefaultLazyEncryptor.java:54)
    at java.util.Optional.orElseThrow (Optional.java:408)
    at com.ulisesbocchio.jasyptspringboot.encryptor.DefaultLazyEncryptor.lambda$createDefault$6 (DefaultLazyEncryptor.java:54)
    at java.util.Optional.orElseGet (Optional.java:369)
    at com.ulisesbocchio.jasyptspringboot.encryptor.DefaultLazyEncryptor.createDefault (DefaultLazyEncryptor.java:50)
    at com.ulisesbocchio.jasyptspringboot.encryptor.DefaultLazyEncryptor.lambda$null$2 (DefaultLazyEncryptor.java:38)
    at java.util.Optional.orElseGet (O

I hope someone can help me out !!

Thank You all ,

Upvotes: 1

Views: 19141

Answers (1)

Oded Kessler
Oded Kessler

Reputation: 163

I would like to share my ipnut.

when trying to encrypt the password please use the following :

when trying to encypt a string with "!" ,,, please use 'yourpassword!!!'

please use ' ' insted of " "

Use Version 1.9.3

java -cp ~/.m2/repository/org/jasypt/jasypt/1.9.3/jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input='Aa$$@123456!' password=secret algorithm=PBEWithMD5AndDES

and add this to your pom :

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

</dependency>

I hope it helped .

Upvotes: 2

Related Questions