This example is showing you how to use the Jasypt API to write a simple code to do string encryption and decryption. In this example we are going to use the BasicTextEncryptor
class which use the PBEWithMD5AndDES
algorithm. This class is an implementation of the TextEncryoptor
interface.
You can download the library from their website, it’s already included with the dependency libraries required by Jasypt such as the commons-codec
and commons-lang
.
package org.kodejava.jasypt;
import org.jasypt.util.text.BasicTextEncryptor;
public class TextEncryptorDemo {
public static void main(String[] args) {
String text = "The quick brown fox jumps over the lazy dog";
System.out.println("Text = " + text);
BasicTextEncryptor bte = new BasicTextEncryptor();
bte.setPassword("HelloWorld");
String encrypted = bte.encrypt(text);
System.out.println("Encrypted = " + encrypted);
String original = bte.decrypt(encrypted);
System.out.println("Original = " + original);
}
}
The result produced by the code above:
Text = The quick brown fox jumps over the lazy dog
Encrypted = kYXn3rL/YChh9EraGYh3cyuRxLo+dKocd+W33yW53TfgQecTpLRcIt5AH974d0YFDcFFXUTfNAk=
Original = The quick brown fox jumps over the lazy dog
Maven Dependencies
<dependency>
<groupId>org.jasypt</groupId>
<artifactId>jasypt</artifactId>
<version>1.9.3</version>
</dependency>
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024
BasicTextEncryptor
is unsecure it uses a weak algorithm. You should use eitherAES256TextEncryptor
orStrongTextEncryptor
from the same package. See RFC 8018 (PKCS#5 2.1) for more information.