1. Overview

In this article, we’ll be looking at the Jasypt (Java Simplified Encryption) library.

Jasypt is a Java library which allows developers to add basic encryption capabilities to projects with minimum effort, and without the need of having an in-depth knowledge about implementation details of encryption protocols.

2. Using Simple Encryption

Consider we’re building a web application in which user submits an account private data. We need to store that data in the database, but it would be insecure to store plain text.

One way to deal with it is to store an encrypted data in the database, and when retrieving that data for a particular user decrypt it.

To perform encryption and decryption using a very simple algorithm, we can use a BasicTextEncryptor class from the Jasypt library:

BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
String privateData = "secret-data";

Then we can use an encrypt() method to encrypt the plain text:

String myEncryptedText = textEncryptor.encrypt(privateData);
assertNotSame(privateData, myEncryptedText);

If we want to store a private data for given user in the database, we can store a myEncryptedText without violating any security restrictions. Should we want to decrypt data back to a plain text, we can use a decrypt() method:

String plainText = textEncryptor.decrypt(myEncryptedText);
assertEquals(plainText, privateData);

We see that decrypted data is equal to plain text data that was previously encrypted.

3. One-way Encryption

The previous example is not an ideal way to perform authentication, that is when we want to store a user password. Ideally, we want to encrypt the password without a way to decrypt it. When the user tries to log into our service, we encrypt his password and compare it with the encrypted password that is stored in the database. That way we do not need to operate on plain text password.

We can use a BasicPasswordEncryptor class to perform the one-way encryption:

String password = "secret-pass";
BasicPasswordEncryptor passwordEncryptor = new BasicPasswordEncryptor();
String encryptedPassword = passwordEncryptor.encryptPassword(password);

Then, we can compare an already encrypted password with a password of a user that perform login process without a need to decrypt password that is already stored in the database:

boolean result = passwordEncryptor.checkPassword("secret-pass", encryptedPassword);


4. Configuring Algorithm for Encryption

We can use a stronger encryption algorithm but we need to remember to install Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files for our JVM (installation instructions are included in the download).

In Jasypt we can use strong encryption by using a StandardPBEStringEncryptor class and customize it using a setAlgorithm() method:

StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
String privateData = "secret-data";

Let’s set the encryption algorithm to be PBEWithMD5AndTripleDES.

Next, the process of encryption and decryption looks the same as the previous one using a BasicTextEncryptor class:

String encryptedText = encryptor.encrypt(privateData);
assertNotSame(privateData, encryptedText);

String plainText = encryptor.decrypt(encryptedText);
assertEquals(plainText, privateData);

5. Using Multi-Threaded Decryption

When we’re operating on the multi-core machine we want to handle processing of decryption in parallel. To achieve a good performance we can use a PooledPBEStringEncryptor and the setPoolSize() API to create a pool of digesters. Each of them can be used by the different thread in parallel:

PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();

It’s good practice to set pool size to be equal to the number of cores of the machine. The code for encryption and decryption is the same as previous ones.

6. Usage in Other Frameworks

A quick final note is that the Jasypt library can be integrated with a lot of other libraries, including of course the Spring Framework.

We only need to create a configuration to add encryption support into our Spring application. And if we want to store sensitive data into the database and we are using Hibernate as the data access framework, we can also integrate Jasypt with it.

Instructions about these integrations, as well as with some other frameworks, can be found in the Guides section on the Jasypt’s home page.

7. Conclusion

In this article, we were looking at the Jasypt library that helps us create more secure applications by using an already well know and tested cryptography algorithms. It is covered with the simple API that is easy to use.

The implementation of all these examples and code snippets can be found in the GitHub project – this is a Maven project, so it should be easy to import and run as it is.

Course – LSS (cat=Security/Spring Security)

I just announced the new Learn Spring Security course, including the full material focused on the new OAuth2 stack in Spring Security:

res – Security (video) (cat=Security/Spring Security)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.