-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathROT18.java
106 lines (93 loc) · 3.49 KB
/
ROT18.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*********
-*- Made by VoxelPixel
-*- For YouTube Tutorial
-*- https://github.com/VoxelPixel
-*- Support me on Patreon: https://www.patreon.com/voxelpixel
*********/
import java.util.Scanner;
public class ROT18 {
private static Scanner in;
public static void main(String[] args){
in = new Scanner(System.in);
System.out.print("1. Encryption\n2. Decryption\nChoose(1,2): ");
int choice = in.nextInt();
in.nextLine();
if(choice == 1){
System.out.println("---Encryption---");
cipherEncryption();
} else if(choice == 2){
System.out.println("---Decryption---");
cipherDecryption();
} else {
System.out.println("Invalid Choice");
}
}
private static void cipherDecryption() {
String rot5 = "5678901234";
String zeroToNine = "0123456789";
int rot13Key = 13;
System.out.println("Message can be alphanumeric");
System.out.print("Enter messsage: ");
String message = in.nextLine();
in.nextLine();
message = message.toUpperCase();
String decrypText = "";
for (int i = 0; i < message.length(); i++) {
String temp = message.charAt(i) + "";
if((int)message.charAt(i) == 32){
decrypText += " ";
} else if (temp.matches("[\\s\\d]+")){
// ROT5
for (int j = 0; j < zeroToNine.length(); j++) {
if(message.charAt(i) == rot5.charAt(j)){
decrypText += zeroToNine.charAt(j);
}
} // inner for
} else if(temp.matches("[\\s\\w]+")) {
// ROT13
int chTemp = (int)temp.charAt(0) - rot13Key;
if (chTemp < 65){
chTemp += 26;
decrypText += (char)chTemp;
} else {
decrypText += (char)chTemp;
}
} // if-else
} // for
System.out.println("Decrypted Text: " + decrypText);
}
private static void cipherEncryption() {
String rot5 = "5678901234";
String zeroToNine = "0123456789";
int rot13Key = 13;
System.out.println("Message can be alphanumeric");
System.out.print("Enter messsage: ");
String message = in.nextLine();
in.nextLine();
message = message.toUpperCase();
String encrypText = "";
for (int i = 0; i < message.length(); i++) {
String temp = message.charAt(i) + "";
if((int)message.charAt(i) == 32){
encrypText += " ";
} else if (temp.matches("[\\s\\d]+")){
// ROT5
for (int j = 0; j < zeroToNine.length(); j++) {
if(message.charAt(i) == zeroToNine.charAt(j)){
encrypText += rot5.charAt(j);
}
} // inner for
} else if(temp.matches("[\\s\\w]+")) {
// ROT13
int chTemp = (int)temp.charAt(0) + rot13Key;
if (chTemp > 90){
chTemp -= 26;
encrypText += (char)chTemp;
} else {
encrypText += (char)chTemp;
}
} // if-else
} // for
System.out.println("Encrypted Text: " + encrypText);
}
}