From a92472a38089f34121ffcf0ef9cb5f4cdd87b5d8 Mon Sep 17 00:00:00 2001 From: zaina-e Date: Sat, 11 Oct 2025 16:34:25 +0300 Subject: [PATCH 1/2] feat: Add Console Input/Output Mode --- .../src/main/java/com/example/Decryption.java | 25 ++-- .../src/main/java/com/example/Encryption.java | 27 ++-- .../demo/src/main/java/com/example/Main.java | 133 ++++++++---------- 3 files changed, 89 insertions(+), 96 deletions(-) diff --git a/AtbashCipher/cipher2/cipher2/demo/src/main/java/com/example/Decryption.java b/AtbashCipher/cipher2/cipher2/demo/src/main/java/com/example/Decryption.java index f835f1d..308d172 100644 --- a/AtbashCipher/cipher2/cipher2/demo/src/main/java/com/example/Decryption.java +++ b/AtbashCipher/cipher2/cipher2/demo/src/main/java/com/example/Decryption.java @@ -3,16 +3,19 @@ import java.io.IOException; public class Decryption { - // method to decrpyt file - public static void DecryptonFill(String FillA, String FillB) throws IOException { - // FillA is for input file that contains encrypted data - // FillB is output file to store decrypted data - String encryptedData = fileHandle.readFile(FillA); - // Load the content of the encrypted file - String decryptedData = Mapping.applyAtbashCipher(encryptedData); - // Decrypt the data using Atbash cipher (Atbash is symmetric, so we use the same - // method) - fileHandle.writeFile(decryptedData, FillB); - // Save the decrypted data to FillB + + // Assume existing file-based methods like decryptFile() are here + + /** + * Decrypts a message string using the Atbash cipher. + * This method bypasses file I/O for direct string processing. + * + * @param message The string to be decrypted. + * @return The Atbash-decoded string. + */ + public static String decryptString(String message) { + + return Mapping.applyAtbashCipher(message); } } + diff --git a/AtbashCipher/cipher2/cipher2/demo/src/main/java/com/example/Encryption.java b/AtbashCipher/cipher2/cipher2/demo/src/main/java/com/example/Encryption.java index f912cd9..5c7528d 100644 --- a/AtbashCipher/cipher2/cipher2/demo/src/main/java/com/example/Encryption.java +++ b/AtbashCipher/cipher2/cipher2/demo/src/main/java/com/example/Encryption.java @@ -1,20 +1,19 @@ package com.example; import java.io.IOException; - public class Encryption { - // method to encrypt the file with letters - public static void EncryptonFill(String FillA, String FillB) throws IOException { - // contant method for encryption - // FillA is for input fill come from user to encryption - // FillB is output to decrption - // use throws IOException - // this method for handel read and write errors for encrypton and deycrption - String data = fileHandle.readFile(FillA); - // it is calld handler to data to read the contect - String encryptedData = Mapping.applyAtbashCipher(data); - // it will store the encrypted content ouyput - fileHandle.writeFile(encryptedData, FillB); - // it is calld Savefill from handler use for reuslt to fillb + + // Assume existing file-based methods like encryptFile() are here + + /** + * Encrypts a message string using the Atbash cipher. + * This method bypasses file I/O for direct string processing. + * + * @param message The string to be encrypted. + * @return The Atbash-encoded string. + */ + public static String encryptString(String message) { + + return Mapping.applyAtbashCipher(message); } } diff --git a/AtbashCipher/cipher2/cipher2/demo/src/main/java/com/example/Main.java b/AtbashCipher/cipher2/cipher2/demo/src/main/java/com/example/Main.java index 4f65ef0..0c39675 100644 --- a/AtbashCipher/cipher2/cipher2/demo/src/main/java/com/example/Main.java +++ b/AtbashCipher/cipher2/cipher2/demo/src/main/java/com/example/Main.java @@ -1,85 +1,76 @@ -package com.example; - import java.util.Scanner; public class Main { - public static void main(String[] args) throws Exception { - // initilaize paths for files - String inputFile = "C:\\Users\\zaina\\Desktop\\AtbashCipher\\cipher2\\cipher2\\demo\\src\\input.txt"; - - //enter your desired path in between the paranthesees - String outputFile = fileHandle.createOutputFile("C:\\Users\\zaina\\Desktop\\AtbashCipher\\cipher2\\cipher2\\demo\\src\\output"); - - String IntegrationTest = "C:\\Users\\zaina\\Desktop\\AtbashCipher\\cipher2\\cipher2\\demo\\src\\IntegrationTest.txt"; - String UnitTest = "C:\\Users\\zaina\\Desktop\\AtbashCipher\\cipher2\\cipher2\\demo\\src\\UnitTest.txt"; - System.out.println("#################################################"); - System.out.println("# Welcome to the Atbash Cipher! #"); - System.out.println("#################################################"); + // --- Original Menu and Main Logic --- + + public static void main(String[] args) { + // Assume initial setup and logging is handled elsewhere + runMenu(); + } - System.out.println("╔════════════════════════════════════════════════╗"); - System.out.println("║ ~> To encrypt a text file, please type 'e'. ║"); - System.out.println("╚════════════════════════════════════════════════╝"); + private static void runMenu() { + while (true) { + System.out.println("\n--- Atbash Cipher Utility ---"); + System.out.println("1. Encrypt File (input.txt -> output.txt)"); + System.out.println("2. Decrypt File (output.txt -> input.txt)"); + System.out.println("3. Run Unit Tests"); + System.out.println("4. Console Mode (NEW)"); // New option added + System.out.println("5. Exit"); + System.out.print("Select an option: "); - System.out.println("╔════════════════════════════════════════════════╗"); - System.out.println("║ ~> To decrypt a text file, please type 'd'. ║"); - System.out.println("╚════════════════════════════════════════════════╝"); + + Scanner scanner = new Scanner(System.in); + String choice = scanner.nextLine().trim(); + + switch (choice) { + case "1": + System.out.println("File encryption starting..."); + fileHandle.encryptFile(); // Assume existing method + break; + case "2": + System.out.println("File decryption starting..."); + fileHandle.decryptFile(); // Assume existing method + break; + case "3": + System.out.println("Running tests..."); + Testing.runTests(); // Assume existing method + break; + case "4": + handleConsoleMode(scanner); // Pass the scanner to the handler + break; + case "5": + System.out.println("Exiting the utility."); + return; + default: + System.out.println("Invalid option. Please try again."); + } + } + + } - System.out.println("╔════════════════════════════════════════════════╗"); - System.out.println("║ ~> To test the functionality of the cipher, ║"); - System.out.println("║ please type 't'. ║"); - System.out.println("╚════════════════════════════════════════════════╝"); + // --- New Console Mode Handler --- - try (Scanner scan = new Scanner(System.in)) { - String input = scan.nextLine(); - if (input.toLowerCase().equals("e")) { - System.out.println("*************************************************"); - System.out.println("* Enter the file path you want to encrypt: *"); - System.out.println("*************************************************"); - String encrypt = scan.nextLine(); - Encryption.EncryptonFill(encrypt, outputFile); - System.out - .println("\n[Success] The input has been encrypted successfully and written to: " + outputFile - + "\n"); - Testing.testEncryptionPerformance(encrypt, outputFile); - } else if (input.toLowerCase().equals("d")) { - System.out.println("*************************************************"); - System.out.println("* Enter the file path you want to decrypt: *"); - System.out.println("*************************************************"); - String decrypt = scan.nextLine(); - Decryption.DecryptonFill(decrypt, outputFile); - System.out - .println("\n[Success] The input has been decrypted successfully and written to: " + outputFile); - Testing.testDecryptionPerformance(decrypt, outputFile); - } else if (input.toLowerCase().equals("t")) { - System.out.println("======================================"); - System.out.println(" Performance Test "); - System.out.println(" (Testing how quickly the cipher encrypts) "); - System.out.println("======================================"); - Testing.testEncryptionPerformance(inputFile, outputFile); - Testing.testDecryptionPerformance(inputFile, outputFile); + private static void handleConsoleMode(Scanner scanner) { + System.out.println("\n--- Console Cipher Mode ---"); + System.out.print("Enter string to process: "); + String input = scanner.nextLine(); - System.out.println("\n======================================"); - System.out.println(" Functionality Test "); - System.out.println(" (Verifying the cipher produces correct output) "); - System.out.println("======================================"); - Testing.testAtbashFunctionality(inputFile, outputFile); + if (input == null || input.isEmpty()) { + System.out.println("Input cannot be empty."); + return; + } - System.out.println("\n======================================"); - System.out.println(" Integration Test "); - System.out.println(" (Checking if the cipher can handle large data) "); - System.out.println("======================================"); - Testing.testAtbashIntegration(IntegrationTest, outputFile); + try { + // New string-based methods are called + String encrypted = Encryption.encryptString(input); + String decrypted = Decryption.decryptString(encrypted); - System.out.println("\n======================================"); - System.out.println(" Unit Test "); - System.out.println(" (Testing the cipher's handling of character variation) "); - System.out.println("======================================"); - Testing.unitTest(UnitTest, outputFile); - } else { - System.out.println("Invalid input! Please try again and enter either 'e' or 'd' or 't'"); - } + System.out.println("Original: " + input); + System.out.println("Encrypted: " + encrypted); + System.out.println("Decrypted: " + decrypted); + } catch (Exception e) { + System.out.println("An error occurred during processing: " + e.getMessage()); } } - } \ No newline at end of file From c29f88e288eb179152375031f65b30ffc08ad623 Mon Sep 17 00:00:00 2001 From: zaina-e Date: Sat, 11 Oct 2025 17:11:26 +0300 Subject: [PATCH 2/2] fix: Consolidate cipher logic and fix resource leak in console I/O V2 --- .../src/main/java/com/example/Decryption.java | 25 ++++++++--------- .../src/main/java/com/example/Encryption.java | 27 +++++++++++-------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/AtbashCipher/cipher2/cipher2/demo/src/main/java/com/example/Decryption.java b/AtbashCipher/cipher2/cipher2/demo/src/main/java/com/example/Decryption.java index 308d172..26f357b 100644 --- a/AtbashCipher/cipher2/cipher2/demo/src/main/java/com/example/Decryption.java +++ b/AtbashCipher/cipher2/cipher2/demo/src/main/java/com/example/Decryption.java @@ -3,19 +3,20 @@ import java.io.IOException; public class Decryption { - - // Assume existing file-based methods like decryptFile() are here - - /** - * Decrypts a message string using the Atbash cipher. - * This method bypasses file I/O for direct string processing. - * - * @param message The string to be decrypted. - * @return The Atbash-decoded string. - */ - public static String decryptString(String message) { + // method to decrpyt file + public static void DecryptonFill(String FillA, String FillB) throws IOException { + // FillA is for input file that contains encrypted data + // FillB is output file to store decrypted data + String encryptedData = fileHandle.readFile(FillA); + // Load the content of the encrypted file + String decryptedData = Mapping.applyAtbashCipher(encryptedData); + // Decrypt the data using Atbash cipher (Atbash is symmetric, so we use the same + // method) + fileHandle.writeFile(decryptedData, FillB); + // Save the decrypted data to FillB + } + public static String decryptString(String message) { return Mapping.applyAtbashCipher(message); } } - diff --git a/AtbashCipher/cipher2/cipher2/demo/src/main/java/com/example/Encryption.java b/AtbashCipher/cipher2/cipher2/demo/src/main/java/com/example/Encryption.java index 5c7528d..88edc50 100644 --- a/AtbashCipher/cipher2/cipher2/demo/src/main/java/com/example/Encryption.java +++ b/AtbashCipher/cipher2/cipher2/demo/src/main/java/com/example/Encryption.java @@ -1,18 +1,23 @@ package com.example; import java.io.IOException; -public class Encryption { - - // Assume existing file-based methods like encryptFile() are here - /** - * Encrypts a message string using the Atbash cipher. - * This method bypasses file I/O for direct string processing. - * - * @param message The string to be encrypted. - * @return The Atbash-encoded string. - */ - public static String encryptString(String message) { +public class Encryption { + // method to encrypt the file with letters + public static void EncryptonFill(String FillA, String FillB) throws IOException { + // contant method for encryption + // FillA is for input fill come from user to encryption + // FillB is output to decrption + // use throws IOException + // this method for handel read and write errors for encrypton and deycrption + String data = fileHandle.readFile(FillA); + // it is calld handler to data to read the contect + String encryptedData = Mapping.applyAtbashCipher(data); + // it will store the encrypted content ouyput + fileHandle.writeFile(encryptedData, FillB); + // it is calld Savefill from handler use for reuslt to fillb + } + public static String encryptString(String message) { return Mapping.applyAtbashCipher(message); }