title: JASYPT with Spring Boot problem encrypt strong password and using it in the app
body: <p>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.</p>
  
  <p>I have an application.yml file that store my database user/password url &amp; ect' </p>
  
  <p>my pom.xml with the Jasypt looks like this: </p>
  
  <pre><code> &lt;!--Jasypt--&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;com.github.ulisesbocchio&lt;/groupId&gt;
              &lt;artifactId&gt;jasypt-spring-boot-starter&lt;/artifactId&gt;
              &lt;version&gt;2.1.2&lt;/version&gt;
              &lt;scope&gt;compile&lt;/scope&gt;
  
          &lt;/dependency&gt;
  </code></pre>
  
  <p>My Main has the following annotation :</p>
  
  <pre><code>@EnableEncryptableProperties
  </code></pre>
  
  <p>my db configuration class :
  Configuration</p>
  
  <pre><code>@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;
  </code></pre>
  
  <p>I have generated the password using : </p>
  
  <pre><code>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
  </code></pre>
  
  <p>in my application.yaml , at the database password location  I placed the generated password I genereted using Jasypt.
  ENC("myGeneratedPassword")</p>
  
  <p>When i run my application everything is working OK ,  the password is decrypted and the application work ok . </p>
  
  <p>my problem is when i have a strong password like : "A12$$T@@!" and i . want to encrypt it , 
  i get :</p>
  
  <pre><code>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**
  </code></pre>
  
  <p>I think it is because of the "!" </p>
  
  <p>I have created a class to use a stronger password :</p>
  
  <pre><code>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);
          }
      }
  </code></pre>
  
  <p>and the result is : 
  Encrypted: QGPEnAN7MpkRC4opCHn8ztSMoiX8Imx0PT/HI7X6yVDtE/bIs/pTaAID76teJ6TG
  Decrypted: A12$$T@@!</p>
  
  <p>when I apply my new encrypted password in my to application.yml : 
  password: ENC(QGPEnAN7MpkRC4opCHn8ztSMoiX8Imx0PT/HI7X6yVDtE/bIs/pTaAID76teJ6TG)</p>
  
  <p>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 )</p>
  
  <p>i get a null from the decrypt process :
  here is the stack-trace: </p>
  
  <pre><code>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)
  </code></pre>
  
  <p>when i try again mvn apring-boot:run  i get : </p>
  
  <pre><code>  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
  </code></pre>
  
  <p>I hope someone can help me out !!</p>
  
  <p>Thank You all , </p>