java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to encrypt or decrypt bytes information using StandardPBEByteEncryptor?

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

package com.javacoderanch.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 result 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 = [-37, -99, 19, 33, -62, 17, -60, 32, 21, 84, 48, 76, -93, 18, 89, 36, -51, -121, -92, 66, -3, -22, 104, 103, 30, -25, -72, 13, 103, -30, 105, -22, -127, -41, 23, -79, 0, -71, -40, -102, 26, 9, 84, -124, 3, -19, 120, -23, 44, 41, -46, 68, -55, -97, -84, 102]
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]