From af4c8fd9f9a3473dd20243c12dc4d74dfc2e343c Mon Sep 17 00:00:00 2001 From: Chal13W1zz Date: Mon, 25 Oct 2021 22:25:42 +0300 Subject: [PATCH 1/4] add banner and beautify Encrypt UI --- src/main/java/App.java | 58 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/src/main/java/App.java b/src/main/java/App.java index c3e66dd..ef7fd0e 100644 --- a/src/main/java/App.java +++ b/src/main/java/App.java @@ -1,13 +1,59 @@ import java.io.Console; public class App { + public static final String RED = "\033[0;31m"; // RED + public static final String GREEN = "\033[0;32m"; // GREEN + public static final String BLUE = "\033[0;34m"; // BLUE + public static final String NEUTRAL = "\033[0m"; // NEUTRAL public static void main(String[] args){ Console myConsole = System.console(); - System.out.println("Enter the text to Encrypt: "); - String input = myConsole.readLine(); - CaesarShift encDec = new CaesarShift(); - System.out.println("Input String: " + input); - System.out.println("Encrypted: " + encDec.encrypt(input)); - System.out.println("Decrypted String: "+encDec.decrypt(encDec.encrypt(input))); + CaesarShift myCaesar = new CaesarShift(); + boolean running = true; + String version = "V1.0"; + + System.out.println(BLUE+" ____ ____ _ _ __ _ \n" + + " / ___|__ _ ___ ___ __ _ _ __/ ___|| |__ (_)/ _| |_ \n" + + "| | / _` |/ _ \\/ __|/ _` | '__\\___ \\| '_ \\| | |_| __|\n" + + "| |__| (_| | __/\\__ \\ (_| | | ___) | | | | | _| |_ \n" + + " \\____\\__,_|\\___||___/\\__,_|_| |____/|_| |_|_|_| \\__|"+version+NEUTRAL); + System.out.println(RED+" Encrypt/Decrypt your messages"+NEUTRAL); + System.out.println(BLUE+" By @Chal13W1zz"+NEUTRAL); + + while (running){ + + System.out.println("\n \nSelect an option \n -> Encrypt a message : 1 \n -> Decrypt a message : 2 \n -> Exit : 3 "); + Integer option = Integer.parseInt(myConsole.readLine(BLUE+"option"+GREEN+"@caesar:"+BLUE+"~$ "+NEUTRAL)); + + + if(option == 1){ + System.out.println("\nEnter the message to Encrypt : "); + String message = myConsole.readLine(BLUE+"message"+GREEN+"@caesar:"+BLUE+"~$ "+NEUTRAL); + System.out.println("\nEnter the shift key '1 - 25' :"); + int key = Integer.parseInt(myConsole.readLine(BLUE+"key"+GREEN+"@caesar:"+BLUE+"~$ "+NEUTRAL)); + myCaesar.setKey(key); + myCaesar.encrypt(message); + System.out.println(RED+"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"+NEUTRAL); + System.out.println(BLUE+"Input String: "+GREEN+message); + System.out.println(BLUE+"Encrypted String: "+GREEN+myCaesar.getEncryptedMsg()); + System.out.println(BLUE+"Shift/Encryption key : "+GREEN+myCaesar.getKey()+NEUTRAL); + System.out.println(RED+"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"+NEUTRAL); + + }else if(option == 2){ + System.out.println("Enter the message to Decrypt : "); + + }else if(option == 3){ + System.out.println("Goodbye :)"); + running = false ; + }else { + System.out.println(RED+"Oops!, invalid Option :("+NEUTRAL); + } + } + + // +// int +// CaesarShift encDec = new CaesarShift("Hello",25); +// System.out.println("Input String: " + input); +// System.out.println("Encrypted: " + encDec.encrypt(input)); +// System.out.println("Decrypted String: "+encDec.decrypt(encDec.encrypt(input))); } } From 22d525f91fda5c975f592d8b71a4e91af76b2833 Mon Sep 17 00:00:00 2001 From: Chal13W1zz Date: Mon, 25 Oct 2021 22:28:55 +0300 Subject: [PATCH 2/4] add getters and setters --- src/main/java/CaesarShift.java | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/main/java/CaesarShift.java b/src/main/java/CaesarShift.java index e603113..f0600fe 100644 --- a/src/main/java/CaesarShift.java +++ b/src/main/java/CaesarShift.java @@ -2,21 +2,21 @@ public class CaesarShift { private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //Encapsulate and make the alphabet immutable - private static int key = 25; //Initialize and encapsulate the shift key + private static int key = 25; //Initialize and encapsulate the shift key private static String encryptedMsg; //Create and encapsulate the message container - private static String decryptedMsg; + private static String decryptedMsg; //Create and encapsulate the decrypted message container - public String encrypt(String encryptMsg){ + public String encrypt(String encryptMsg) { String upCased = encryptMsg.toUpperCase(); char[] upCasedArrs = upCased.toCharArray();//split the string to a character array ArrayList encryptedChars = new ArrayList(); //loop through the character array - for(Character character : upCasedArrs ){ + for (Character character : upCasedArrs) { int index = ALPHABET.indexOf(character.toString());//get the character rank in the alphabet - int encryptedCharIndex = Math.floorMod((index+key),26);//shift the character using the key and get the new characters rank in the alphabet + int encryptedCharIndex = Math.floorMod((index + key), 26);//shift the character using the key and get the new characters rank in the alphabet encryptedChars.add(ALPHABET.charAt(encryptedCharIndex));//get the character from the alphabet rank and add it to the char array - encryptedMsg = encryptedChars.toString().replaceAll("\\[|\\]|\\s","").replaceAll(",","");//convert and cleanup the char array to a string + encryptedMsg = encryptedChars.toString().replaceAll("\\[|\\]|\\s", "").replaceAll(",", "");//convert and cleanup the char array to a string } return encryptedMsg; } @@ -37,4 +37,20 @@ public String decrypt(String decryptMsg) { } + public static int getKey() { + return key; + } + + public static void setKey(int key) { + CaesarShift.key = key; + } + + public static String getEncryptedMsg() { + return encryptedMsg; + } + + public static String getDecryptedMsg() { + return decryptedMsg; + } + } From f60f4105105b52d4e2750deb0bde696bff894bff Mon Sep 17 00:00:00 2001 From: Chal13W1zz Date: Mon, 25 Oct 2021 22:35:37 +0300 Subject: [PATCH 3/4] beautify decrypt UI --- src/main/java/App.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/App.java b/src/main/java/App.java index ef7fd0e..242a0bb 100644 --- a/src/main/java/App.java +++ b/src/main/java/App.java @@ -40,9 +40,19 @@ public static void main(String[] args){ }else if(option == 2){ System.out.println("Enter the message to Decrypt : "); + String message = myConsole.readLine(BLUE+"encryptedMessage"+GREEN+"@caesar:"+BLUE+"~$ "+NEUTRAL); + System.out.println("\nEnter the shift key '1 - 25' :"); + int key = Integer.parseInt(myConsole.readLine(BLUE+"key"+GREEN+"@caesar:"+BLUE+"~$ "+NEUTRAL)); + myCaesar.setKey(key); + myCaesar.decrypt(message); + System.out.println(RED+"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"+NEUTRAL); + System.out.println(BLUE+"Input String: "+GREEN+message); + System.out.println(BLUE+"Decrypted String: "+GREEN+myCaesar.getDecryptedMsg()); + System.out.println(BLUE+"Shift/Decryption key : "+GREEN+myCaesar.getKey()+NEUTRAL); + System.out.println(RED+"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"+NEUTRAL); }else if(option == 3){ - System.out.println("Goodbye :)"); + System.out.println(RED+"Goodbye :)"); running = false ; }else { System.out.println(RED+"Oops!, invalid Option :("+NEUTRAL); From 96f3a5cc3a3231a5ce9b6eff94f4e00858c75dd0 Mon Sep 17 00:00:00 2001 From: Chal13W1zz Date: Tue, 26 Oct 2021 00:01:22 +0300 Subject: [PATCH 4/4] add jar packaging instructions --- build.gradle | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/build.gradle b/build.gradle index 2650abc..51fc1ff 100644 --- a/build.gradle +++ b/build.gradle @@ -2,6 +2,14 @@ plugins { id 'java' } +jar { + manifest { + attributes( + 'Main-Class': 'App' + ) + } +} + group 'com.chalie.caesar' version '1.0-SNAPSHOT'