SpringBoot_DB_Password_Encryption_using_CastleBouncy
SpringBoot_DB_Password_Encryption_using_CastleBouncy
Overview
This project demonstrates how to encrypt and decrypt a database password using the Bouncy Castle
library in a Spring Boot application. The encrypted password is stored in the application.properties file and
decrypted at runtime using a key provided in a separate file.
Project Structure
- `application.properties`: Contains the encrypted password and path to the encryption key.
spring.datasource.url=jdbc:mysql://localhost:3306/testdb
spring.datasource.username=root
spring.datasource.password=ENC(6Vuz3rMTnP8=)
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
encryption.key.path=classpath:key.txt
@Configuration
public class PasswordDecryptionConfig {
@Value("${spring.datasource.password}")
private String encryptedPassword;
@Value("${encryption.key.path}")
private String keyPath;
private String decryptedPassword;
@Configuration
public class DataSourceConfig {
@Autowired
private PasswordDecryptionConfig passwordDecryptionConfig;
@Value("${spring.datasource.url}")
private String dbUrl;
@Value("${spring.datasource.username}")
private String dbUsername;
@Value("${spring.datasource.driver-class-name}")
private String driverClassName;
@Bean
public DataSource dataSource() {
return DataSourceBuilder.create()
.driverClassName(driverClassName)
.url(dbUrl)
.username(dbUsername)
.password(passwordDecryptionConfig.getDecryptedPassword())
.build();
}
}
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk18on</artifactId>
<version>1.80</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
Encryption Algorithm
AES algorithm from Bouncy Castle is used. Encrypted passwords are Base64 encoded.
Steps to Run
1. Open the project in your IDE (like IntelliJ or Eclipse).
3. Use the key from `key.txt` to encrypt your password and update `application.properties`.
Security Note
Never commit `key.txt` or decrypted passwords to source control. Keep them secure.
Enhancement
We can even fetch the secret key from an environment variable instead of file.
set ENCRYPTION_SECRET_KEY=my-secret-key-1234
2. Update application.properties:
encryption.secret.key=${ENCRYPTION_SECRET_KEY}
3. Modify PasswordDecryptionConfig.java:
@Configuration
public class PasswordDecryptionConfig {
@Value("${spring.datasource.password}")
private String encryptedPassword;
@Value("${encryption.secret.key}")
private String secretKey;
@PostConstruct
public void decryptPassword() {
try {
String password = encryptedPassword.replace("ENC(", "").replace(")", "");
this.decryptedPassword = EncryptionUtil.decrypt(password, secretKey);
} catch (Exception e) {
throw new RuntimeException("Failed to decrypt DB password", e);
}
}
}
If you want the secret key to persist and be accessible across sessions or reboots, you should set it
through System Environment Variables in Windows.
Follow the same steps but add it under System variables instead.
🔁 After Setting:
Restart your IDE or terminal to ensure the environment variable is picked up.
Spring Boot will now resolve ${ENCRYPTION_SECRET_KEY} in application.properties.
🧪 To verify:
echo %ENCRYPTION_SECRET_KEY%
And in Java:
System.out.println(System.getenv("ENCRYPTION_SECRET_KEY"));
Modify PasswordDecryptionConfig.java:
package com.example.demo.config;
import com.example.demo.util.EncryptionUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
@Configuration
public class PasswordDecryptionConfig {
@Value("${spring.datasource.password}")
private String encryptedPassword;
@PostConstruct
public void decryptPassword() {
try {
// Fetch the secret key from environment variables
String secretKey = System.getenv("ENCRYPTION_SECRET_KEY");