-
Notifications
You must be signed in to change notification settings - Fork 5
Encoder decoder application by debugger #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
b59c7bc
358148a
7dff2c0
5eb2bc7
3c890bd
a6e5f85
a0f7749
3377d11
b5b6986
9207ae2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 { | ||||||||||||||||||||||||||||||||||||||
| // 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| return decodedText.toString(); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| public static void main(String[] args) { | ||||||||||||||||||||||||||||||||||||||
| SwingUtilities.invokeLater(() -> new EncoderDecoder()); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,43 @@ | ||||||
|  | ||||||
| # 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. | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix incorrect filename in download instructions The instruction mentions -2. Download the `editor.java` file from this repository.
+2. Download the `EncoderDecoder.java` file from this repository.📝 Committable suggestion
Suggested change
|
||||||
| 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. | ||||||
|
|
||||||
There was a problem hiding this comment.
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: