How do I encrypt or decrypt bytes information using StandardPBEByteEncryptor?

This code demonstrate how to use the StandardPBEByteEncryptor class to encrypt and decrypt bytes information.

package org.kodejava.example.jasypt;

import org.jasypt.encryption.pbe.StandardPBEByteEncryptor;

import java.util.Arrays;

public class ByteEncryptorDemo {
    public static void main(String[] args) {
        String text = "The quick brown fox jumps over the lazy dog";
        System.out.println("Text      = " + Arrays.toString(text.getBytes()));

        StandardPBEByteEncryptor encryptor = new StandardPBEByteEncryptor();
        encryptor.setAlgorithm("PBEWithMD5AndDES");
        encryptor.setPassword("HelloWorld");

        byte[] encrypted = encryptor.encrypt(text.getBytes());
        System.out.println("Encrypted = " + Arrays.toString(encrypted));

        byte[] original = encryptor.decrypt(encrypted);
        System.out.println("Original  = " + Arrays.toString(original));
    }
}

The results of our code are:

Text      = [84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]
Encrypted = [-63, -127, 0, 50, 98, 79, 58, 20, 54, -13, -104, -80, 21, 100, 42, 27, 121, 84, 46, 107, -37, 119, -107, -25, 118, -46, -128, -31, -20, -114, 34, -121, 66, 105, 45, 38, 79, 90, 41, -128, -128, -48, 113, 69, -10, 63, 95, -43, 46, -10, 57, -61, -16, 49, -57, -120]
Original  = [84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]

How do I use BasicTextEncryptor for encrypting and decrypting string information?

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.example.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 = blke1B2qZJyeqOy07En7yAu3vFTFKIiJ3F3gAHb+2rOs/eBPqbvB6HK4j9LwG/E7VETRvQK3VIs=
Original  = The quick brown fox jumps over the lazy dog