DES Encryption Program Code Using Java JFrame

DES Encryption Program Code Using Java JFrame

DES Encryption Program Using Java JFrame and Swing

A simple Java Swing application that implements DES (Data Encryption Standard) encryption and decryption using javax.crypto libraries. This program uses JFrame for the GUI.

Explanation:

  1. GUI Components:
  • The application consists of a simple Swing GUI with fields to input the text, key, and an output area to display the encrypted or decrypted text.
  • JTextField is used for inputting the text and key.
  • JTextArea is used for displaying the output.
  • Two buttons (JButton) are provided for encryption and decryption.
  1. Encryption and Decryption:
  • The encrypt method uses Cipher to encrypt the input text with the provided DES key. The encrypted data is then encoded using Base64 to make it readable.
  • The decrypt method performs the reverse process, decoding the Base64-encoded string and decrypting it back to its original form.
  1. Key Validation:
  • The DES key must be exactly 8 characters long, which is a requirement for DES encryption. The application checks this and prompts the user if the key length is invalid.

DES Encryption Program Using JFrame and Swing

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Base64;

public class DESEncryptionGUI extends JFrame {

    private JTextField inputTextField;
    private JTextField keyTextField;
    private JTextArea outputTextArea;
    private JButton encryptButton;
    private JButton decryptButton;

    private SecretKey secretKey;

    public DESEncryptionGUI() {
        setTitle("DES Encryption/Decryption");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(5, 2));

        panel.add(new JLabel("Input Text:"));
        inputTextField = new JTextField();
        panel.add(inputTextField);

        panel.add(new JLabel("Key (8 characters):"));
        keyTextField = new JTextField();
        panel.add(keyTextField);

        panel.add(new JLabel("Output:"));
        outputTextArea = new JTextArea();
        outputTextArea.setLineWrap(true);
        outputTextArea.setWrapStyleWord(true);
        outputTextArea.setEditable(false);
        panel.add(new JScrollPane(outputTextArea));

        encryptButton = new JButton("Encrypt");
        decryptButton = new JButton("Decrypt");

        panel.add(encryptButton);
        panel.add(decryptButton);

        add(panel);

        // Action listener for Encrypt button
        encryptButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String inputText = inputTextField.getText();
                    String key = keyTextField.getText();
                    if (key.length() != 8) {
                        JOptionPane.showMessageDialog(null, "Key must be 8 characters long.");
                        return;
                    }
                    secretKey = new SecretKeySpec(key.getBytes(), "DES");
                    String encryptedText = encrypt(inputText, secretKey);
                    outputTextArea.setText("Encrypted: " + encryptedText);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });

        // Action listener for Decrypt button
        decryptButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String inputText = inputTextField.getText();
                    String key = keyTextField.getText();
                    if (key.length() != 8) {
                        JOptionPane.showMessageDialog(null, "Key must be 8 characters long.");
                        return;
                    }
                    secretKey = new SecretKeySpec(key.getBytes(), "DES");
                    String decryptedText = decrypt(inputText, secretKey);
                    outputTextArea.setText("Decrypted: " + decryptedText);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    public String encrypt(String strToEncrypt, SecretKey secretKey) throws Exception {
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedBytes = cipher.doFinal(strToEncrypt.getBytes("UTF-8"));
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    public String decrypt(String strToDecrypt, SecretKey secretKey) throws Exception {
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(strToDecrypt));
        return new String(decryptedBytes, "UTF-8");
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new DESEncryptionGUI().setVisible(true);
            }
        });
    }
}

How to Run the Program:

  1. Copy the code into a Java IDE or text editor.
  2. Compile and run the program.
  3. Input text and a key in the provided fields, then click the “Encrypt” button to see the encrypted text.
  4. To decrypt, paste the encrypted text back into the input field and use the same key, then click “Decrypt” to see the original text.
See also  Bill Management System in Python Free Source Code

Final year Java Projects :- Click Here

Steps

  1. Launching the Program:
  • When you run the DESEncryptionGUI class, a window (JFrame) will appear with the following fields and buttons:
    • Input Text: A text field where you enter the text to be encrypted or decrypted.
    • Key (8 characters): A text field where you enter the DES key. The key must be exactly 8 characters long.
    • Output: A text area where the result (encrypted or decrypted text) is displayed.
    • Encrypt Button: Clicking this button will encrypt the text using the provided key.
    • Decrypt Button: Clicking this button will decrypt the text using the provided key.
  1. Example Scenario:
  • Input Text: HelloWorld
  • Key (8 characters): MySecret
  • Click on “Encrypt” Button:
    • The output area will display the encrypted text (Base64 encoded).
  • Copy the Encrypted Text from the output area to the input field.
  • Click on “Decrypt” Button:
    • The output area will display the original text HelloWorld.

Sample Output

  1. Initial Screen:
  • When the program starts, the fields will be empty.
  1. After Encryption: Input Text: HelloWorld
    Key (8 characters): MySecret Output:
   Encrypted: +kthM2BCQcU=

This shows the Base64-encoded encrypted text.

  1. After Decryption:
  • Copy the encrypted text (+kthM2BCQcU=) back into the input field and use the same key MySecret.
  • After clicking the “Decrypt” button, the output will show:
   Decrypted: HelloWorld

Top JSP PROJECTS :- Click here

Screenshots (Conceptual)

If you were to see this in a graphical interface:

  1. Before Encryption:
   Input Text:      [HelloWorld]
   Key (8 chars):   [MySecret]
   Output:          [                    ]

(Click “Encrypt”)

  1. After Encryption:
   Input Text:      [HelloWorld]
   Key (8 chars):   [MySecret]
   Output:          [Encrypted: +kthM2BCQcU=]
  1. After Decryption:
   Input Text:      [+kthM2BCQcU=]
   Key (8 chars):   [MySecret]
   Output:          [Decrypted: HelloWorld]

Important Notes:

  • Security: DES is outdated and insecure for modern applications. Consider using AES for stronger encryption.
  • GUI Experience: This example shows how Swing can be used to create interactive Java applications, making it suitable for learning and small-scale projects.
See also  Online Shopping System using PHP, MySQL with Free Source Code

Considerations:

  • DES is considered insecure by today’s standards for cryptography. For actual encryption needs, consider using more secure algorithms like AES.
  • The code provided here is meant for educational purposes and should not be used in production systems.

encryption program
how to make an encryption program in python
aes encryption program
string encryption program in java
encryption program meaning
caesar cipher encryption program in c
aes encryption program in java
what was different about zimmermann encryption program
des encryption program in java
encryption program app
encryption audit program
encryption algorithm program in c++
aes encryption program in c
aes encryption program in python
an encryption program
best encryption program
blowfish encryption program
binary encryption program in c++

1 comment

Post Comment