I just announced the new Spring 5 modules in REST With Spring:
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"; textEncryptor.setPasswordCharArray("some-random-data".toCharArray());
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); assertTrue(result);
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"; encryptor.setPassword("some-random-passwprd"); encryptor.setAlgorithm("PBEWithMD5AndTripleDES");
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(); encryptor.setPoolSize(4); encryptor.setPassword("some-random-data"); encryptor.setAlgorithm("PBEWithMD5AndTripleDES");
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.
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.