Secure Passwords In a Spring Boot Project Using Jasypt

Jasypt is a Java library which provides us to add basic encryption capabilities to projects with minimum effort and without writing any code !! 😀

let's see how we can do that…

Steps To Add Encryption Using Jasypt :

  1. Add Dependency in your pom.xml
  2. Add annotation in the Spring Boot Application main Configuration class
  3. Select a secret key to be used for encryption and decryption
  4. Generate Encrypted Key
  5. Add the encrypted key in the config file (application.properties)

1. Add Dependency in your pom.xml

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

2. Add annotation in the Spring Boot Application main Configuration class

@Configuration
@EnableEncryptableProperties
public class MyProject {

}

3. Select a secret key to be used for encryption and decryption

jasypt.encryptor.password=myprojectsecretKey

4. Generate Encrypted Key

java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input=”your_password” password=myprojectsecretKey algorithm=PBEWithMD5AndDES
Generated encrypted key

Here is the meaning of used parameter

input: It the password which you want to encrypt or hide. (your_password)
password: It is the secret key which is mentioned in application.properties and chosen by us.(myprojectsecretKey)
algorithm: It is the encryption algorithm used( PBEWithMD5AndDES).

5. Add the encrypted key in the config file (application.properties)

spring.datasource.mydatabase.password=ENC(vhnKrz7YdptHHbGB9HDMLZdwHiXZ66xa)

Spring boot will automatically decrypt it when you start the application.
You can use same secret key decrypt the password!

Run the application as usual …

🙌 Thanks for reading, hope it helps you 🙌

Love Programming and Writing, Software Engineer, Pune