-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypt.html
executable file
·413 lines (393 loc) · 15.5 KB
/
encrypt.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
<!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="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 encrypt(sign, modal) { //sign:true为启用签名,modal:true为从模态框运行
//判断是否选择了密钥
if (document.getElementById("pubdata").value === '' || (sign && document.getElementById("pridata") === '')) {
window.alert("请选择密钥!");
throw new Error("请选择密钥!");
}
// put keys in backtick (``) to avoid errors caused by spaces or tabs
const publicKeyArmored = `${PubRead(document.getElementById("pubdata").value)}`;
//判断是否启用签名
if (sign) {
const privateKeyArmored = `${PriRead(document.getElementById("pridata").value)}`; // encrypted private key
var keyname = PriReadName(document.getElementById("pridata").value);
//是否加密到签名者
if (document.getElementById("entosigner").checked) {
var keyidlen = (await openpgp.key.readArmored(privateKeyArmored)).keys[0].getKeyIds().length;
var keyid = new Array();
for (var ii = 0; ii < keyidlen; ++ii)
keyid[ii] = (await openpgp.key.readArmored(privateKeyArmored)).keys[0].getKeyIds()[ii].toHex();
var pubid = await PubSearchId(keyid); //搜索签名者的公钥
if (pubid === -1) {
alert("没有找到签名者的公钥\n信息将不会加密到签名者");
var pubkeytmp = [`${publicKeyArmored}`];
} else {
var pubkeytmp = [`${publicKeyArmored}`, `${PubRead(pubid)}`];
}
} else {
var pubkeytmp = [`${publicKeyArmored}`];
}
//这一段是按照官方文档来的
const pubkey = await Promise.all(pubkeytmp.map(async (key) => {
return (await openpgp.key.readArmored(key)).keys[0];
}));
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("私钥密码错误!");
throw new Error(e);
}
try {
const { data: encrypted } = await openpgp.encrypt({
message: openpgp.message.fromText(document.getElementById("text").value), // input as Message object
publicKeys: pubkey, // for encryption
privateKeys: [privateKey]
});
document.getElementById("output").value = encrypted;
} catch (e) {
window.alert("加密和签名错误\n错误代码:" + e);
throw new Error(e);
}
} else {
try {
const { data: encrypted } = await openpgp.encrypt({
message: openpgp.message.fromText(document.getElementById("text").value), // input as Message object
publicKeys: (await openpgp.key.readArmored(publicKeyArmored)).keys // for encryption
});
document.getElementById("output").value = encrypted;
} catch (e) {
window.alert("加密错误\n错误代码:" + e);
throw new Error(e);
}
}
await openpgp.destroyWorker();
$("#output").show("slow");
$("#copy").show("slow");
}
//加密和签名部分(二进制)
async function encrypt_bin(sign, modal) { //sign:true为启用签名,modal:true为从模态框运行
//判断是否选择了密钥
if (document.getElementById("pubdata").value === '' || (sign && document.getElementById("pridata") === '')) {
window.alert("请选择密钥!");
throw new Error("请选择密钥!");
}
// put keys in backtick (``) to avoid errors caused by spaces or tabs
const publicKeyArmored = `${PubRead(document.getElementById("pubdata").value)}`;
//判断是否启用签名
if (sign) {
const privateKeyArmored = `${PriRead(document.getElementById("pridata").value)}`; // encrypted private key
var keyname = PriReadName(document.getElementById("pridata").value);
//是否加密到签名者
if (document.getElementById("entosigner").checked) {
var keyidlen = (await openpgp.key.readArmored(privateKeyArmored)).keys[0].getKeyIds().length;
var keyid = new Array();
for (var ii = 0; ii < keyidlen; ++ii)
keyid[ii] = (await openpgp.key.readArmored(privateKeyArmored)).keys[0].getKeyIds()[ii].toHex();
var pubid = await PubSearchId(keyid); //搜索签名者的公钥
if (pubid === -1) {
alert("没有找到签名者的公钥\n信息将不会加密到签名者");
var pubkeytmp = [`${publicKeyArmored}`];
} else {
var pubkeytmp = [`${publicKeyArmored}`, `${PubRead(pubid)}`];
}
} else {
var pubkeytmp = [`${publicKeyArmored}`];
}
//这一段是按照官方文档来的
const pubkey = await Promise.all(pubkeytmp.map(async (key) => {
return (await openpgp.key.readArmored(key)).keys[0];
}));
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("私钥密码错误!");
throw new Error(e);
}
try {
const { data: encrypted } = await openpgp.encrypt({
message: await readBin_data(), // input as Message object
publicKeys: pubkey, // for encryption
privateKeys: [privateKey]
});
document.getElementById("output").value = encrypted;
} catch (e) {
window.alert("加密和签名错误\n错误代码:" + e);
throw new Error(e);
}
} else {
try {
const { data: encrypted } = await openpgp.encrypt({
message: await readBin_data(), // input as Message object
publicKeys: (await openpgp.key.readArmored(publicKeyArmored)).keys // for encryption
});
document.getElementById("output").value = encrypted;
} catch (e) {
window.alert("加密错误\n错误代码:" + e);
throw new Error(e);
}
}
await openpgp.destroyWorker();
$("#output").show("slow");
$("#copy").show("slow");
}
//读取文件,返回Message对象,从全局变量window.bin_file
function readBin_data() {
return new Promise(async (resolve, reject) => {
if (window.bin_file != undefined) {
var reader = new FileReader();
reader.onload = (async () => {
var u8 = new Uint8Array(reader.result)
msg = await openpgp.message.fromBinary(
u8,
window.bin_file.name
);
resolve(msg);
});
reader.onerror = () => {
throw new Error("读取文件出错");
}
reader.readAsArrayBuffer(window.bin_file);
} else {
alert("未上传文件");
throw new Error("未上传文件");
}
});
}
</script>
<script>
//清空和隐藏文本框
function cleanall() {
if (window.confirm("确认清空文本框?")) {
document.getElementById("text").value = '';
document.getElementById("output").value = '';
window.bin_file = undefined;
$("#output").hide();
$("#copy").hide();
}
}
</script>
<script>
//保存文件信息到全局变量
function saveFiles(file) {
window.bin_file = file[0];
}
</script>
<script>
//元素显示和隐藏部分
$(function () {
//判断是否是二进制数据
if (document.getElementById("bin_data").checked) {
document.getElementById("Modal_btn").onclick = Function("encrypt_bin(true, true)");
document.getElementById("encrypt_sign_btn").onclick = Function("encrypt_bin(true, false)");
document.getElementById("encrypt_btn").onclick = Function("encrypt_bin(false, false)");
$("#text").hide();
$("#file").show();
} else {
document.getElementById("Modal_btn").onclick = Function("encrypt(true, true)");
document.getElementById("encrypt_sign_btn").onclick = Function("encrypt(true, false)");
document.getElementById("encrypt_btn").onclick = Function("encrypt(false, false)");
$("#text").show();
$("#file").hide();
}
$("#bin_data").click(function () {
if (document.getElementById("bin_data").checked) {
document.getElementById("Modal_btn").onclick = Function("encrypt_bin(true, true)");
document.getElementById("encrypt_sign_btn").onclick = Function("encrypt_bin(true, false)");
document.getElementById("encrypt_btn").onclick = Function("encrypt_bin(false, false)");
$("#text").hide("slow");
$("#file").show("slow");
} else {
document.getElementById("Modal_btn").onclick = Function("encrypt(true, true)");
document.getElementById("encrypt_sign_btn").onclick = Function("encrypt(true, false)");
document.getElementById("encrypt_btn").onclick = Function("encrypt(false, false)");
$("#text").show("slow");
$("#file").hide("slow");
}
});
if (document.getElementById("nosign").checked) {
$(".onlysign").hide();
} else {
$(".onlyencript").hide();
}
$("#nosign").click(function () {
$(".onlysign").toggle("slow");
$(".onlyencript").toggle("slow");
});
});
$(function () {
$("#pubdata").hide();
$("#pridata").hide();
$("#output").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>
<script>
//密钥选择部分
$(function () {
var res = PriReadIndex();
var len = res.length;
var msg = '';
if (len === 0) {
msg = '<a class="dropdown-item" >无已保存的私钥,请添加私钥</a>'
} else {
for (var i = 0; i < len; ++i) {
msg = msg + '<a class="dropdown-item" onclick="Pri(' + res[i][0] + ',\'' + res[i][1] + '\');">' + char_conv(res[i][1]) + '</a>'
}
}
document.getElementById("primenu").innerHTML = msg;
res = PubReadIndex();
len = res.length;
msg = '';
if (len === 0) {
msg = '<a class="dropdown-item" >无已保存的公钥,请添加公钥</a>'
} else {
for (var i = 0; i < len; ++i) {
msg = msg + '<a class="dropdown-item" onclick="Pub(' + res[i][0] + ',\'' + res[i][1] + '\');">' + char_conv(res[i][1]) + '</a>'
}
}
document.getElementById("pubmenu").innerHTML = msg;
});
function Pub(id, name) {
document.getElementById("pubmsg").innerHTML = char_conv(name);
document.getElementById("pubdata").value = id;
}
function Pri(id, name) {
document.getElementById("primsg").innerHTML = char_conv(name);
document.getElementById("pridata").value = id;
}
</script>
<h1>加密</h1>
<form>
<div class="from-check">
<label class="from-check-label">
<input type="checkbox" id="bin_data">二进制数据<br>
</label>
<label class="from-check-label">
<input type="checkbox" id="nosign">禁用签名<br>
</label>
<label class="from-check-label onlysign">
<input type="checkbox" id="entosigner">加密到签名者<br>
</label>
</div>
</form>
<div class="dropdown">
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" id="pubmsg">
请选择公钥
</button>
<div class="dropdown-menu" id="pubmenu">
</div>
</div>
<div class="dropdown onlysign">
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" id="primsg">
请选择私钥
</button>
<div class="dropdown-menu" id="primenu">
</div>
</div>
<!-- 下面这两项仅用于保存数据 -->
<textarea id="pubdata"></textarea>
<textarea id="pridata"></textarea>
<textarea id="text" rows="10" class="textarea-inherit" placeholder="请输入要加密的信息"></textarea>
<input type="file" id="file" onchange="saveFiles(this.files)" /><br>
<div class="row mx-auto" style="width: 95%;">
<button type="button" onclick="encrypt(true,false)" class="onlysign col btn btn-primary"
id="encrypt_sign_btn">加密和签名</button>
<button type="button" onclick="encrypt(false,false)" class="onlyencript col btn btn-primary"
id="encrypt_btn">加密</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="decrypt.html">跳转到解密或验证</a><br>
</div>
<textarea id="output" rows="10" 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="encrypt(true,true)">确认</button>
</div>
</div>
</div>
</div>
</body>
</html>