-
Notifications
You must be signed in to change notification settings - Fork 0
/
decrypt.html
462 lines (432 loc) · 17.8 KB
/
decrypt.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
460
461
462
<!DOCTYPE html>
<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0'>
<meta name='apple-mobile-web-app-capable' content='yes'>
<meta name='apple-mobile-web-app-status-bar-style' content='black'>
<meta name='format-detection' content='telephone=no'>
<meta charset="utf-8">
<html>
<head>
<title>解密或签名验证</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/openpgp/4.10.4/openpgp.min.js"
integrity="sha384-/N1ZJTH7aZFvzCM9Jy9dQmQzroYQpB5L2qrNPgYpg1/tbwVDvaqWwGHfHeFhSpcn"
crossorigin="anonymous" defer></script>
<script src="https://cdn.staticfile.org/jquery/3.5.1/jquery.min.js"
integrity="sha384-ZvpUoO/+PpLXR1lu4jmpXWu80pZlYUAfxl5NsBMWOEPSjUn/6Z/hRTt8+pR6L4N2"
crossorigin="anonymous"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/4.5.0/js/bootstrap.bundle.min.js"
integrity="sha384-1CmrxMRARb6aLqgBO7yyAxTOQE2AKb9GfXnEo760AUcUmFx3ibVJJAzGytlQcNXd"
crossorigin="anonymous" defer></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/4.5.0/js/bootstrap.min.js"
integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI"
crossorigin="anonymous" defer></script>
<script src="js/dark.js" defer></script>
<script src="js/storepub.js" defer></script>
<script src="js/storepri.js" defer></script>
<script src="js/config.js" defer></script>
<script src="js/char_conv.js" defer></script>
<script src="js/loader.js" defer></script>
<script src="js/searchkey.js" defer></script>
<script src="js/blob.js" defer></script>
<script src="https://cdn.staticfile.org/clipboard.js/2.0.6/clipboard.min.js"
integrity="sha384-x6nRSkfSsKGBsvlLFHHNju+buS3zYUztVnTRz/0JKgOIk3ulS6bNce/vHOvYE2eY"
crossorigin="anonymous" defer></script>
</head>
<body class="container-fluid center">
<div class="loader"></div>
<script>
//搜索并导入公钥
async function importKey(keyid) {
$(".loader").fadeIn();
var pub = await searchById(keyid);
$(".loader").fadeOut();
if (typeof (pub) === "undefined") {
alert("没有在服务器上找到此公钥");
return
}
try {
var keydata = (await openpgp.key.readArmored(pub)).keys;
if (typeof (keydata[0]) === "undefined")
throw new Error("密钥已损坏");
} catch (e) {
alert("密钥读取错误!\n" + e);
return
}
var len = keydata.length;
for (var i = 0; i < len; ++i) { //如果有多个密钥,则拆开保存
try {
if (window.confirm("确认添加公钥?\n密钥信息:\n" + keydata[i].getUserIds().toString())) {
var name = window.prompt("要保存的别名:", keydata[i].getUserIds().toString());
if (name == null || name == "")
continue;
if (await PubSave(name, keydata[i].armor()))
alert("添加成功!");
}
} catch (e) {
alert("出现错误\n错误代码:" + e);
}
}
}
//解密和签名部分(文本文件)
async function decrypt(modal) { //如果modal为true,则已运行模态框
var enkeyids = new Array();
var signkeyids = new Array();
try {
var msgdata = (await openpgp.message.readArmored(document.getElementById("text").value));
} catch (e) {
alert("数据读取出错,请检查密文是否粘贴完整");
throw new Error(e);
}
//读取所有加密的公钥id
for (var i = 0; i < msgdata.getEncryptionKeyIds().length; ++i)
enkeyids[i] = msgdata.getEncryptionKeyIds()[i].toHex();
//已加密的信息签名需要解密后才能验证
if (msgdata.getEncryptionKeyIds().length === 0) {
//读取所有签名的私钥id
for (var i = 0; i < msgdata.getSigningKeyIds().length; ++i)
signkeyids[i] = msgdata.getSigningKeyIds()[i].toHex();
var pubid = await PubSearchId(signkeyids); //第一个找到的已知公钥id
}
var priid = await PriSearchId(enkeyids); //第一个找到的已知私钥id
if (priid !== -1) {
//文本被加密
const privateKeyArmored = `${PriRead(priid)}`; // 用作解密的私钥
var keyname = PriReadName(priid);
const { keys: [privateKey] } = await openpgp.key.readArmored(privateKeyArmored);
if (!privateKey.isDecrypted())
try {
if (modal !== true) { //如果没有运行模态框,则运行
document.getElementById("Modal_title").innerHTML = "请输入密钥:" + char_conv(keyname) + "的密码";
$("#Modal_password").modal("show");
return
}
const passphrase = `${document.getElementById("keypassword").value}`; // 私钥密码
await privateKey.decrypt(passphrase);
} catch (e) {
window.alert("私钥密码错误");
return
}
try {
var decrypted = await openpgp.decrypt({
message: msgdata, // 要解密的信息
privateKeys: [privateKey] //解密用的私钥
});
//判断是否有签名
if (decrypted.signatures.length !== 0) {
for (var i = 0; i < decrypted.signatures.length; ++i)
signkeyids[i] = decrypted.signatures[i].keyid.toHex();
pubid = await PubSearchId(signkeyids); //第一个找到的已知公钥id
if (pubid !== -1) {
var pub = (await openpgp.key.readArmored(PubRead(pubid))).keys;
//这是为了解决在某种情况下,GPG使用ECC加密和签名,签名验证失败的问题(疑似换行符问题)
decrypted = await openpgp.decrypt({
//上面解密的时候会修改msgdata,所以不得不重新读一遍
message: (await openpgp.message.readArmored(document.getElementById("text").value)),
privateKeys: [privateKey],
publicKeys: pub
});
var valid = false;
for (var i = 0; i < decrypted.signatures.length; ++i) {
valid = decrypted.signatures[i].valid;
if (valid)
break;
}
if (valid) {
document.getElementById("signmsg").innerHTML = "签名验证成功,信息未被篡改,由" + char_conv(PubReadName(pubid)) + "签署";
$("#signmsg").show("slow");
} else {
$("#signmsg").hide();
alert("签名验证失败\n信息被篡改");
}
} else {
$("#signmsg").hide();
alert("签名未验证\n没有找到已知的公钥\n所需公钥id有:" + signkeyids);
}
}
document.getElementById("output").value = decrypted.data;
} catch (e) {
window.alert("解密错误\n错误代码:" + e);
return
}
await openpgp.destroyWorker();
$("#output").show("slow");
$("#copy").show("slow");
} else if (msgdata.getEncryptionKeyIds().length !== 0) {
alert("错误:没有找到解密的私钥.\n请导入私钥.\n所需私钥id有:" + enkeyids);
return
} else if (msgdata.getEncryptionKeyIds().length === 0 && msgdata.getSigningKeyIds().length !== 0) { //未加密,仅进行了签名
if (pubid != -1) {
const publicKeyArmored = `${PubRead(pubid)}`;
try {
//判断是一般签名还是明文签名,这是一个临时解决方案
if (msgdata.packets[0].packets.length === 0)
msgdata = await openpgp.cleartext.readArmored(document.getElementById("text").value); //如果是明文签名,则使用这个读取
const verified = await openpgp.verify({
message: msgdata, // parse armored message
publicKeys: (await openpgp.key.readArmored(publicKeyArmored)).keys // for verification
});
const { valid } = verified.signatures[0];
if (valid) {
document.getElementById("signmsg").innerHTML = "签名验证成功,信息未被篡改,由" + char_conv(PubReadName(pubid)) + "签署";
$("#signmsg").show("slow");
} else {
$("#signmsg").hide();
alert("签名验证失败\n信息被篡改");
}
} catch (e) {
window.alert("签名验证错误\n错误代码:" + e);
return
}
await openpgp.destroyWorker();
} else {
alert("错误:没有找到验证签名的公钥。\n请导入公钥。\n所需公钥id有:" + signkeyids);
if (window.confirm("是否在服务器上搜索并导入此公钥?"))
await importKey(signkeyids);
return
}
} else if (msgdata.getEncryptionKeyIds().length === 0 && msgdata.getSigningKeyIds().length === 0) { //使用密码加密
if (modal !== true) { //如果没有运行模态框,则运行
document.getElementById("Modal_title").innerHTML = "请输入密码";
$("#Modal_password").modal("show");
return
}
var password = [document.getElementById("keypassword").value];
try {
const { data: decrypted } = await openpgp.decrypt({
message: msgdata, // input as Message object
passwords: password
});
document.getElementById("output").value = decrypted;
} catch (e) {
if (e == "Error: Error decrypting message: Session key decryption failed.")
window.alert("密码错误");
else if (e == "Error: Error decrypting message: Modification detected.")
window.alert("密码错误或密文被篡改");
else
window.alert("解密错误\n错误代码:" + e);
return
}
await openpgp.destroyWorker();
$("#output").show("slow");
$("#copy").show("slow");
} else {
alert("未知错误");
return
}
}
//解密和签名部分(二进制文件)
async function decrypt_bin(modal) { //如果modal为true,则已运行模态框
var enkeyids = new Array();
var signkeyids = new Array();
try {
var msgdata = (await openpgp.message.readArmored(document.getElementById("text").value));
} catch (e) {
alert("数据读取出错,请检查密文是否粘贴完整");
throw new Error(e);
}
//读取所有加密的公钥id
for (var i = 0; i < msgdata.getEncryptionKeyIds().length; ++i)
enkeyids[i] = msgdata.getEncryptionKeyIds()[i].toHex();
//已加密的信息签名需要解密后才能验证
if (msgdata.getEncryptionKeyIds().length === 0) {
//读取所有签名的私钥id
for (var i = 0; i < msgdata.getSigningKeyIds().length; ++i)
signkeyids[i] = msgdata.getSigningKeyIds()[i].toHex();
var pubid = await PubSearchId(signkeyids); //第一个找到的已知公钥id
}
var priid = await PriSearchId(enkeyids); //第一个找到的已知私钥id
if (priid !== -1) {
//被加密
const privateKeyArmored = `${PriRead(priid)}`; // 用作解密的私钥
var keyname = PriReadName(priid);
const { keys: [privateKey] } = await openpgp.key.readArmored(privateKeyArmored);
if (!privateKey.isDecrypted())
try {
if (modal !== true) { //如果没有运行模态框,则运行
document.getElementById("Modal_title").innerHTML = "请输入密钥:" + char_conv(keyname) + "的密码";
document.getElementById("Modal_btn").onclick = Function("decrypt_bin(true)");
$("#Modal_password").modal("show");
return
}
const passphrase = `${document.getElementById("keypassword").value}`; // 私钥密码
await privateKey.decrypt(passphrase);
} catch (e) {
window.alert("私钥密码错误");
return
}
try {
var decrypted = await openpgp.decrypt({
message: msgdata, // 要解密的信息
privateKeys: [privateKey], //解密用的私钥
format: 'binary'
});
//判断是否有签名
if (decrypted.signatures.length !== 0) {
for (var i = 0; i < decrypted.signatures.length; ++i)
signkeyids[i] = decrypted.signatures[i].keyid.toHex();
pubid = await PubSearchId(signkeyids); //第一个找到的已知公钥id
if (pubid !== -1) {
var pub = (await openpgp.key.readArmored(PubRead(pubid))).keys;
//这是为了解决在某种情况下,GPG使用ECC加密和签名,签名验证失败的问题(疑似换行符问题)
const decrypted = await openpgp.decrypt({
//上面解密的时候会修改msgdata,所以不得不重新读一遍
message: (await openpgp.message.readArmored(document.getElementById("text").value)),
privateKeys: [privateKey],
publicKeys: pub,
format: 'binary'
});
var valid = false;
for (var i = 0; i < decrypted.signatures.length; ++i) {
valid = decrypted.signatures[i].valid;
if (valid)
break;
}
if (valid) {
document.getElementById("signmsg").innerHTML = "签名验证成功,信息未被篡改,由" + char_conv(PubReadName(pubid)) + "签署";
$("#signmsg").show("slow");
} else {
$("#signmsg").hide();
alert("签名验证失败\n信息被篡改");
}
} else {
$("#signmsg").hide();
alert("签名未验证\n没有找到已知的公钥\n所需公钥id有:" + signkeyids);
}
}
download_bin(decrypted.data, decrypted.filename);
} catch (e) {
window.alert("解密错误\n错误代码:" + e);
return
}
await openpgp.destroyWorker();
} else if (msgdata.getEncryptionKeyIds().length !== 0) {
alert("错误:没有找到解密的私钥.\n请导入私钥.\n所需私钥id有:" + enkeyids);
return
} else if (msgdata.getEncryptionKeyIds().length === 0 && msgdata.getSigningKeyIds().length === 0) { //使用密码加密
if (modal !== true) { //如果没有运行模态框,则运行
document.getElementById("Modal_title").innerHTML = "请输入密码";
document.getElementById("Modal_btn").onclick = Function("decrypt_bin(true)");
$("#Modal_password").modal("show");
return
}
var password = [document.getElementById("keypassword").value];
try {
const decrypted = await openpgp.decrypt({
message: msgdata, // input as Message object
passwords: password,
format: 'binary'
});
download_bin(decrypted.data, decrypted.filename);
} catch (e) {
if (e == "Error: Error decrypting message: Session key decryption failed.")
window.alert("密码错误");
else if (e == "Error: Error decrypting message: Modification detected.")
window.alert("密码错误或密文被篡改");
else
window.alert("解密错误\n错误代码:" + e);
return
}
await openpgp.destroyWorker();
} else {
alert("未知错误");
return
}
}
function download_bin(data, filename) {
$("#download").show("slow");
var url = Uint8Array2blobUrl(data);
document.getElementById("show_file").href = url;
document.getElementById("download_file").href = url;
document.getElementById("download_file").download = filename;
}
function clean_bin() {
var url = document.getElementById("download_file").href;
if (url != '') {
window.URL.revokeObjectURL(url);
document.getElementById("show_file").href = '';
document.getElementById("download_file").href = '';
document.getElementById("download_file").download = '';
}
$("#download").hide("slow");
}
</script>
<script>
//元素显示和隐藏部分
$(function () {
$("#output").hide();
$("#signmsg").hide();
$("#copy").hide();
$("#download").hide("slow");
});
</script>
<script>
//清空和隐藏文本框
function cleanall() {
if (window.confirm("确认清空文本框?")) {
document.getElementById("text").value = '';
document.getElementById("output").value = ''
clean_bin();
$("#output").hide();
$("#signmsg").hide();
$("#copy").hide();
}
}
</script>
<script>
//复制部分的代码
$(function () {
var clipboard = new ClipboardJS('#copy');
clipboard.on('success', function (e) {
e.clearSelection();
document.getElementById("copy").innerHTML = "已复制";
setTimeout(function () { document.getElementById("copy").innerHTML = "复制输出"; }, 2000);
});
clipboard.on('error', function (e) {
alert('复制失败,您的浏览器不支持!');
});
});
</script>
<h1>解密</h1>
<textarea id="text" rows="10" class="textarea-inherit" placeholder="请粘贴密文或签名"
autofocus="autofocus"></textarea><br>
<div class="row mx-auto" style="width: 95%;">
<button type="button" onclick="decrypt(false)" class="col btn btn-primary">解密或验证签名(文本)</button>
<button type="button" onclick="decrypt_bin(false)" class="col btn btn-primary">解密或验证签名(二进制)</button>
<button data-clipboard-target="#output" id="copy" class="col btn btn-primary">复制明文</button>
<button type="button" class="btn btn-danger" onclick="cleanall()">清空文本框</button><br>
<a type="button" class="btn btn-light col-sm-3" href="encrypt.html">跳转到加密</a><br>
<a type="button" class="btn btn-light col-sm-3" href="encrypt_password.html">跳转到使用密码加密</a><br>
</div>
<div class="alert alert-success alert-dismissible mx-auto" id="signmsg" style="width: 95%;"></div><br>
<div id="download" class="row mx-auto" style="width: 95%;">
<a type="button" class="btn btn-primary col-sm-3 offset-sm-3" href="" id="show_file">查看文件</a>
<a type="button" class="btn btn-primary col-sm-3" href="" id="download_file">下载文件</a>
</div>
<textarea id="output" rows="20" class="textarea-inherit" readonly></textarea>
<!-- 用于输入密码的模态框 -->
<div class="modal fade" id="Modal_password">
<div class="modal-dialog">
<div class="modal-content">
<!-- 模态框头部 -->
<div class="modal-header">
<h4 class="modal-title" id="Modal_title"></h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- 模态框主体 -->
<div class="modal-body">
<form onsubmit="return false">
<input type="password" class="input-inherit" id="keypassword">
</form>
</div>
<!-- 模态框底部 -->
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
<button type="button" class="btn btn-primary" data-dismiss="modal" id="Modal_btn"
onclick="decrypt(true)">确认</button>
</div>
</div>
</div>
</div>
</body>
</html>