-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
47 lines (41 loc) · 1.86 KB
/
script.js
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
function encrypt() {
var message = document.getElementById('message').value;
var shift = parseInt(document.getElementById('shift').value);
// 65 TO 90 FOR A-Z
//97 TO 122 FOR a-z
var encryptedMessage = '';
for (var i = 0; i < message.length; i++) {
var charCode = message.charCodeAt(i);
if (65 <= charCode && charCode <= 90) {
encryptedMessage += String.fromCharCode(((charCode - 65 + shift) % 26) + 65);
} else if (97 <= charCode && charCode <= 122) {
encryptedMessage += String.fromCharCode(((charCode - 97 + shift) % 26) + 97);
} else {
displayResults(message, shift, 'Invalid Character: ' + message[i]);
return;
}
}
displayResults(message, shift, encryptedMessage);
}
function decrypt() {
var message = document.getElementById('message').value;
var shift = parseInt(document.getElementById('shift').value);
var decryptedMessage = '';
for (var i = 0; i < message.length; i++) {
var charCode = message.charCodeAt(i);
if (65 <= charCode && charCode <= 90) {
decryptedMessage += String.fromCharCode(((charCode - 65 - shift + 26) % 26) + 65);
} else if (97 <= charCode && charCode <= 122) {
decryptedMessage += String.fromCharCode(((charCode - 97 - shift + 26) % 26) + 97);
} else {
displayResults(message, shift, 'Invalid Character: ' + message[i]);
return;
}
}
displayResults(message, shift, decryptedMessage);
}
function displayResults(originalMessage, shift, resultMessage) {
document.getElementById('originalMessage').innerText = 'Original Message: ' + originalMessage;
document.getElementById('shiftValue').innerText = 'Shift Value: ' + shift;
document.getElementById('encryptedMessage').innerText = 'Encrypted/Decrypted Message: ' + resultMessage;
}