The Advanced Cipher Tool is a robust and user-friendly encryption and decryption utility built using Java Swing. This Java encryption tool source code with GUI allows users to secure their text using different cryptographic techniques, including the Caesar Cipher, XOR Cipher, and Vigenère Cipher. The tool features a modern graphical user interface (GUI), a dark mode toggle, file handling capabilities, and clipboard integration.
Features of Advanced Cipher Tool
- Multiple Encryption Algorithms: Supports three encryption methods—Caesar Cipher, XOR Cipher, and Vigenère Cipher.
- Graphical User Interface (GUI): Built using Java Swing with an intuitive layout.
- Dark Mode Support: Switch between light and dark themes for better visibility.
- Clipboard Integration: Copy the encrypted/decrypted text with a single click.
- File Handling: Load input from a file and save output to a file.
- Real-time Status Updates: A status bar provides instant feedback on user actions.
Note
You can get its code from my GitHub repository
You can also download the Zip File of its source code
Details of How to create an encryption GUI in Java
1. GUI Design
The user interface is built using Java Swing, a popular framework for desktop applications. The layout ensures a clean and organized appearance.
- JTextArea for Input & Output
- The input text area allows users to enter plain text (or encrypted text for decryption).
- The output text area displays the result after encryption or decryption.
- The output field is set to non-editable to prevent accidental modifications.
- JComboBox for Algorithm Selection
- A dropdown list contains Caesar Cipher, XOR Cipher, and Vigenère Cipher as selectable encryption methods.
- When the user chooses an algorithm, the program determines which encryption logic to apply.
- JTextField for Encryption Key
- A user-provided key is essential for both encryption and decryption.
- For the Caesar Cipher, the key represents a numerical shift.
- For the XOR and Vigenère Ciphers, the key is a string used in complex transformations.
- JButtons for Actions
- The application includes buttons for:
- Encrypting the text
- Decrypting the text
- Copying the output to the clipboard
- Loading a file into the input field
- Saving the output to a file
- Clearing all fields
- The application includes buttons for:
- JCheckBox for Dark Mode
- Users can toggle between light and dark themes.
- The color scheme dynamically updates the background and text colors for better visibility.
- JLabel as a Status Bar
- Displays real-time messages such as “Encryption successful,” “File loaded successfully,” or “Invalid input” to enhance usability.

2. Implementation Cipher Algorithms in java
The tool implements three encryption techniques:
a) Caesar Cipher
A simple substitution cipher that shifts letters by a fixed number (the key).Each letter in the text is replaced by another letter N places away in the alphabet.
Example:
- Text:
"HELLO"
- Shift Key:
3
- Encrypted Text:
"KHOOR"

Edge Cases Handled:
- Wraps around at the end of the alphabet (Z → A).
- Ignores non-alphabetic characters (spaces, numbers, symbols remain unchanged).
private String caesarCipher(String text, String key, boolean encrypt) {
int shift = Integer.parseInt(key);
if (!encrypt) shift = -shift;
StringBuilder result = new StringBuilder();
for (char c : text.toCharArray()) {
if (Character.isLetter(c)) {
char base = Character.isUpperCase(c) ? 'A' : 'a';
result.append((char) (base + (c - base + shift + 26) % 26));
} else {
result.append(c);
}
}
return result.toString();
}
b) XOR encryption in Java
Uses a bitwise XOR operation between the text and a key. Each character in the text is XOR-ed with the corresponding character from the key. The same operation is used for both encryption and decryption, making it symmetric.
Example:
- Text:
"HELLO"
- Key:
"KEY"
(repeated if shorter than the text) - Encrypted Text:
"$%&'"
(non-readable output)
Edge Cases Handled:
- The key loops if it’s shorter than the text.
- Works on all character types, including spaces and symbols.
private String xorCipher(String text, String key) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
result.append((char) (text.charAt(i) ^ key.charAt(i % key.length())));
}
return result.toString();
}
c) Vigenère cipher implementation in Java
A polyalphabetic substitution cipher where letters are shifted based on a repeating key sequence.A polyalphabetic substitution cipher that shifts letters based on a keyword.
Each letter in the text is shifted by the corresponding letter in the key.
Unlike the Caesar Cipher, which uses a fixed shift, this uses a varying shift.
Example:
- Text:
"HELLO"
- Key:
"KEY"
(repeats if shorter than the text) - Shift Values:
K=10, E=4, Y=24
- Encrypted Text:
"RIJVS"

Edge Cases Handled:
- The key wraps around if shorter than the text.
- Non-alphabetic characters remain unchanged.
private String vigenereCipher(String text, String key, boolean encrypt) {
StringBuilder result = new StringBuilder();
int keyIndex = 0;
for (char c : text.toCharArray()) {
char base = Character.isUpperCase(c) ? 'A' : 'a';
int shift = key.charAt(keyIndex % key.length()) - base;
shift = encrypt ? shift : -shift;
result.append((char) (base + (c - base + shift + 26) % 26));
keyIndex++;
}
return result.toString();
}
Java file Encryption and Decryption Workflow
Displays a status message if input is missing or incorrect.
Encryption Process
- The user enters text and selects an algorithm.
- The key is entered (validated for correctness).
- The encryption method is applied, transforming the text.
- The result appears in the output area.
Decryption Process
- The user enters encrypted text and the correct key.
- The corresponding decryption logic is applied.
- The original text is restored in the output area.
Validation and Error Handling
- Ensures that the user provides both text and a valid key before proceeding.
- Displays a status message if input is missing or incorrect.
3. Clipboard Integration
The tool allows users to copy the encrypted/decrypted text to the clipboard for easy sharing. The Copy Output button allows users to quickly copy the encrypted or decrypted text. Uses the Java AWT Clipboard API to store the text and make it available for pasting elsewhere. If the output area is empty, a message “No text to copy” is displayed.
private void copyToClipboard() {
String text = outputText.getText();
if (!text.isEmpty()) {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection selection = new StringSelection(text);
clipboard.setContents(selection, null);
statusBar.setText("Text copied to clipboard!");
} else {
statusBar.setText("No text to copy.");
}
}
4. File Handling in java
The application allows users to load text from a file and save the encrypted/decrypted output to a file.
Loading a File
private void loadFile() {
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
inputText.read(reader, null);
statusBar.setText("File loaded successfully.");
} catch (IOException e) {
statusBar.setText("Error loading file.");
}
}
}
Saving a File
private void saveFile() {
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
writer.write(outputText.getText());
statusBar.setText("File saved successfully.");
} catch (IOException e) {
statusBar.setText("Error saving file.");
}
}
}
5. Dark Mode Implementation in java
The GUI theme can be toggled between dark and light modes.
private void toggleTheme() {
isDarkMode = themeToggle.isSelected();
updateTheme();
}
private void updateTheme() {
Color backgroundColor = isDarkMode ? new Color(45, 45, 45) : Color.WHITE;
Color textColor = isDarkMode ? Color.WHITE : Color.BLACK;
getContentPane().setBackground(backgroundColor);
inputText.setBackground(backgroundColor);
outputText.setBackground(backgroundColor);
inputText.setForeground(textColor);
outputText.setForeground(textColor);
}
Conclusion
The Advanced Cipher Tool provides an interactive and visually appealing way to encrypt and decrypt text. By integrating different ciphers, file handling, clipboard support, and a dark mode, it ensures ease of use and versatility.
With further enhancements, additional encryption methods like AES or RSA could be added to improve security. The tool serves as an excellent learning project for cryptography enthusiasts and Java developers alike.
Java encryption tool source code with GUI
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.io.*;
class AdvancedCipherTool extends JFrame {
private JTextArea inputText, outputText;
private JComboBox<String> algorithmSelector;
private JTextField keyField;
private JButton encryptButton, decryptButton, copyButton, loadButton, saveButton, clearButton;
private JLabel statusBar;
private JCheckBox themeToggle;
private boolean isDarkMode = false;
public AdvancedCipherTool() {
setTitle("Advanced Encryption Tool");
setSize(900, 650);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout(10, 10));
setLocationRelativeTo(null);
initComponents();
addComponents();
addListeners();
updateTheme();
}
private void initComponents() {
inputText = new JTextArea(10, 40);
outputText = new JTextArea(10, 40);
outputText.setEditable(false);
algorithmSelector = new JComboBox<>(new String[]{"Caesar Cipher", "XOR Cipher", "Vigenere Cipher"});
keyField = new JTextField(15);
encryptButton = new JButton("Encrypt");
decryptButton = new JButton("Decrypt");
copyButton = new JButton("Copy Output");
loadButton = new JButton("Load File");
saveButton = new JButton("Save File");
clearButton = new JButton("Clear Fields");
themeToggle = new JCheckBox("Dark Mode");
statusBar = new JLabel("Ready", SwingConstants.CENTER);
setButtonStyle(encryptButton);
setButtonStyle(decryptButton);
setButtonStyle(copyButton);
setButtonStyle(loadButton);
setButtonStyle(saveButton);
setButtonStyle(clearButton);
}
private void addComponents() {
JPanel textPanel = new JPanel(new GridLayout(1, 2, 10, 0));
textPanel.add(createTextAreaPanel("Input Text:", inputText));
textPanel.add(createTextAreaPanel("Output Text:", outputText));
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.Y_AXIS));
controlPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JPanel topControls = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
topControls.add(new JLabel("Algorithm:"));
topControls.add(algorithmSelector);
topControls.add(new JLabel("Key:"));
topControls.add(keyField);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
buttonPanel.add(encryptButton);
buttonPanel.add(decryptButton);
buttonPanel.add(copyButton);
buttonPanel.add(loadButton);
buttonPanel.add(saveButton);
buttonPanel.add(clearButton);
buttonPanel.add(themeToggle);
controlPanel.add(topControls);
controlPanel.add(buttonPanel);
add(textPanel, BorderLayout.CENTER);
add(controlPanel, BorderLayout.SOUTH);
add(statusBar, BorderLayout.NORTH);
}
private JPanel createTextAreaPanel(String title, JTextArea textArea) {
JPanel panel = new JPanel(new BorderLayout());
JLabel label = new JLabel(title, SwingConstants.CENTER);
label.setFont(new Font("Arial", Font.BOLD, 14));
panel.add(label, BorderLayout.NORTH);
panel.add(new JScrollPane(textArea), BorderLayout.CENTER);
return panel;
}
private void addListeners() {
encryptButton.addActionListener(e -> performOperation(true));
decryptButton.addActionListener(e -> performOperation(false));
copyButton.addActionListener(e -> copyToClipboard());
loadButton.addActionListener(e -> loadFile());
saveButton.addActionListener(e -> saveFile());
clearButton.addActionListener(e -> clearFields());
themeToggle.addActionListener(e -> toggleTheme());
}
private void setButtonStyle(JButton button) {
button.setFocusPainted(false);
button.setBackground(new Color(30, 144, 255));
button.setForeground(Color.WHITE);
button.setBorder(BorderFactory.createEmptyBorder(5, 15, 5, 15));
}
private void toggleTheme() {
isDarkMode = themeToggle.isSelected();
updateTheme();
}
private void updateTheme() {
Color backgroundColor = isDarkMode ? new Color(45, 45, 45) : Color.WHITE;
Color textColor = isDarkMode ? Color.WHITE : Color.BLACK;
getContentPane().setBackground(backgroundColor);
inputText.setBackground(backgroundColor);
outputText.setBackground(backgroundColor);
inputText.setForeground(textColor);
outputText.setForeground(textColor);
}
private void performOperation(boolean isEncrypt) {
String text = inputText.getText();
String key = keyField.getText();
String algorithm = (String) algorithmSelector.getSelectedItem();
if (text.isEmpty() || key.isEmpty()) {
statusBar.setText("Please enter text and key");
return;
}
if (algorithm.equals("Caesar Cipher")) {
outputText.setText(isEncrypt ? caesarCipher(text, key, true) : caesarCipher(text, key, false));
} else if (algorithm.equals("XOR Cipher")) {
outputText.setText(xorCipher(text, key));
} else if (algorithm.equals("Vigenere Cipher")) {
outputText.setText(isEncrypt ? vigenereCipher(text, key, true) : vigenereCipher(text, key, false));
}
}
private String caesarCipher(String text, String key, boolean encrypt) {
int shift = Integer.parseInt(key);
if (!encrypt) shift = -shift;
StringBuilder result = new StringBuilder();
for (char c : text.toCharArray()) {
if (Character.isLetter(c)) {
char base = Character.isUpperCase(c) ? 'A' : 'a';
result.append((char) (base + (c - base + shift + 26) % 26));
} else {
result.append(c);
}
}
return result.toString();
}
private String xorCipher(String text, String key) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
result.append((char) (text.charAt(i) ^ key.charAt(i % key.length())));
}
return result.toString();
}
private String vigenereCipher(String text, String key, boolean encrypt) {
StringBuilder result = new StringBuilder();
int keyIndex = 0;
for (char c : text.toCharArray()) {
char base = Character.isUpperCase(c) ? 'A' : 'a';
int shift = key.charAt(keyIndex % key.length()) - base;
shift = encrypt ? shift : -shift;
result.append((char) (base + (c - base + shift + 26) % 26));
keyIndex++;
}
return result.toString();
}
private void copyToClipboard() {
String text = outputText.getText();
if (!text.isEmpty()) {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection selection = new StringSelection(text);
clipboard.setContents(selection, null);
statusBar.setText("Text copied to clipboard!");
} else {
statusBar.setText("No text to copy.");
}
}
private void loadFile() {
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
inputText.read(reader, null);
statusBar.setText("File loaded successfully.");
} catch (IOException e) {
statusBar.setText("Error loading file.");
}
}
}
private void saveFile() {
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
writer.write(outputText.getText());
statusBar.setText("File saved successfully.");
} catch (IOException e) {
statusBar.setText("Error saving file.");
}
}
}
private void clearFields() {
inputText.setText("");
outputText.setText("");
keyField.setText("");
statusBar.setText("Fields cleared.");
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new AdvancedCipherTool().setVisible(true));
}
}
Interesting project