Jasypt for Java: Complete Guide to Encrypting Configuration Files
Jasypt (Java Simplified Encryption) is a powerful yet easy-to-use library for encryption in Java applications. It provides developers with a convenient API for performing cryptographic operations such as encrypting and decrypting text, numbers, binary data, and—most importantly—configuration files. Jasypt's main goal is to make encryption as transparent and easy to integrate as possible, without requiring deep knowledge of cryptography.
Why do you need Jasypt? In modern applications, data security is critical. Configuration files (properties, YAML, XML) often contain sensitive information: database passwords, API keys, authentication tokens, and connection strings to third-party services. Storing this data in plain text poses a huge security risk. Jasypt solves this problem by allowing you to encrypt confidential values directly in configuration files. When the application starts, the library automatically decrypts them, making the process secure and seamless for the developer.
Jasypt's key advantage is its integration with popular frameworks like Spring Framework (via the jasypt-spring-boot module). This lets you use annotations and configurations for automatic encryption/decryption of properties. Additionally, Jasypt supports various encryption algorithms (PBEWithMD5AndDES, PBEWithSHA1AndDESede, PBEWithSHA256AndAES, and others), giving you flexibility in choosing your security level. The library also includes utilities for working with keys, salts, and initialization vectors, making it a complete tool for data security in the Java ecosystem.
Installing Jasypt
Jasypt is distributed through Maven Central. To add the library to your project, use Maven or Gradle build systems.
Maven
<dependency>
<groupId>org.jasypt</groupId>
<artifactId>jasypt</artifactId>
<version>1.9.3</version>
</dependency>
Gradle
implementation 'org.jasypt:jasypt:1.9.3'
For Spring Boot integration, add:
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
Quick Start: Minimal Working Example
Let's create a simple application that encrypts and decrypts a string using Jasypt.
import org.jasypt.util.text.BasicTextEncryptor;
public class QuickStartExample {
public static void main(String[] args) {
// 1. Create the encryptor
BasicTextEncryptor encryptor = new BasicTextEncryptor();
// 2. Set the master password (secret key)
encryptor.setPassword("mySecretKey123");
// 3. Encrypt the text
String plainText = "Hello, Jasypt!";
String encryptedText = encryptor.encrypt(plainText);
System.out.println("Encrypted text: " + encryptedText);
// 4. Decrypt the text
String decryptedText = encryptor.decrypt(encryptedText);
System.out.println("Decrypted text: " + decryptedText);
// Verification
System.out.println("Success: " + plainText.equals(decryptedText));
}
}
This example demonstrates the basic Jasypt workflow: creating an encryptor, setting a password, encrypting, and decrypting. Note that the encrypted text looks like a random string of characters.
Key Jasypt Methods and Classes
Below are 8 key classes and methods from the Jasypt library with detailed descriptions and code examples.