-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaesarCipher.java
More file actions
103 lines (91 loc) · 4.25 KB
/
CaesarCipher.java
File metadata and controls
103 lines (91 loc) · 4.25 KB
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
// CaesarCipher encryption
// Sarthak Gupta
// 4/18/2020
// CaesarCipher class encrypts messages based on 1 key and 2 keys.
public class CaesarCipher {
// encrypts input string by shifting over letters by numerical key
public static String encryptOne(String input, int key) {
String encrypted = input;
String upperAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String lowerAlphabet = upperAlphabet.toLowerCase();
String shiftedUpperAlphabet = upperAlphabet.substring(key) + upperAlphabet.substring(0, key);
String shiftedLowerAlphabet = shiftedUpperAlphabet.toLowerCase();
for (int i = 0; i < encrypted.length(); i++) {
char currChar = encrypted.charAt(i);
if (Character.isUpperCase(currChar)) {
int index = upperAlphabet.indexOf(currChar);
if (index != -1) {
char newChar = shiftedUpperAlphabet.charAt(index);
String part1 = encrypted.substring(0, i);
String part2 = encrypted.substring(i+1);
encrypted = part1 + newChar + part2;
}
}
if (Character.isLowerCase(currChar)) {
int index = lowerAlphabet.indexOf(currChar);
if (index != -1) {
char newChar = shiftedLowerAlphabet.charAt(index);
String part1 = encrypted.substring(0, i);
String part2 = encrypted.substring(i+1);
encrypted = part1 + newChar + part2;
}
}
}
return encrypted.toString();
}
// encrypts every odd character in string by first key and every even character in string by second key
public static String encryptTwo(String input, int key1, int key2) {
String encrypted = input;
String upperAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String lowerAlphabet = upperAlphabet.toLowerCase();
// for odd characters in string
for (int i = 0; i < encrypted.length(); i+=2) {
char currChar = encrypted.charAt(i);
String shiftedUpperAlphabet1 = upperAlphabet.substring(key1) + upperAlphabet.substring(0, key1);
String shiftedLowerAlphabet1 = shiftedUpperAlphabet1.toLowerCase();
if (Character.isUpperCase(currChar)) {
int index = upperAlphabet.indexOf(currChar);
if (index != -1) {
char newChar = shiftedUpperAlphabet1.charAt(index);
String part1 = encrypted.substring(0, i);
String part2 = encrypted.substring(i+1);
encrypted = part1 + newChar + part2;
}
}
if (Character.isLowerCase(currChar)) {
int index = lowerAlphabet.indexOf(currChar);
if (index != -1) {
char newChar = shiftedLowerAlphabet1.charAt(index);
String part1 = encrypted.substring(0, i);
String part2 = encrypted.substring(i+1);
encrypted = part1 + newChar + part2;
}
}
}
// for even characters in string
for (int i = 1; i < encrypted.length(); i+=2) {
char currChar = encrypted.charAt(i);
String shiftedUpperAlphabet2 = upperAlphabet.substring(key2) + upperAlphabet.substring(0, key2);
String shiftedLowerAlphabet2 = shiftedUpperAlphabet2.toLowerCase();
if (Character.isUpperCase(currChar)) {
int index = upperAlphabet.indexOf(currChar);
if (index != -1) {
char newChar = shiftedUpperAlphabet2.charAt(index);
String part1 = encrypted.substring(0, i);
String part2 = encrypted.substring(i+1);
encrypted = part1 + newChar + part2;
}
}
if (Character.isLowerCase(currChar)) {
int index = lowerAlphabet.indexOf(currChar);
if (index != -1) {
char newChar = shiftedLowerAlphabet2.charAt(index);
String part1 = encrypted.substring(0, i);
String part2 = encrypted.substring(i+1);
encrypted = part1 + newChar + part2;
}
}
}
return encrypted.toString();
}
}