Protect Your Sensitive Data with Java Encryption Decryption Project

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

In this project, we will be creating a simple encryption and decryption project using Java. The objective of this project is to familiarize the reader with the basics of encryption and decryption and to demonstrate how to create a working project that can encrypt and decrypt a given message using a user-specified key.

About Java Encryption Decryption

Encryption is a method of converting plain text into an unreadable format, called ciphertext, to protect sensitive information from unauthorized access. Decryption is the reverse process of encryption, which converts the ciphertext back into the original plain text.

The Caesar Cipher is a type of encryption algorithm that uses a fixed number of positions to shift each letter of the plaintext message. This number of positions is determined by the key, which is shared between the sender and the recipient to encrypt and decrypt the message.

For example, if the key is set at 3, the letter “A” in the plaintext message will be replaced by “D” in the ciphertext. To decrypt the message, the recipient simply shifts the letters back 3 positions to reveal the original plaintext. While the Caesar Cipher is considered to be a basic encryption method, it is still used in many educational and historical contexts.

Prerequisites for Encryption Decryption using Java

Before proceeding with this project, it is recommended that the reader has a basic understanding of the Java programming language, as well as the concepts of encryption and decryption. Additionally, it is assumed that the reader has a working Java development environment set up on their computer.

Download Java Encryption Decryption Project

Please download the source code of java Encryption Decryption project from the following link: Java Encryption Decryption Project Code

Steps to Create Encryption Decryption using Java

Following are the steps for developing the Java Encryption Decryption project:

Step 1: Importing Libraries

The first step in creating our encryption and decryption project is to import the necessary libraries. In this case, we will be using the javax.swing, java.awt and java.awt.event libraries.

package com.training.dataFlair;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

Step 2: Creating the Main Class

Next, we will create the main class for our project, which we will call EncryptionDecryption. This class will extend the JFrame class, which is part of the javax.swing library and provides a basic window frame for our project.
public class EncryptionDecryption extends JFrame{}

Step 3: Defining the GUI Elements

In this step, we will define the various GUI elements that will be used in our project. This includes text fields for input and key, a text area for output, radio buttons for encryption and decryption, and a button group to group the radio buttons together.

private JTextField inputField;   //text field for input
    private JTextField keyField;     //text field for key
    private JTextArea outputArea;    //text area for output
    private JRadioButton encryptRadio; //radio button for encrypt option
    private JRadioButton decryptRadio; //radio button for decrypt option
    private ButtonGroup radioGroup;  //group for radio buttons

Step 4: Setting up the GUI

In this step, we will set up the GUI for our project by creating a panel for input fields, adding the input field and key field to the panel, and adding the panel to the top of the frame. We will also create a panel for radio buttons, adding the encrypt and decrypt radio buttons to the panel and adding it to the left side of the frame. We will also add output area to the center and a button to perform the selected operation.

//setting up the GUI with input fields, radio buttons, and a button to perform the selected operation
    public EncryptionDecryption() {
        setTitle("Encryption/Decryption by Data-Flair");
        setSize(600, 300);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
     
       
        JPanel inputPanel = new JPanel();
        inputField = new JTextField(20);
        keyField = new JTextField(20);
       //Adds the input field and key field to the input panel and adds the input panel to the top of the frame
        inputPanel.add(new JLabel("Input: "));
        inputPanel.add(inputField);
        inputPanel.add(new JLabel("Key: "));
        inputPanel.add(keyField);
        add(inputPanel, BorderLayout.NORTH);

      //Adds the encrypt and decrypt radio buttons to the radio panel and adds it to left
        JPanel radioPanel = new JPanel();
        encryptRadio = new JRadioButton("Encrypt");
        decryptRadio = new JRadioButton("Decrypt");
        radioGroup = new ButtonGroup();
        radioGroup.add(encryptRadio);
        radioGroup.add(decryptRadio);
        radioPanel.add(encryptRadio);
        radioPanel.add(decryptRadio);
        add(radioPanel, BorderLayout.WEST);

        //Adds the output area to the center 
        outputArea = new JTextArea();
        add(new JScrollPane(outputArea), BorderLayout.CENTER);

      //Adds a button to perform the selected operation and an action listener to the button
        JButton performOperationButton = new JButton("Perform Selected Operation" );
        performOperationButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String input = inputField.getText();
                int key = Integer.parseInt(keyField.getText());
                String output = "";
                if (key < 1 || key > 25) {
                    JOptionPane.showMessageDialog(null, "Key must be between 1 and 25", "Invalid Key", JOptionPane.ERROR_MESSAGE);
                } else if (encryptRadio.isSelected()) {
                    output = encrypt(input, key);
               
                } else if (decryptRadio.isSelected()) {
                    output = decrypt(input, key);
                }
                outputArea.setText(output);
            }
        });
        add(performOperationButton, BorderLayout.SOUTH);
    }


Step 5: Implementing the Encryption Method

In this step, we will implement the encryption method, which will take in the input string and key value as parameters and return the encrypted string. The method uses a for loop to iterate through each character in the input string, and if the character is a letter, it shifts it by the key value. If the character goes beyond ‘Z’ or ‘z’ it bring it back by 26.

//Encrypt method
    private String encrypt(String input, int key) {
        String output = "";
 
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            //If the character is a letter, shift it by the key value, if it goes beyond 'Z' or 'z' bring it back by 26
            if (Character.isLetter(c)) {
                c = (char)(c + key);
                if (Character.isUpperCase(input.charAt(i)) && c > 'Z' ||
                    Character.isLowerCase(input.charAt(i)) && c > 'z') {
                    c = (char)(c - 26);
                }
            }
            output += c;
        }
        return output;
    }

Step 6: Implementing the Decryption Method

In this step, we will implement the decryption method, which is similar to the encryption method, but instead of shifting the characters by the key value, it shifts them back by the key value.

// Decrypt method
    private String decrypt(String input, int key) {
        return encrypt(input, 26 - key);
    }

Step 7: Now to run the code you need to call the JFrame and make it visible by adding

//Main method creates an instance of the EncryptionDecryption class and sets it to be visible
    public static void main(String[] args) {
        EncryptionDecryption ed = new EncryptionDecryption();
        ed.setVisible(true);
    }


at the end of the class.
In conclusion, this project provided a basic overview of how to create a simple encryption and decryption project using Java. It covered the steps involved in creating a GUI, implementing the encryption and decryption methods..

Java Encryption Decryption Output

java encryption decryption output

Summary

In conclusion, this project provided a basic overview of how to create a simple encryption and decryption project using Java. It covered the steps involved in creating a GUI and implementing the encryption and decryption methods. We have implemented a Java GUI program that performs encryption and decryption using the Caesar Cipher technique. The program has a text field for input, a text field for key, a radio button to select either encryption or decryption, and a button to perform the selected operation.

The encryption and decryption methods shift the letters of the input by the key value, and the decrypt method uses the encrypt method with the key being 26 minus the original key. The output of the selected operation is displayed in a text area.

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

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