Search code examples
spring-bootencryptionjasypt

When using jasypt, how do I get the encrypted value to put in my config?


I have a Spring Boot project where I need to encrypt some properties in application.properties. I've tried using jasypt, but I quickly ran into a problem: How do I encrypt the property to put in the config? I found some webpages that would let me encrypt and decrypt, but they used the old algorithm instead of the new default PBEWITHHMACSHA512ANDAES_256.


Solution

  • You can use the mvn command to get the encrypted value to your config file.

    For example, if you want to encrypt the value mySecret place the value as a property in the application.properties file enclosed within brackets and prefixed by DEC.

    secret.property.example.value=DEC(mySecret)
    

    Now run the encrypt command with mvn by specifying the password.

    mvn jasypt:encrypt -Djasypt.encryptor.password="myPassword"
    

    Running the above command will generate the encrypted secret value and store it in the properties file. Your value would look something like this

    secret.property.example.value=ENC(mySecretEncrypted)
    

    You can directly read the value in the application with @Value annotation and the string you get there will already be decrypted.