-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex-cn.html
459 lines (452 loc) · 20.5 KB
/
index-cn.html
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4"
crossorigin="anonymous"></script>
<title>SHA256 with RSA</title>
</head>
<body class="container-md">
<div class="row my-4">
<div class="col-md-12">
<h1>SHA256 with RSA</h1>
</div>
</div>
<div class="row">
<div class="col-md-3">
<ul class="navbar-nav flex-grow-1 my-4" style="position: fixed;">
<li style="margin-bottom: 0.5rem;">
滚动到
</li>
<li style="margin-bottom: 0.5rem;">
<a href="#generate" style="text-decoration: none;">生成</a>
</li>
<li style="margin-bottom: 0.5rem;">
<a href="#sign" style="text-decoration: none;">签名</a>
</li>
<li style="margin-bottom: 0.5rem;">
<a href="#verify" style="text-decoration: none;">验证签名</a>
</li>
<li style="margin-bottom: 0.5rem;">
<a href="#encrypt" style="text-decoration: none;">加密</a>
</li>
<li style="margin-bottom: 0.5rem;">
<a href="#decrypt" style="text-decoration: none;">解密</a>
</li>
</ul>
</div>
<div class="col-md-9">
<div class="card my-4">
<div class="card-header" id="generate">生成</div>
<div class="card-body">
<p><i>生成一对新的 PEM 格式的 RSA 钥匙。</i></p>
<div>
<label>
复杂度
<input type="number" id="generate-complexity" value="2048" class="form-control" />
</label>
</div>
<table class="table">
<thead>
<tr>
<th>私钥</th>
<th>公钥</th>
</tr>
</thead>
<tbody>
<tr>
<td><a id="privateKeyLink" style="display: none; text-decoration: none;">下载</a></td>
<td><a id="publicKeyLink" style="display: none; text-decoration: none;">下载</a></td>
</tr>
</tbody>
</table>
<p id="generate-error" style="color: red;"></p>
</div>
<div class="card-footer">
<button class="btn btn-success" onclick="generate()">
开始
</button>
<button class="btn disabled" style="display: none; color: green;" id="update_generate_msg">
已更新
</button>
</div>
</div>
<div class="card my-4">
<div class="card-header" id="sign">签名</div>
<div class="card-body">
<p><i>用私钥签署一条消息,证明消息由你创建。</i></p>
<div>
<label>
私钥
<input type="file" id="sign-private-key" class="form-control" />
</label>
</div>
<label for="sign-content">消息</label>
<textarea rows="5" id="sign-content" class="form-control"></textarea>
<label for="sign-signature">签名</label>
<textarea rows="3" id="sign-signature" class="form-control" readonly disabled></textarea>
<p id="sign-error" style="color: red;"></p>
</div>
<div class="card-footer">
<button class="btn btn-success" onclick="sign()">
开始
</button>
<button class="btn disabled" style="display: none; color: green;" id="update_sign_msg">
已更新
</button>
</div>
</div>
<div class="card my-4">
<div class="card-header" id="verify">验证签名</div>
<div class="card-body">
<p><i>验证一条消息是否是(和下面的公钥配对的)私钥持有者创建的。</i></p>
<div>
<label>
公钥
<input type="file" id="verify-public-key" class="form-control" />
</label>
</div>
<label for="verify-content">消息</label>
<textarea rows="5" id="verify-content" class="form-control"
placeholder="Blank spaces and new lines at the end can lead to different result."></textarea>
<label for="verify-signature">签名</label>
<textarea rows="3" id="verify-signature" class="form-control"></textarea>
<p>Result: <span id="verify-result"></span></p>
<p id="verify-error" style="color: red;"></p>
</div>
<div class="card-footer">
<button class="btn btn-success" onclick="verify()">
开始
</button>
<button class="btn disabled" style="display: none; color: green;" id="update_verify_msg">
已更新
</button>
</div>
</div>
<div class="card my-4">
<div class="card-header" id="encrypt">加密</div>
<div class="card-body">
<p><i>加密一条消息,该消息只能被对应的私钥持有者解密。</i></p>
<div>
<label>
公钥
<input type="file" id="encrypt-public-key" class="form-control" />
</label>
</div>
<label for="encrypt-content">消息</label>
<textarea rows="5" id="encrypt-content" class="form-control"></textarea>
<label for="encrypt-result">加密内容</label>
<textarea rows="8" id="encrypt-result" class="form-control" readonly disabled></textarea>
<p id="encrypt-error" style="color: red;"></p>
</div>
<div class="card-footer">
<button class="btn btn-success" onclick="encrypt()">
开始
</button>
<button class="btn disabled" style="display: none; color: green;" id="update_encrypt_msg">
已更新
</button>
</div>
</div>
<div class="card my-4">
<div class="card-header" id="decrypt">解密</div>
<div class="card-body">
<p><i>解密被对应的公钥加密的消息。</i></p>
<div>
<label>
私钥
<input type="file" id="decrypt-private-key" class="form-control" />
</label>
</div>
<label for="decrypt-content">加密内容</label>
<textarea rows="8" id="decrypt-content" class="form-control" placeholder="按以下格式粘贴:
{
"encrypted_session_key": "...",
"nonce": "...",
"encrypted_message": "..."
}"></textarea>
<label for="decrypt-result">消息</label>
<textarea rows="5" id="decrypt-result" class="form-control" readonly disabled></textarea>
<p id="decrypt-error" style="color: red;"></p>
</div>
<div class="card-footer">
<button class="btn btn-success" onclick="decrypt()">
开始
</button>
<button class="btn disabled" style="display: none; color: green;" id="update_decrypt_msg">
已更新
</button>
</div>
</div>
</div>
</div>
<script>
function arrayBufferToBase64(buffer) {
let binary = "";
const bytes = new Uint8Array(buffer);
const len = bytes.byteLength;
for (let i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return btoa(binary);
}
function base64ToArrayBuffer(base64) {
const binaryString = atob(base64);
const len = binaryString.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes.buffer;
}
function createDownloadLink(keyBase64, fileName, linkId) {
const keyBlob = new Blob([keyBase64], { type: "application/octet-stream" });
const url = URL.createObjectURL(keyBlob);
const link = document.getElementById(linkId);
link.href = url;
link.download = fileName;
link.style.display = "inline"; // Make sure the link is visible
}
async function generate() {
const n = document.getElementById("generate-complexity").value;
const error = document.getElementById("generate-error");
error.innerText = "";
try {
const keyPair = await crypto.subtle.generateKey(
{
name: "RSASSA-PKCS1-v1_5",
modulusLength: n,
publicExponent: new Uint8Array([1, 0, 1]),
hash: "SHA-256",
},
true,
["sign", "verify"]
);
const privateKey = await crypto.subtle.exportKey("pkcs8", keyPair.privateKey);
const publicKey = await crypto.subtle.exportKey("spki", keyPair.publicKey);
const privateKeyBase64 = "-----BEGIN RSA PRIVATE KEY-----\n" + arrayBufferToBase64(privateKey) + "\n-----END RSA PRIVATE KEY-----";
const publicKeyBase64 = "-----BEGIN PUBLIC KEY-----\n" + arrayBufferToBase64(publicKey) + "\n-----BEGIN PUBLIC KEY-----";
createDownloadLink(privateKeyBase64, "private_key.txt", "privateKeyLink");
createDownloadLink(publicKeyBase64, "public_key.txt", "publicKeyLink");
} catch (e) {
error.innerText = e.toString();
return;
}
const msg = document.getElementById("update_generate_msg");
msg.style.display = "inline-block";
setTimeout(function () {
msg.style.display = "none";
}, 1000);
}
async function sign() {
const content = document.getElementById("sign-content").value;
const private_key_node = document.getElementById("sign-private-key");
const error = document.getElementById("sign-error");
error.innerText = "";
if (private_key_node.files.length === 0) {
error.innerText = "Private key should not be empty.";
return;
}
const reader = new FileReader();
try {
reader.readAsText(private_key_node.files[0]);
} catch (e) {
error.innerText = e.toString();
}
reader.onload = async function (e) {
try {
const private_key = e.target.result.toString();
const private_key_obj = await crypto.subtle.importKey(
"pkcs8",
base64ToArrayBuffer(private_key.replace(/(-----(BEGIN|END) RSA PRIVATE KEY-----|\n)/g, "")),
{
name: "RSASSA-PKCS1-v1_5",
hash: "SHA-256",
},
false,
["sign"]
);
const signature = await crypto.subtle.sign(
"RSASSA-PKCS1-v1_5", private_key_obj, new TextEncoder().encode(content));
document.getElementById("sign-signature").value = arrayBufferToBase64(signature);
} catch (e) {
error.innerText = e.toString();
}
const msg = document.getElementById("update_sign_msg");
msg.style.display = "inline-block";
setTimeout(function () {
msg.style.display = "none";
}, 1000);
};
}
async function verify() {
const error = document.getElementById("verify-error");
error.innerText = "";
const is_valid_node = document.getElementById("verify-result");
is_valid_node.innerText = "";
const public_key_node = document.getElementById("verify-public-key");
if (public_key_node.files.length === 0) {
error.innerText = "Public key should not be empty.";
return;
}
const reader = new FileReader();
try {
reader.readAsText(public_key_node.files[0]);
} catch (e) {
error.innerText = e.toString();
}
reader.onload = async function (e) {
try {
const public_key = e.target.result.toString();
const public_key_obj = await crypto.subtle.importKey(
"spki",
base64ToArrayBuffer(public_key.replace(/(-----(BEGIN|END) PUBLIC KEY-----|\n)/g, "")),
{
name: "RSASSA-PKCS1-v1_5",
hash: "SHA-256",
},
false,
["verify"]
);
const signature = document.getElementById("verify-signature").value;
const content = document.getElementById("verify-content").value;
const is_valid = await crypto.subtle.verify(
"RSASSA-PKCS1-v1_5", public_key_obj, base64ToArrayBuffer(signature),
new TextEncoder().encode(content)
);
if (is_valid) {
is_valid_node.style.color = "green";
is_valid_node.innerText = "VALID";
} else {
is_valid_node.style.color = "red";
is_valid_node.innerText = "INVALID";
}
} catch (e) {
error.innerText = e.toString();
}
};
const msg = document.getElementById("update_verify_msg");
msg.style.display = "inline-block";
setTimeout(function () {
msg.style.display = "none";
}, 1000);
}
async function encrypt() {
const public_key_node = document.getElementById("encrypt-public-key");
const error = document.getElementById("encrypt-error");
error.innerText = "";
const encrypted_result = document.getElementById("encrypt-result");
encrypted_result.innerHTML = "";
if (public_key_node.files.length === 0) {
error.innerText = "Public key should not be empty.";
return;
}
const reader = new FileReader();
try {
reader.readAsText(public_key_node.files[0]);
} catch (e) {
error.innerText = e.toString();
}
reader.onload = async function (e) {
try {
const public_key = e.target.result.toString();
const public_key_obj = await crypto.subtle.importKey(
"spki",
base64ToArrayBuffer(public_key.replace(/(-----(BEGIN|END) PUBLIC KEY-----|\n)/g, "")),
{
name: "RSA-OAEP",
hash: "SHA-256",
},
false,
["encrypt"]
);
const sessionKey = crypto.getRandomValues(new Uint8Array(32));
const encryptedSessionKey = await crypto.subtle.encrypt({ name: "RSA-OAEP" }, public_key_obj, sessionKey);
const content = document.getElementById("encrypt-content").value;
const aesKey = await crypto.subtle.importKey("raw", sessionKey, { name: "AES-GCM" }, false, ["encrypt"]);
const cipherAes = await crypto.subtle.encrypt(
{
name: "AES-GCM",
iv: sessionKey.slice(0, 12), // 12-byte nonce for AES-GCM
},
aesKey,
new TextEncoder().encode(content)
);
encrypted_result.innerHTML = JSON.stringify(
{
encrypted_session_key: arrayBufferToBase64(encryptedSessionKey),
nonce: arrayBufferToBase64(sessionKey.slice(0, 12)),
encrypted_message: arrayBufferToBase64(cipherAes),
},
null,
4
);
} catch (e) {
error.innerText = e.toString();
}
};
const msg = document.getElementById("update_encrypt_msg");
msg.style.display = "inline-block";
setTimeout(function () {
msg.style.display = "none";
}, 1000);
}
async function decrypt() {
const content = document.getElementById("decrypt-content").value;
const private_key_node = document.getElementById("decrypt-private-key");
const error = document.getElementById("decrypt-error");
error.innerText = "";
if (private_key_node.files.length === 0) {
error.innerText = "Private key should not be empty.";
return;
}
const reader = new FileReader();
try {
reader.readAsText(private_key_node.files[0]);
} catch (e) {
error.innerText = e.toString();
}
const message_node = document.getElementById("decrypt-result");
message_node.innerHTML = "";
reader.onload = async function (e) {
try {
const private_key = e.target.result.toString();
const private_key_obj = await crypto.subtle.importKey(
"pkcs8",
base64ToArrayBuffer(private_key.replace(/(-----(BEGIN|END) RSA PRIVATE KEY-----|\n)/g, "")),
{
name: "RSA-OAEP",
hash: "SHA-256",
},
false,
["decrypt"]
);
const parsedContent = JSON.parse(content);
const encryptedSessionKey = base64ToArrayBuffer(parsedContent.encrypted_session_key);
const sessionKey = await crypto.subtle.decrypt({ name: "RSA-OAEP" }, private_key_obj, encryptedSessionKey);
const cipherAes = await crypto.subtle.decrypt(
{
name: "AES-GCM",
iv: base64ToArrayBuffer(parsedContent.nonce),
},
await crypto.subtle.importKey("raw", sessionKey, { name: "AES-GCM" }, false, ["decrypt"]),
base64ToArrayBuffer(parsedContent.encrypted_message)
);
message_node.innerHTML = new TextDecoder().decode(cipherAes);
} catch (e) {
error.innerText = e.toString();
}
};
const msg = document.getElementById("update_decrypt_msg");
msg.style.display = "inline-block";
setTimeout(function () {
msg.style.display = "none";
}, 1000);
}
</script>
</body>
</html>