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' diff --git a/src/main/java/App.java b/src/main/java/App.java index c3e66dd..242a0bb 100644 --- a/src/main/java/App.java +++ b/src/main/java/App.java @@ -1,13 +1,69 @@ 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 : "); + 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(RED+"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))); } } 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; + } + }