-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesarcipher.js
More file actions
45 lines (43 loc) · 1.33 KB
/
caesarcipher.js
File metadata and controls
45 lines (43 loc) · 1.33 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
function CaesarCipher({ input, shift }) {
const originalOrderUp = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const originalOrderLow = "abcdefghijklmnopqrstuvwxyz";
let result = "";
let foundFlag = false;
for (let i = 0; i < input.length; i++) {
foundFlag = false;
for (let j = 0; j < originalOrderUp.length; j++) {
if (input[i] == originalOrderUp[j]) {
result = result + originalOrderUp[(j + shift) % 26];
foundFlag = true;
}
}
for (let k = 0; k < originalOrderLow.length; k++) {
if (input[i] == originalOrderLow[k]) {
result = result + originalOrderLow[(k + shift) % 26];
foundFlag = true;
}
}
if (foundFlag == false) {
result = result + input[i];
}
}
this.result = result;
}
CaesarCipher.deCipher = function (encryptedText, shifted) {
let decipher = new CaesarCipher({
"input": encryptedText,
"shift": (shifted * -1) + 26
});
return decipher.result;
}
CaesarCipher.crack = function (encryptedText) {
let allShifts = []
for (let i = 0; i < 26; i++) {
let shift = new CaesarCipher({
"input": encryptedText,
"shift": i
});
allShifts.push(shift.result)
}
return allShifts
}