How do I validate user’s password with PasswordEncryptor?

Every application that you’ll create may require an authentication process. This authentication process will at least contains a process of checking user’s login name and their password. To make the system reliable the password we usually stored the password in an encrypted form.

The BasicPasswordEncryptor which implements the PasswordEncryptor interface provide a BasicPasswordEncryptor.encryptPassword(String password) method for encrypting user’s password. To check if the user’s password is correct we use the BasicPasswordEncryptor.checkPassword(String plainText, String encryptedPassword) method.

package org.kodejava.jasypt;

import org.jasypt.util.password.BasicPasswordEncryptor;
import org.jasypt.util.password.PasswordEncryptor;

public class PasswordEncryptorDemo {
    public static void main(String[] args) {
        // Creates an instance of BasicPasswordEncryptor.
        PasswordEncryptor encryptor = new BasicPasswordEncryptor();

        // Encrypted version of user password.
        String encrypted = encryptor.encryptPassword("secret");
        System.out.println("encrypted = " + encrypted);

        // Compare user's plain text password with the encrypted one to check
        // if they are match.
        if (encryptor.checkPassword("secret", encrypted)) {
            System.out.println("Welcome to Jasypt");
        } else {
            System.out.println("Invalid secret word, access denied!");
        }
    }
}

Maven Dependencies

<dependency>
    <groupId>org.jasypt</groupId>
    <artifactId>jasypt</artifactId>
    <version>1.9.3</version>
</dependency>

Maven Central

How do I create a message digest?

Creating a digest of a string message can be easily done using the general digester class Digester. First we need to get an instance of Digester, we call the class constructor and pass SHA-1 as the algorithm. After having a Digester instance we create the message digest by executing or calling the Digester.digest(byte[] binary) method of this class.

package org.kodejava.jasypt;

import org.jasypt.util.digest.Digester;

import java.util.Arrays;

public class DigesterDemo {
    public static void main(String[] args) {
        // Creates a new instance of Digester, using the SHA-1 algorithm.
        Digester digester = new Digester("SHA-1");

        byte[] message = "Hello World from Jasypt".getBytes();

        // Creates a digest from an array of byte message.
        byte[] digest = digester.digest(message);

        System.out.println("Digest = " + new String(digest));
        System.out.println("Digest = " + Arrays.toString(digest));
    }
}

Maven Dependencies

<dependency>
    <groupId>org.jasypt</groupId>
    <artifactId>jasypt</artifactId>
    <version>1.9.3</version>
</dependency>

Maven Central

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.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 = [64, -99, -51, -21, -2, -6, -1, 76, -46, 87, 119, 62, -83, -74, 30, -90, 0, -97, 92, 0, 116, 32, -97, 67, -121, 57, -116, -11, 52, -73, 0, -104, -85, -109, -28, 106, 100, -76, -75, 55, -12, 49, 16, 65, -37, 88, 78, -68, 39, 14, 96, -22, 56, 63, -26, 18]
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]

Maven Dependencies

<dependency>
    <groupId>org.jasypt</groupId>
    <artifactId>jasypt</artifactId>
    <version>1.9.3</version>
</dependency>

Maven Central

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.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>

Maven Central