-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshiftCipher.js
More file actions
61 lines (49 loc) · 2.22 KB
/
shiftCipher.js
File metadata and controls
61 lines (49 loc) · 2.22 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
/*
A shift cipher takes a plain text message and shifts each letter forward in the alphabet by a given number. For example, a shift cipher with a shift of 1 would turn the string 'hello' to 'ifmmp'.
encrypt: takes a plain text string and returns a capitalized string with each letter shifted forward in the alphabet based on the set shift value.
decrypt: takes an encrypted message and returns a lower case string with each letter shifted back in the alphabet based on the set shift value.
In both methods, any character outside the alphabet should remain the same.
But if a character is shifted outside the alphabet in either direction it should be wrapped around to the other side. For example, encrypting a y with a shift of 4 results in C and decrypting an A with a shift of 1 result in z.
*/
class ShiftCipher {
constructor(number) {
this.number = number;
}
encrypt(string) {
let newString = '';
for (const char of string) {
if((char.charCodeAt() >= 97 && char.charCodeAt() <= 122) || ((char.charCodeAt() >= 65 && char.charCodeAt() <= 90))) {
let upChar = char.toUpperCase();
let atChar = upChar.charCodeAt() + this.number;
if(atChar > 90) {
atChar = 64 + (atChar - 90);
newString += String.fromCharCode(atChar);
} else {
newString += String.fromCharCode(atChar);
}
} else {
newString += char;
}
} return newString;
}
decrypt(string) {
let newString = '';
for (const char of string) {
if((char.charCodeAt() >= 97 && char.charCodeAt() <= 122) || ((char.charCodeAt() >= 65 && char.charCodeAt() <= 90))) {
let downChar = char.toLowerCase();
let atChar = downChar.charCodeAt() - this.number;
if(atChar < 97) {
atChar = 123 - (97 - atChar);
newString += String.fromCharCode(atChar);
} else {
newString += String.fromCharCode(atChar);
}
} else {
newString += char;
}
} return newString;
}
}
const cipher = new ShiftCipher(2);
console.log(cipher.encrypt('I love to code!')); // returns 'K NQXG VQ EQFG!'
console.log(cipher.decrypt('K <3 OA RWRRA')); // returns 'i <3 my puppy'