-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaesar.js
72 lines (57 loc) · 1.49 KB
/
caesar.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var plain1 = document.getElementById('plain1');
var cipher1 = document.getElementById('cipher1');
var cipher2 = document.getElementById('cipher2');
var plain2 = document.getElementById('plain2');
var encryptBtn = document.getElementById('encryptBtn');
var decryptBtn = document.getElementById('decryptBtn');
var num = document.getElementById('num');
var error = document.getElementById('error');
var reset = document.getElementById('reset');
function isUpper(ch) {
return (ch.charCodeAt(0) > 64 && ch.charCodeAt(0) < 91);
}
function isLower(ch) {
return (ch.charCodeAt(0) > 96 && ch.charCodeAt(0) < 123);
}
encryptBtn.addEventListener("click", function() {
crypt(parseInt(num.value),plain1,cipher1);
},false);
decryptBtn.addEventListener("click", function() {
crypt(parseInt(num.value) * -1, cipher2, plain2);
},false);
reset.addEventListener("click", function() {
plain1.value = " ";
plain2.value = " ";
cipher1.value = " ";
cipher2.value = " ";
},false);
function crypt(k, plain, cipher) {
var a = [];
var x;
if(!k) {
error.innerHTML = "Please Enter Key First";
return;
}
for(var i = 0; i < plain.value.length; i++) {
var ch = plain.value.charAt(i);
x = (ch.charCodeAt(0) + k);
if (isUpper(ch)) {
if( x > 90 ) {
x -= 26;
}
if(x < 65) {
x += 26;
}
a.push(String.fromCharCode(x));
} else if (isLower(ch)) {
if( x > 122 ) {
x -= 26;
}
if(x < 97) {
x += 26;
}
a.push(String.fromCharCode(x));
}
}
cipher.value = a.join('');
}