-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaffine.html
More file actions
125 lines (125 loc) · 3.24 KB
/
Copy pathaffine.html
File metadata and controls
125 lines (125 loc) · 3.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Affine Cipher</title>
<style type="text/css">
body{text-align:center;}
.center{width:600px;height:500px;position:absolute;left:50%;margin-left:-300px;border:2pxsolid;}
.center2{width:600px;height:550px;position:absolute;left:50%;margin-left:-300px;top:600px;border:2pxsolid;}
.comments{width:50%;overflow:auto;word-break:break-all;}
</style>
</head>
<body>
<h2 style="background-color: pink">Affine Cipher: Encryption - Decrption</h2>
<br>
<div class="center">
<br>
<br>
<form>
Plaintext:
<textarea class="comments" rows="10" cols="10" id="input1"></textarea><br>
k_1:
<input type="text" name="k_1" id="input2"><br>
k_2:
<input type="text" name="k_2" id="input3"><br>
<button type="button" onclick="eFunction()">Encrypt</button><br>
Ciphertext:
<textarea class="comments" rows="10" cols="10" id="output1"></textarea>
</form>
</div>
<div class="center2">
<br>
<br>
Ciphertext:
<textarea class="comments" rows="10" cols="10" id="input4"></textarea><br>
k_1:
<input type="text" name="k_1" id="input5"><br>
k_2:
<input type="text" name="k_2" id="input6"><br>
<button type="button" onclick="dFunction()">Decrypt</button><br>
Plaintext:
<textarea class="comments" rows="10" cols="10" id="output2"></textarea>
</div>
<script type="text/javascript">
function eFunction() {
var k1;
var k2;
var code2 = "";
var plaintext = document.getElementById("input1").value;
plaintext = plaintext.toLowerCase();
var result = "";
var num;
k1 = document.getElementById("input2").value;
k1 = parseInt(k1);
k2 = document.getElementById("input3").value;
k2 = parseInt(k2);
if (k1 < 26 && isNaN(k1) == false && k1 % 2 != 0 && k1 != 13 && k2 >= 0 && k2 <= 25) {
for (var i = 0; i <= plaintext.length - 1; i++) {
code = plaintext[i].charCodeAt() - 97;
code = k1 * code + k2;
code = code % 26;
num = code;
code2 += String.fromCharCode(num + 97);
result = code2 + ""
}
document.getElementById("output1").value = result
} else {
document.getElementById("output1").value = "k1 or k2 is not correct"
}
}
function dFunction() {
var k1;
var k11;
k11 = parseInt(k11);
var k2;
var code2 = "";
var ciphertext = document.getElementById("input4").value;
ciphertext = ciphertext.toLowerCase();
var result = "";
var num;
k1 = document.getElementById("input5").value;
k1 = parseInt(k1);
k2 = document.getElementById("input6").value;
k2 = parseInt(k2);
if (k1 < 26 && isNaN(k1) == false && k1 % 2 != 0 && k1 != 13 && k2 >= 0 && k2 <= 25) {
if (k1 == 1) {
k11 = 1
}
if (k1 == 3) {
k11 = 9
}
if (k1 == 5) {
k11 = 21
}
if (k1 == 7) {
k11 = 15
}
if (k1 == 11) {
k11 = 19
}
if (k1 == 17) {
k11 = 23
}
if (k1 == 25) {
k11 = 25
}
for (var i = 0; i <= ciphertext.length - 1; i++) {
code = ciphertext[i].charCodeAt() - 97;
code = k11 * (code - k2);
code = code % 26;
if (code < 0) {
code += 26
}
num = code;
code2 += String.fromCharCode(num + 97);
result = code2 + ""
}
document.getElementById("output2").value = result
} else {
document.getElementById("output2").value = "k1 or k2 is not correct"
}
}
</script>
</body>
</html>