forked from PQC-at-QUT/engagement
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
192 lines (137 loc) · 3.39 KB
/
index.html
File metadata and controls
192 lines (137 loc) · 3.39 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>PQC at QUT</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.2.0/crypto-js.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jsencrypt/bin/jsencrypt.min.js"></script>
<style>
body{
font-family: Arial;
max-width:700px;
margin:40px auto;
}
textarea,input,select{
width:100%;
padding:8px;
margin-top:10px;
font-size:16px;
}
textarea{
height:90px;
}
#algorithm {
font-size: 16px;
font-weight: bold;
padding: 10px;
}
#ciphertext{
background:#f3f3f3;
}
#rsaSection{
display:none;
}
</style>
</head>
<body>
<h1>Encryption Demo</h1>
<label>Algorithm</label>
<select id="algorithm">
<option value="caesar">Caesar</option>
<option value="vigenere">Vigenère</option>
<option value="xor">XOR</option>
<option value="aes">AES</option>
<option value="rsa">RSA</option>
</select>
<label>Message</label>
<textarea id="plaintext"></textarea>
<label>Key / Shift</label>
<input id="key">
<h3>Ciphertext</h3>
<textarea id="ciphertext" readonly></textarea>
<div id="rsaSection">
<h3>Public Key</h3>
<textarea id="publickey" readonly></textarea>
</div>
<script>
let rsa = new JSEncrypt({default_key_size: 1024});
rsa.getKey(); // generate key pair
function update(){
const algo = document.getElementById("algorithm").value;
const text = document.getElementById("plaintext").value;
const key = document.getElementById("key").value;
const rsaSection = document.getElementById("rsaSection");
document.getElementById("publickey").value = rsa.getPublicKey();
// Display public key only if RSA is selected
if(algo === "rsa"){
rsaSection.style.display = "block";
} else{
rsaSection.style.display = "none";
}
if(text === "" || key === "" ){
document.getElementById("ciphertext").value = "Input plaintext message and key/shift.";
return;
}
let result = "";
if(algo === "rsa"){
result = rsa.encrypt(text);
}
if(algo === "caesar"){
result = caesar(text, parseInt(key));
}
if(algo === "vigenere"){
result = vigenere(text, key);
}
if(algo === "xor"){
result = xorCipher(text, key);
}
if(algo === "aes"){
result = CryptoJS.AES.encrypt(text, key).toString();
}
document.getElementById("ciphertext").value = result;
}
function caesar(text, shift){
text = text.toUpperCase();
let result = "";
for(let i=0;i<text.length;i++){
let c = text.charCodeAt(i);
if(c>=65 && c<=90){
result += String.fromCharCode((c-65+shift)%26+65);
}else{
result += text[i];
}
}
return result;
}
function vigenere(text,key){
text = text.toUpperCase();
key = key.toUpperCase();
let result = "";
let k=0;
for(let i=0;i<text.length;i++){
let c=text.charCodeAt(i);
if(c>=65 && c<=90){
let shift = key.charCodeAt(k%key.length)-65;
result+=String.fromCharCode((c-65+shift)%26+65);
k++;
}else{
result+=text[i];
}
}
return result;
}
function xorCipher(text,key){
let result="";
for(let i=0;i<text.length;i++){
let t=text.charCodeAt(i);
let k=key.charCodeAt(i%key.length);
result+=String.fromCharCode(t^k);
}
return result;
}
document.getElementById("plaintext").addEventListener("input",update);
document.getElementById("key").addEventListener("input",update);
document.getElementById("algorithm").addEventListener("change",update);
</script>
</body>
</html>