Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 159 additions & 0 deletions EncoderDecoder.java
Original file line number Diff line number Diff line change
@@ -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<String> 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 {
Comment on lines +84 to +96
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Make Caesar Cipher shift value configurable

The shift value for Caesar Cipher is hardcoded to 3, which limits the usefulness of this cipher algorithm. Consider making this value configurable through the UI.

Add a spinner or text field for the user to select the shift value:

 if (algorithm.equals("Caesar Cipher")) {
-    int shift = 3; // Define the shift value for the Caesar Cipher (you can make it dynamic if needed)
+    int shift = getCaesarShiftValue(); // Get the shift value from the UI
     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")) {

And add a method to get the shift value:

private int getCaesarShiftValue() {
    // If you've added a spinner or text field for the shift value, get its value here
    // For now, return the default value
    return 3;
}

// 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);
}
Comment on lines +97 to +110
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Remove redundant custom encoding implementation

The "custom" encoding implementation is identical to the Caesar Cipher with a shift of 3. This creates code duplication and confusion. Either remove this code or implement a truly different custom algorithm.

-    } 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);
+    } else {
+        // Handle any other algorithms that might be added in the future
+        encodedText.append("Unsupported algorithm: " + algorithm);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// 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);
}
} else {
// Handle any other algorithms that might be added in the future
encodedText.append("Unsupported algorithm: " + algorithm);
}


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);
}
Comment on lines +137 to +150
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Remove redundant custom decoding implementation

Similar to the encoding method, the "custom" decoding implementation is identical to the Caesar Cipher with a shift of 3. This creates code duplication and should be removed or replaced with a truly different algorithm.

-    } 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);
+    } else {
+        // Handle any other algorithms that might be added in the future
+        decodedText.append("Unsupported algorithm: " + algorithm);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// 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);
}
} else {
// Handle any other algorithms that might be added in the future
decodedText.append("Unsupported algorithm: " + algorithm);
}


return decodedText.toString();
}


public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new EncoderDecoder());
}
}
1 change: 0 additions & 1 deletion README.md

This file was deleted.

43 changes: 43 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -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.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix incorrect filename in download instructions

The instruction mentions editor.java but the actual file name is EncoderDecoder.java as seen in the implementation.

-2. Download the `editor.java` file from this repository.
+2. Download the `EncoderDecoder.java` file from this repository.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
2. Download the `editor.java` file from this repository.
2. Download the `EncoderDecoder.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.