-
Notifications
You must be signed in to change notification settings - Fork 13
/
CaesarCipher.java
86 lines (68 loc) · 3.11 KB
/
CaesarCipher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*********
-*- Made by VoxelPixel
-*- For YouTube Tutorial
-*- https://github.com/VoxelPixel
-*- Support on me Patreon: https://www.patreon.com/voxelpixel
*********/
import java.util.Scanner;
public class CaesarCipher {
//we'll be using ASCII in this
public static void main(String[] args){
System.out.print("1. Encryption\n2. Decryption\nChoose(1,2): ");
Scanner in = new Scanner(System.in);
int choice = in.nextInt();
if (choice == 1){
System.out.println("Encryption");
in.nextLine(); //to pass empty line to scanner to avoid erros
System.out.println("Message can only be lower or uppercase alphabet");
System.out.print("Enter Message: ");
String msg = in.nextLine();
System.out.print("Enter key (0-25): "); //26 being length of alphabet
int key = in.nextInt();
String encrypMsg = "";
for (int i = 0; i < msg.length(); i++) {
// again casting
if ((int)msg.charAt(i) == 32){
encrypMsg += (char)32; // ignoring space, casting int to char
} else if ((int)msg.charAt(i) + key > 122){
int temp = ((int)msg.charAt(i) + key) - 122;
encrypMsg += (char)(96 + temp);
} else if ((int)msg.charAt(i) + key > 90 && (int)msg.charAt(i) < 96){
int temp = ((int)msg.charAt(i) + key) - 90;
encrypMsg += (char)(64+temp);
} else {
encrypMsg += (char)((int)msg.charAt(i) + key);
}
} // for loop
System.out.println(encrypMsg);
} else if (choice == 2){
System.out.println("Decryption");
in.nextLine();
System.out.println("Message can only be upper or lowercase alphabet");
System.out.print("Enter encrypted Text: ");
String encypText = in.nextLine();
System.out.println("Enter key (0-25): ");
int dcyptkey = in.nextInt();
String decrypMsg = "";
for (int i = 0; i < encypText.length(); i++) {
// now type casting
if((int)encypText.charAt(i) == 32){
decrypMsg += (char)32;
} else if (((int)encypText.charAt(i) - dcyptkey) < 97 && ((int)encypText.charAt(i) - dcyptkey) > 90) {
//lower case
int temp = ((int)encypText.charAt(i) - dcyptkey) + 26;
decrypMsg += (char)temp;
} else if ((encypText.charAt(i) - dcyptkey) < 65) {
// upper case
int temp = ((int)encypText.charAt(i) - dcyptkey) + 26;
decrypMsg += (char)temp;
} else {
decrypMsg += (char)((int)encypText.charAt(i) - dcyptkey);
}
} // for loop
System.out.println(decrypMsg);
} else {
System.out.println("Wrong Choice");
}
}
}