How to write a simple Spring Boot Application to encrypt and decrypt?

Here I will be using crypto library from BouncyCastleProvider and implement it in spring boot application. Find below the simplest code that I have attached even for beginners to follow through.

package demo;

import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.Scanner;

public class EncryptAndDecrypt {

    public static void main(String[] args) {

        SimpleStringPBEConfig simpleStringPBEConfig = new SimpleStringPBEConfig();
        simpleStringPBEConfig.setAlgorithm("PBEWITHSHA256AND256BITAES-CBC-BC"); // See below for what this algorithm means.

        PooledPBEStringEncryptor pooledPBEStringEncryptor = new PooledPBEStringEncryptor();

        Scanner userInput = new Scanner(;

        System.out.println("Press :   E    to Encrypt a text and hit Enter.   ");
        System.out.println("Press :   D   to Decrypt a text and hit Enter.   ");

        String encryptOrDecrypt = userInput.nextLine();

        if (encryptOrDecrypt.equals("E")) {

            System.out.println("Enter text that you want to encrypt below: ");

            String textToEncrypt = userInput.nextLine();

            System.out.println("Your Encrypted ENC() Text is:    " + "ENC(" + pooledPBEStringEncryptor.encrypt(textToEncrypt) + ")");

        } else if (encryptOrDecrypt.equals("D")) {

            System.out.println("Enter text that you want to decrypt below: ");

            String encryptedTextToDecrypt = userInput.nextLine();

            if (encryptedTextToDecrypt.startsWith("ENC(")) {

                try {
                    System.out.println("Your Decrypted ENC() Text is:    " + pooledPBEStringEncryptor.decrypt(encryptedTextToDecrypt.substring(3, encryptedTextToDecrypt.length() - 1)));
                } catch (Exception e) {
                    //Your custom exception

            } else {

                try {
                    System.out.println("Your Decrypted ENC() Text is:    " + pooledPBEStringEncryptor.decrypt(encryptedTextToDecrypt));
                } catch (Exception e) {
                    //Your custom exception




Dependency Used :


Alright so above code and dependency should help you understand further.

Now briefly as promised above on the code comment I will explain what the Algorithm text stands for:


PBE = Password-Based Encryption

WITHSHA256 = SHA-256 is a modern hash algorithm, and is used to demonstrate that the data can’t be modified by anyone with having the password at hand. 256-bit hashes are the up-to-date standard, with 512-bit hashes.

AND256BITAES = AES-256 is a modern encryption algorithm. The 256-bit key size is for those who want just a tiny bit extra guarantee, even however a 128-bit key size can be adequate for many practices.

CBC = The CBC mode for encryption is the default choice for turning a block cipher into a stream cipher.

BC = Crypto provider library aka BouncyCastle.

Once again there are other encryption methods and providers. Choose wisely based on your or project needs.

Leave a Reply