Skip to content
Amrit Sinha edited this page May 13, 2023 · 1 revision

File Encryption

The FileEncryption class provides a simple GUI-based application to encrypt or decrypt a file using a given key. The application utilizes the XOR operation to modify the bytes of the file.

How to Use

  1. Launch the application by running the main method in the FileEncryption class.

  2. A window titled "File Encryption" will appear.

  3. Enter the encryption/decryption key in the text field labeled "Key".

  4. Click the "Select File" button to choose the file you want to encrypt or decrypt.

  5. After selecting the file, the application will perform the encryption or decryption operation using the entered key.

  6. Once the operation is complete, a dialog box will display the message "Done!".

Code Explanation

The FileEncryption class contains the following methods:

public static void encryptDecrypt(int key)

This method performs the encryption or decryption operation on a file using the provided key.

  1. It creates an instance of JFileChooser to allow the user to select a file for encryption or decryption.

  2. The selected file is read using a FileInputStream, and its contents are stored in a byte array.

  3. Each byte in the array is XORed with the given key to perform the encryption or decryption.

  4. The modified byte array is then written back to the same file using a FileOutputStream.

  5. Finally, a dialog box is displayed to indicate the completion of the operation.

public static void main(String[] args)

This method is the entry point of the application. It sets up the graphical user interface (GUI) and handles the button click event.

  1. The main method initializes a JFrame and configures its properties such as title, size, and close operation.

  2. A Font object is created to set the font style for the label, text field, and button.

  3. The GUI components, including a label, a text field, and a button, are created and added to the frame using a FlowLayout.

  4. An action listener is attached to the button. When the button is clicked, the text from the text field is retrieved, converted to an integer, and passed to the encryptDecrypt method for further processing.

  5. Finally, the frame is set visible, and the application is ready for user interaction.

That's it! You can now use the FileEncryption application to encrypt or decrypt files using a specified key.