-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypt_password.html
164 lines (159 loc) · 5.7 KB
/
encrypt_password.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
<!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/config.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() {
const password = [document.getElementById('passwd').value];
//密码确认
if (window.confirm("确认密码:\n" + password[0])) {
try {
if (!document.getElementById("bin_data").checked) {
var message = await openpgp.message.fromText(document.getElementById("text").value);
} else {
var message = await readBin_data();
}
const { data: encrypted } = await openpgp.encrypt({
message: message, // input as Message object
passwords: password
});
document.getElementById("output").value = encrypted;
} catch (e) {
window.alert("加密错误\n错误代码:" + e);
throw new Error(e);
}
} else {
alert("加密已取消");
throw new Error("加密已取消");
}
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("未上传文件");
}
});
}
//保存文件信息到全局变量
function saveFiles(file) {
window.bin_file = file[0];
}
</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 () {
$("#output").hide();
$("#copy").hide();
//判断是否是二进制数据
if (document.getElementById("bin_data").checked) {
$("#text").hide();
$("#file").show();
} else {
$("#text").show();
$("#file").hide();
}
$("#bin_data").click(function () {
if (document.getElementById("bin_data").checked) {
$("#text").hide("slow");
$("#file").show("slow");
} else {
$("#text").show("slow");
$("#file").hide("slow");
}
});
})
</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>
<form onsubmit="return false">
<input type="password" class="input-inherit" id="passwd" placeholder="要使用的密码" autofocus="autofocus"><br>
</form>
<div class="from-check">
<label class="from-check-label">
<input type="checkbox" id="bin_data">二进制数据<br>
</label>
</div>
<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()" 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="decrypt.html">跳转到解密</a><br>
</div>
<textarea id="output" rows="10" class="textarea-inherit" readonly></textarea>
</body>
</html>