diff --git a/EncoderDecoder.java b/EncoderDecoder.java new file mode 100644 index 0000000..ad226ba --- /dev/null +++ b/EncoderDecoder.java @@ -0,0 +1,159 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.util.Base64; + +public class EncoderDecoder extends JFrame implements ActionListener { + private JTextArea inputTextArea, outputTextArea; + private JComboBox algorithmComboBox; + private JButton encodeButton, decodeButton; + + public EncoderDecoder() { + setTitle("Encoder Decoder"); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setSize(500, 300); + setLocationRelativeTo(null); + + JPanel mainPanel = new JPanel(); + mainPanel.setLayout(new BorderLayout()); + + inputTextArea = new JTextArea(5, 20); + JScrollPane inputScrollPane = new JScrollPane(inputTextArea); + + outputTextArea = new JTextArea(5, 20); + JScrollPane outputScrollPane = new JScrollPane(outputTextArea); + outputTextArea.setEditable(false); + + JPanel inputPanel = new JPanel(); + inputPanel.setLayout(new GridLayout(1, 2)); + inputPanel.add(new JLabel("Input:")); + inputPanel.add(inputScrollPane); + + JPanel outputPanel = new JPanel(); + outputPanel.setLayout(new GridLayout(1, 2)); + outputPanel.add(new JLabel("Output:")); + outputPanel.add(outputScrollPane); + + JPanel buttonPanel = new JPanel(); + encodeButton = new JButton("Encode"); + decodeButton = new JButton("Decode"); + buttonPanel.add(encodeButton); + buttonPanel.add(decodeButton); + + JPanel algorithmPanel = new JPanel(); + algorithmComboBox = new JComboBox<>(); + algorithmComboBox.addItem("Caesar Cipher"); + algorithmComboBox.addItem("Base64 Encoding"); + // Add more encoding algorithms as needed + algorithmPanel.add(new JLabel("Select Algorithm:")); + algorithmPanel.add(algorithmComboBox); + + mainPanel.add(inputPanel, BorderLayout.NORTH); + mainPanel.add(outputPanel, BorderLayout.CENTER); + mainPanel.add(buttonPanel, BorderLayout.SOUTH); + mainPanel.add(algorithmPanel, BorderLayout.WEST); + + encodeButton.addActionListener(this); + decodeButton.addActionListener(this); + + add(mainPanel); + setVisible(true); + } + + public void actionPerformed(ActionEvent e) { + if (e.getSource() == encodeButton) { + // Get selected algorithm + String selectedAlgorithm = (String) algorithmComboBox.getSelectedItem(); + // Encode input text + String encodedText = encode(inputTextArea.getText(), selectedAlgorithm); + // Display encoded text + outputTextArea.setText(encodedText); + } else if (e.getSource() == decodeButton) { + // Get selected algorithm + String selectedAlgorithm = (String) algorithmComboBox.getSelectedItem(); + // Decode input text + String decodedText = decode(inputTextArea.getText(), selectedAlgorithm); + // Display decoded text + outputTextArea.setText(decodedText); + } + } + + private String encode(String input, String algorithm) { + StringBuilder encodedText = new StringBuilder(); + + if (algorithm.equals("Caesar Cipher")) { + int shift = 3; // Define the shift value for the Caesar Cipher (you can make it dynamic if needed) + for (char c : input.toCharArray()) { + if (Character.isLetter(c)) { + char base = Character.isUpperCase(c) ? 'A' : 'a'; + encodedText.append((char) (((c - base + shift) % 26) + base)); + } else { + encodedText.append(c); + } + } + } else if (algorithm.equals("Base64 Encoding")) { + encodedText.append(Base64.getEncoder().encodeToString(input.getBytes())); + } else { + // Handle custom encoding algorithms + // For example, let's say you have a custom substitution cipher where 'A' maps to 'X', 'B' maps to 'Y', etc. + StringBuilder customEncodedText = new StringBuilder(); + for (char c : input.toCharArray()) { + if (Character.isLetter(c)) { + char base = Character.isUpperCase(c) ? 'A' : 'a'; + char encodedChar = (char) (((c - base + 3) % 26) + base); // This is just an example custom encoding logic + customEncodedText.append(encodedChar); + } else { + customEncodedText.append(c); + } + } + encodedText.append(customEncodedText); + } + + return encodedText.toString(); +} + + + private String decode(String input, String algorithm) { + StringBuilder decodedText = new StringBuilder(); + + if (algorithm.equals("Caesar Cipher")) { + int shift = 3; // Define the shift value for the Caesar Cipher (you can make it dynamic if needed) + for (char c : input.toCharArray()) { + if (Character.isLetter(c)) { + char base = Character.isUpperCase(c) ? 'A' : 'a'; + decodedText.append((char) (((c - base - shift + 26) % 26) + base)); + } else { + decodedText.append(c); + } + } + } else if (algorithm.equals("Base64 Encoding")) { + try { + byte[] decodedBytes = Base64.getDecoder().decode(input); + decodedText.append(new String(decodedBytes)); + } catch (IllegalArgumentException e) { + decodedText.append("Invalid Base64 encoded string."); + } + } else { + // Handle custom decoding algorithms + // For example, let's say you have a custom substitution cipher where 'X' maps to 'A', 'Y' maps to 'B', etc. + StringBuilder customDecodedText = new StringBuilder(); + for (char c : input.toCharArray()) { + if (Character.isLetter(c)) { + char base = Character.isUpperCase(c) ? 'A' : 'a'; + char decodedChar = (char) (((c - base - 3 + 26) % 26) + base); // This is just an example custom decoding logic + customDecodedText.append(decodedChar); + } else { + customDecodedText.append(c); + } + } + decodedText.append(customDecodedText); + } + + return decodedText.toString(); +} + + + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> new EncoderDecoder()); + } +} diff --git a/README.md b/README.md deleted file mode 100644 index 1a0c609..0000000 --- a/README.md +++ /dev/null @@ -1 +0,0 @@ -JAVA/CPP repo for OPENHACK. diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..fffc89f --- /dev/null +++ b/readme.md @@ -0,0 +1,43 @@ +![E D](https://github.com/Shubham25104/OH24-CPP-Java/assets/117568702/80e98f33-e877-4cd0-b7b8-1b0d4b87162a) +# Encoder Decoder Application + +The Encoder Decoder application is a Java program with a graphical user interface (GUI) that allows users to encode plaintext messages into ciphertext and decode ciphertext back into plaintext using various encoding algorithms. + +## Features + +- Encode plaintext messages into ciphertext using different encoding algorithms. +- Decode ciphertext back into plaintext. +- Support for multiple encoding algorithms such as Caesar cipher, Base64 encoding, and custom substitution ciphers. +- User-friendly graphical interface for inputting messages, selecting encoding algorithms, and viewing the encoded or decoded output. + +## How to Run + +1. Ensure you have Java installed on your PC. If not, download and install Java from [here](https://www.java.com/en/download/). +2. Download the `editor.java` file from this repository. +3. Open Command Prompt (on Windows) or Terminal (on macOS/Linux). +4. Navigate to the directory where the `editor.java` file is located. +5. Compile the Java source file using the following command: + ```bash + javac EncoderDecoder.java + ``` +6. Run the compiled Java program using the following command: + ```bash + java EncoderDecoder + ``` + +## Usage + +**Input Message:** Enter the message you want to encode/decode into the input text area. +**Select Algorithm:** Choose the encoding algorithm from the dropdown menu. +**Encode:** Click the "Encode" button to encode the input message using the selected algorithm. +**Decode:** Click the "Decode" button to decode the input message using the selected algorithm. +**Output Message:** View the encoded or decoded message in the output text area. + +## Supported Algorithms +**Caesar Cipher:** Shifts each letter in the message by a fixed number of positions. +**Base64 Encoding:** Converts binary data into ASCII text format. +## Notes + + +- The application uses Java Swing for the graphical user interface. +