Symmetric Encryption in java

              Symmetric encryption is the process of using same key for both encryption and decryption.

Encryption: It is the process of converting plain text into ciphertext using an encryption key.

Decryption: It converts the ciphertext into original plain text using the same key.

Program implementation in java:

              Program implementation starts from reading input text from the user. A secret key is generated. The input is encrypted using the secret key. Same key is used for decryption.

Steps to follow:

  • Built-in packages javax.crypto.* ,java.util.Base64 and java.util.Scanner are included.
  • A public class is created with main() function.

Input reading:

  • Scanner object is created to read the system input.
  • Using this object, input text is read until the next line.

Secret key Generation:

  • KeyGenerator object is created to get instance for AES(Advanced Encryption Standard).
  • It is initialised as 128 bit.
  • generateKey() is used to generate secret key.

Encryption Process:

  • Cipher object is created and get assigned with instance of AES.
  • Cipher object is initialised with encrypted mode and secret key.
  • A byte of data is created from input text.
  • It gets encoded Base64.getEncoder(). The encrypted text is printed in the output screen.

Decryption Process:

  • Cipher object is initialised with Decrypted mode and secret key.
  • The data is decoded using Base64 decoder.
  • Finally, it displays in the output screen.          

Program:

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import javax.crypto.spec.SecretKeySpec;

import java.util.Base64;

import java.util.Scanner;

public class SymmetricEncryptionEg {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        // Get the input from the user

        System.out.println("Enter a string: ");

        String inputTxt = scanner.nextLine();  

        try {

            // A secret key is generated

            KeyGenerator keyGenerator1 = KeyGenerator.getInstance("AES");

            keyGenerator1.init(128);

            SecretKey secrKey = keyGenerator1.generateKey();

            // Let us Encrypt the input text

            Cipher cipher1 = Cipher.getInstance("AES");

            cipher1.init(Cipher.ENCRYPT_MODE, secrKey);

            byte[] encryptBytes = cipher1.doFinal(inputTxt.getBytes());

            String encryptTxt = Base64.getEncoder().encodeToString(encryptBytes);

            System.out.println("The Encrypted Text is: " + encryptTxt);

            // it is the process of Decrypting the encrypted text

            cipher1.init(Cipher.DECRYPT_MODE, secrKey);

            byte[] decryptBytes = cipher1.doFinal(Base64.getDecoder().decode(encryptTxt));

            String decryptTxt = new String(decryptBytes);

            System.out.println("The Decrypted Text is : " + decryptTxt);

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

Output:

C:\raji\blog>javac SymmetricEncryptionEg.java

C:\raji\blog>java SymmetricEncryptionEg

Enter a string:

Welcome to Cryptography in java

The Encrypted Text is: TK8+C2B1+DwJ+CyFPc2YhOn1uRRN4V9W6n1KAGgtNd0=

The Decrypted Text is : Welcome to Cryptography in java

This is the way of implementing Symmetric encryption in java. Keep coding!!!

No comments:

Post a Comment