Skip to content

Commit 80ff9c3

Browse files
authoredOct 25, 2021
Merge pull request #3 from Chal13W1zz/frontend
Major UI improvements.
2 parents ee91610 + 96f3a5c commit 80ff9c3

File tree

3 files changed

+92
-12
lines changed

3 files changed

+92
-12
lines changed
 

‎build.gradle

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ plugins {
22
id 'java'
33
}
44

5+
jar {
6+
manifest {
7+
attributes(
8+
'Main-Class': 'App'
9+
)
10+
}
11+
}
12+
513
group 'com.chalie.caesar'
614
version '1.0-SNAPSHOT'
715

‎src/main/java/App.java

+62-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,69 @@
11
import java.io.Console;
22

33
public class App {
4+
public static final String RED = "\033[0;31m"; // RED
5+
public static final String GREEN = "\033[0;32m"; // GREEN
6+
public static final String BLUE = "\033[0;34m"; // BLUE
7+
public static final String NEUTRAL = "\033[0m"; // NEUTRAL
48
public static void main(String[] args){
59
Console myConsole = System.console();
6-
System.out.println("Enter the text to Encrypt: ");
7-
String input = myConsole.readLine();
8-
CaesarShift encDec = new CaesarShift();
9-
System.out.println("Input String: " + input);
10-
System.out.println("Encrypted: " + encDec.encrypt(input));
11-
System.out.println("Decrypted String: "+encDec.decrypt(encDec.encrypt(input)));
10+
CaesarShift myCaesar = new CaesarShift();
11+
boolean running = true;
12+
String version = "V1.0";
13+
14+
System.out.println(BLUE+" ____ ____ _ _ __ _ \n" +
15+
" / ___|__ _ ___ ___ __ _ _ __/ ___|| |__ (_)/ _| |_ \n" +
16+
"| | / _` |/ _ \\/ __|/ _` | '__\\___ \\| '_ \\| | |_| __|\n" +
17+
"| |__| (_| | __/\\__ \\ (_| | | ___) | | | | | _| |_ \n" +
18+
" \\____\\__,_|\\___||___/\\__,_|_| |____/|_| |_|_|_| \\__|"+version+NEUTRAL);
19+
System.out.println(RED+" Encrypt/Decrypt your messages"+NEUTRAL);
20+
System.out.println(BLUE+" By @Chal13W1zz"+NEUTRAL);
21+
22+
while (running){
23+
24+
System.out.println("\n \nSelect an option \n -> Encrypt a message : 1 \n -> Decrypt a message : 2 \n -> Exit : 3 ");
25+
Integer option = Integer.parseInt(myConsole.readLine(BLUE+"option"+GREEN+"@caesar:"+BLUE+"~$ "+NEUTRAL));
26+
27+
28+
if(option == 1){
29+
System.out.println("\nEnter the message to Encrypt : ");
30+
String message = myConsole.readLine(BLUE+"message"+GREEN+"@caesar:"+BLUE+"~$ "+NEUTRAL);
31+
System.out.println("\nEnter the shift key '1 - 25' :");
32+
int key = Integer.parseInt(myConsole.readLine(BLUE+"key"+GREEN+"@caesar:"+BLUE+"~$ "+NEUTRAL));
33+
myCaesar.setKey(key);
34+
myCaesar.encrypt(message);
35+
System.out.println(RED+"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"+NEUTRAL);
36+
System.out.println(BLUE+"Input String: "+GREEN+message);
37+
System.out.println(BLUE+"Encrypted String: "+GREEN+myCaesar.getEncryptedMsg());
38+
System.out.println(BLUE+"Shift/Encryption key : "+GREEN+myCaesar.getKey()+NEUTRAL);
39+
System.out.println(RED+"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"+NEUTRAL);
40+
41+
}else if(option == 2){
42+
System.out.println("Enter the message to Decrypt : ");
43+
String message = myConsole.readLine(BLUE+"encryptedMessage"+GREEN+"@caesar:"+BLUE+"~$ "+NEUTRAL);
44+
System.out.println("\nEnter the shift key '1 - 25' :");
45+
int key = Integer.parseInt(myConsole.readLine(BLUE+"key"+GREEN+"@caesar:"+BLUE+"~$ "+NEUTRAL));
46+
myCaesar.setKey(key);
47+
myCaesar.decrypt(message);
48+
System.out.println(RED+"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"+NEUTRAL);
49+
System.out.println(BLUE+"Input String: "+GREEN+message);
50+
System.out.println(BLUE+"Decrypted String: "+GREEN+myCaesar.getDecryptedMsg());
51+
System.out.println(BLUE+"Shift/Decryption key : "+GREEN+myCaesar.getKey()+NEUTRAL);
52+
System.out.println(RED+"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"+NEUTRAL);
53+
54+
}else if(option == 3){
55+
System.out.println(RED+"Goodbye :)");
56+
running = false ;
57+
}else {
58+
System.out.println(RED+"Oops!, invalid Option :("+NEUTRAL);
59+
}
60+
}
61+
62+
//
63+
// int
64+
// CaesarShift encDec = new CaesarShift("Hello",25);
65+
// System.out.println("Input String: " + input);
66+
// System.out.println("Encrypted: " + encDec.encrypt(input));
67+
// System.out.println("Decrypted String: "+encDec.decrypt(encDec.encrypt(input)));
1268
}
1369
}

‎src/main/java/CaesarShift.java

+22-6
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
public class CaesarShift {
44
private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //Encapsulate and make the alphabet immutable
5-
private static int key = 25; //Initialize and encapsulate the shift key
5+
private static int key = 25; //Initialize and encapsulate the shift key
66
private static String encryptedMsg; //Create and encapsulate the message container
7-
private static String decryptedMsg;
7+
private static String decryptedMsg; //Create and encapsulate the decrypted message container
88

9-
public String encrypt(String encryptMsg){
9+
public String encrypt(String encryptMsg) {
1010
String upCased = encryptMsg.toUpperCase();
1111
char[] upCasedArrs = upCased.toCharArray();//split the string to a character array
1212
ArrayList<Character> encryptedChars = new ArrayList<Character>();
1313

1414
//loop through the character array
15-
for(Character character : upCasedArrs ){
15+
for (Character character : upCasedArrs) {
1616
int index = ALPHABET.indexOf(character.toString());//get the character rank in the alphabet
17-
int encryptedCharIndex = Math.floorMod((index+key),26);//shift the character using the key and get the new characters rank in the alphabet
17+
int encryptedCharIndex = Math.floorMod((index + key), 26);//shift the character using the key and get the new characters rank in the alphabet
1818
encryptedChars.add(ALPHABET.charAt(encryptedCharIndex));//get the character from the alphabet rank and add it to the char array
19-
encryptedMsg = encryptedChars.toString().replaceAll("\\[|\\]|\\s","").replaceAll(",","");//convert and cleanup the char array to a string
19+
encryptedMsg = encryptedChars.toString().replaceAll("\\[|\\]|\\s", "").replaceAll(",", "");//convert and cleanup the char array to a string
2020
}
2121
return encryptedMsg;
2222
}
@@ -37,4 +37,20 @@ public String decrypt(String decryptMsg) {
3737

3838
}
3939

40+
public static int getKey() {
41+
return key;
42+
}
43+
44+
public static void setKey(int key) {
45+
CaesarShift.key = key;
46+
}
47+
48+
public static String getEncryptedMsg() {
49+
return encryptedMsg;
50+
}
51+
52+
public static String getDecryptedMsg() {
53+
return decryptedMsg;
54+
}
55+
4056
}

0 commit comments

Comments
 (0)