Skip to content
Merged
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
8 changes: 8 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ plugins {
id 'java'
}

jar {
manifest {
attributes(
'Main-Class': 'App'
)
}
}

group 'com.chalie.caesar'
version '1.0-SNAPSHOT'

Expand Down
68 changes: 62 additions & 6 deletions src/main/java/App.java
Original file line number Diff line number Diff line change
@@ -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)));
}
}
28 changes: 22 additions & 6 deletions src/main/java/CaesarShift.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Character> encryptedChars = new ArrayList<Character>();

//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;
}
Expand All @@ -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;
}

}