Encryption Tool using java with complete source Code GUI

Introduction:
The Encryption Tool is a Java-based GUI application designed to help users encrypt and decrypt text using fundamental cryptographic techniques. It provides a simple yet effective way to secure sensitive information, making it ideal for beginners in cryptography, students, and anyone looking for a lightweight encryption solution. The tool supports Caesar Cipher and XOR Encryption, offering users a choice between a shift-based method and a bitwise operation-based method for securing their data.
Features of Encryption Tool
- Graphical User Interface (GUI): Provides an intuitive and easy-to-use interface for encryption and decryption.
- Multiple Encryption Methods: Supports both Caesar Cipher (shift-based encryption) and XOR Encryption (bitwise operation-based encryption).
- Customizable Key Input: Users can specify encryption keys for better security.
- Real-time Processing: Encrypts and decrypts text instantly with a single click.
- Maintains Text Integrity: Preserves non-alphabetic characters during encryption and decryption.
Required Modules:
javax.swing.*
– Provides components for building the graphical user interface (GUI), such as buttons, text areas, labels, and panels.java.awt.*
– Contains classes for managing layout, colors, and fonts in the GUI.java.awt.event.ActionEvent
– Handles action events like button clicks.java.awt.event.ActionListener
– Used to listen and respond to user interactions.
How to Run the Code:
Prerequisites:
✔ Java Development Kit (JDK 8 or later) installed.
✔ A code editor (such as VS Code, IntelliJ IDEA, or Eclipse) or a terminal/command prompt to run the code.
Steps to Run the Code:
1. Install Java (If Not Installed)
If Java is not installed, download and install the latest JDK from:
🔗 Oracle JDK Download
or
🔗 OpenJDK Download
After installation, verify it by running:
java -version
javac -version
You should see output confirming the Java version.
2. Save the Java Code
- Open a text editor or IDE.
- Copy the Java code and save it as
EncryptionTool.java
in a folder.
3. Compile the Java Program
Open a terminal or command prompt, navigate to the folder where the file is saved, and run:
javac EncryptionTool.java
This will compile the code and generate a EncryptionTool.class
file.
4. Run the Java Program
After successful compilation, execute the program using:
java EncryptionTool
This will launch the GUI-based Encryption Tool, where you can enter text, choose encryption methods, and encrypt/decrypt messages.
Running in an IDE (Eclipse, IntelliJ, VS Code)
- Open your IDE and create a new Java project.
- Add a new Java class and paste the code.
- Click Run or use the shortcut
Shift + F10
(IntelliJ) /Ctrl + F11
(Eclipse).
Code Explanation :
Main Components:
EncryptionTool
Class:- This is the main class that starts the program. It uses
SwingUtilities.invokeLater()
to ensure that the GUI-related code runs on the Event Dispatch Thread (EDT), which is the appropriate thread for handling GUI updates in Swing.
- This is the main class that starts the program. It uses
EncryptionGUI
Class:- This class extends
JFrame
and creates the main GUI window. - It contains:
JTextArea
(inputTextArea
): For the user to input text to encrypt or decrypt.JTextArea
(outputTextArea
): To display the encrypted or decrypted output (non-editable).JComboBox
(encryptionMethod
): A dropdown to select between the Caesar Cipher or XOR Encryption methods.JTextField
(keyField
): To enter the encryption key. For Caesar Cipher, it’s a number, and for XOR Encryption, it’s a single character.JButton
s (encryptButton
anddecryptButton
): Buttons to trigger encryption or decryption actions.
- This class extends
GUI Setup:
setLayout(new BorderLayout())
: This sets the layout for the main frame, dividing it into regions (North, South, Center).topPanel
(GridLayout): A panel at the top that contains:- A label and the
inputTextArea
for the user to input the text. - A label and
keyField
for entering the key. - A dropdown (
encryptionMethod
) for choosing the encryption method.
- A label and the
outputTextArea
: The central area where the encrypted/decrypted text will be shown.buttonPanel
: A panel at the bottom containing the “Encrypt” and “Decrypt” buttons.- When clicked, these buttons call the
processText()
method to perform the encryption or decryption.
- When clicked, these buttons call the
Event Handling:
encryptButton.addActionListener(e -> processText(true))
: This triggers the encryption process when the “Encrypt” button is clicked.decryptButton.addActionListener(e -> processText(false))
: This triggers the decryption process when the “Decrypt” button is clicked.
Encryption/Decryption Logic:
processText(boolean isEncrypt)
:- This method is responsible for handling the encryption and decryption logic based on the selected method and key.
- It checks if the selected method is “Caesar Cipher” or “XOR Encryption” and then calls the respective method to perform the encryption or decryption.
Caesar Cipher:
- A classical substitution cipher where each letter is shifted by a certain number (key).
- In the
caesarCipher
method, the text is iterated character by character:- If the character is a letter, it’s shifted by the specified key (or reversed if decrypting).
- If it’s not a letter (like punctuation), it remains unchanged.
- The formula for shifting is
(c - base + shift + 26) % 26 + base
, where:base
is'A'
or'a'
depending on whether the character is uppercase or lowercase.shift
is the key value that can be positive or negative.
XOR Encryption:
- This is a bitwise XOR encryption. Each character in the text is XOR’d with the key (which is a single character in this case).
- The
xorCipher
method performs this by iterating over each character in the text, applying the XOR operation (c ^ key
), and storing the result.
Encryption/Decryption Output:
- The result of the encryption or decryption process is set to the
outputTextArea
for display.
Source Code:
Get Discount on Top Educational Courses
import javax.swing.*;
import java.awt.*;
public class EncryptionTool {
public static void main(String[] args) {
SwingUtilities.invokeLater(EncryptionGUI::new);
}
}
class EncryptionGUI extends JFrame {
private JTextArea inputTextArea, outputTextArea;
private JComboBox encryptionMethod;
private JTextField keyField;
public EncryptionGUI() {
setTitle("Encryption Tool");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel topPanel = new JPanel(new GridLayout(3, 2));
topPanel.add(new JLabel("Enter Text:"));
inputTextArea = new JTextArea(3, 20);
topPanel.add(new JScrollPane(inputTextArea));
topPanel.add(new JLabel("Key (number for Caesar, char for XOR):"));
keyField = new JTextField();
topPanel.add(keyField);
encryptionMethod = new JComboBox(new String[]{"Caesar Cipher", "XOR Encryption"});
topPanel.add(new JLabel("Method:"));
topPanel.add(encryptionMethod);
add(topPanel, BorderLayout.NORTH);
outputTextArea = new JTextArea(3, 20);
outputTextArea.setEditable(false);
add(new JScrollPane(outputTextArea), BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
JButton encryptButton = new JButton("Encrypt");
JButton decryptButton = new JButton("Decrypt");
buttonPanel.add(encryptButton);
buttonPanel.add(decryptButton);
add(buttonPanel, BorderLayout.SOUTH);
encryptButton.addActionListener(e -> processText(true));
decryptButton.addActionListener(e -> processText(false));
setVisible(true);
}
private void processText(boolean isEncrypt) {
String text = inputTextArea.getText();
String keyText = keyField.getText();
String method = (String) encryptionMethod.getSelectedItem();
String result = "";
if (method.equals("Caesar Cipher")) {
int key = Integer.parseInt(keyText);
result = isEncrypt ? caesarCipher(text, key) : caesarCipher(text, -key);
} else if (method.equals("XOR Encryption")) {
char key = keyText.charAt(0);
result = xorCipher(text, key);
}
outputTextArea.setText(result);
}
private String caesarCipher(String text, int shift) {
StringBuilder result = new StringBuilder();
for (char c : text.toCharArray()) {
if (Character.isLetter(c)) {
char base = Character.isLowerCase(c) ? 'a' : 'A';
result.append((char) ((c - base + shift + 26) % 26 + base));
} else {
result.append(c);
}
}
return result.toString();
}
private String xorCipher(String text, char key) {
StringBuilder result = new StringBuilder();
for (char c : text.toCharArray()) {
result.append((char) (c ^ key));
}
return result.toString();
}
}
Output:

