Java

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;

@SpringBootApplication
public class EncryptAndDecrypt {

    public static void main(String[] args) {

        SimpleStringPBEConfig simpleStringPBEConfig = new SimpleStringPBEConfig();
        simpleStringPBEConfig.setPassword("yourKey");
        simpleStringPBEConfig.setAlgorithm("PBEWITHSHA256AND256BITAES-CBC-BC"); // See below for what this algorithm means.
        simpleStringPBEConfig.setKeyObtentionIterations("500");
        simpleStringPBEConfig.setPoolSize("1");
        simpleStringPBEConfig.setStringOutputType("base64");
        simpleStringPBEConfig.setProviderClassName("org.bouncycastle.jce.provider.BouncyCastleProvider");
        simpleStringPBEConfig.setProviderName("BC");

        PooledPBEStringEncryptor pooledPBEStringEncryptor = new PooledPBEStringEncryptor();
        pooledPBEStringEncryptor.setConfig(simpleStringPBEConfig);

        Scanner userInput = new Scanner(System.in);

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

        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 :

        <dependency>
            <groupId>com.github.ulisesbocchio</groupId>
            <artifactId>jasypt-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.jasypt</groupId>
            <artifactId>jasypt-spring4</artifactId>
            <version>1.9.3</version>
        </dependency>
        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcpkix-jdk15on</artifactId>
            <version>1.64</version>
        </dependency>

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:

PBEWITHSHA256AND256BITAES-CBC-BC

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

Your email address will not be published. Required fields are marked *