-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
66 lines (59 loc) · 2.24 KB
/
script.js
File metadata and controls
66 lines (59 loc) · 2.24 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
// Client-side validation
var shiftInput = document.getElementById("shift");
shiftInput.addEventListener("input", function() {
var currentValue = shiftInput.value;
if (currentValue !== "" && !/^\d+$/.test(currentValue)) {
shiftInput.value = currentValue.slice(0, -1);
}
});
var plaintextInput = document.getElementById("plaintext");
plaintextInput.addEventListener("input", function() {
var currentValue = plaintextInput.value;
if (currentValue !== "" && !/^[a-zA-Z0-9]+$/.test(currentValue)) {
plaintextInput.value = currentValue.slice(0, -1);
}
});
// Live encryption
var plaintextInput = document.getElementById("plaintext");
var shiftInput = document.getElementById("shift");
var ciphertextOutput = document.getElementById("ciphertext");
function encrypt() {
var plaintext = plaintextInput.value;
var shift = parseInt(shiftInput.value);
var ciphertext = "";
for (var i = 0; i < plaintext.length; i++) {
var charCode = plaintext.charCodeAt(i);
if (charCode >= 65 && charCode <= 90) {
// Uppercase letters
ciphertext += String.fromCharCode((charCode - 65 + shift) % 26 + 65);
} else if (charCode >= 97 && charCode <= 122) {
// Lowercase letters
ciphertext += String.fromCharCode((charCode - 97 + shift) % 26 + 97);
} else {
ciphertext += plaintext.charAt(i);
}
}
ciphertextOutput.value = ciphertext;
}
plaintextInput.addEventListener("input", encrypt);
shiftInput.addEventListener("input", encrypt);
// Copy to clipboard
var copyButton = document.getElementById("copy-button");
copyButton.addEventListener("click", function() {
var ciphertextOutput = document.getElementById("ciphertext");
ciphertextOutput.select();
document.execCommand("copy");
});
// Save and load messages
var saveButton = document.getElementById("save-button");
saveButton.addEventListener("click", function() {
var ciphertextOutput = document.getElementById("ciphertext");
localStorage.setItem("ciphertext", ciphertextOutput.value);
});
var loadButton = document.getElementById("load-button");
loadButton.addEventListener("click", function() {
var ciphertextOutput = document.getElementById("ciphertext");
var savedCiphertext = localStorage.getItem("ciphertext");
if (savedCiphertext !== null) {
ciphertext
}});