-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
48 lines (39 loc) · 1.15 KB
/
app.js
File metadata and controls
48 lines (39 loc) · 1.15 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
(() => {
// https://en.wikipedia.org/wiki/Caesar_cipher
class CaesarCipher {
static RE = /[a-z]/;
constructor(shift) {
this.shift = shift;
}
encode(plaintext) {
let ciphertext = "";
for (let i = 0; i < plaintext.length; i++) {
if (this.constructor.RE.test(plaintext.charAt(i))) {
ciphertext += String.fromCharCode(
((plaintext.charCodeAt(i) - 97 + this.shift) % 26) + 97,
);
} else {
ciphertext += plaintext.charAt(i);
}
}
return ciphertext;
}
decode(ciphertext) {
let plaintext = "";
for (let i = 0; i < ciphertext.length; i++) {
if (this.constructor.RE.test(ciphertext.charAt(i))) {
plaintext += String.fromCharCode(
((ciphertext.charCodeAt(i) - 97 + 26 - this.shift) % 26) + 97,
);
} else {
plaintext += ciphertext.charAt(i);
}
}
return plaintext;
}
}
const encoded = "13x.0ns60@fvzcyrybtva.pbz";
const cipher = new CaesarCipher(13);
const decoded = cipher.decode(encoded);
document.getElementById("email").href = `mailto:${decoded}`;
})();