我将使用openpgp.js,尝试用JavaScript编写一个GnuPG加密文件的样例解密器。我尝试了一下,没有考虑到它是否可行。我创建了以下页面:popup.html。
<!doctype html>
<!--
-->
<html>
<head>
<title>Popup</title>
<script src="openpgp.js"></script>
<script src="popup.js"></script>
</head>
<body>
<p>Upload message: </p><input id="message" type="file"/><br>
<p>Upload secret key: </p><input id="secret" type="file"/><br>
<p>Secret key password: </p><input id="password" type="password"/><br><br>
<button id="decrypt">Decrypt</button>
<p id="output"></p>
<div id="loadingDiv"></div>
</body>
</html>
popup.js
var message = "";
var secret = "";
function readMessage (e) {
var file = e.target.files[0];
if (!file) {
message = "";
}
var reader = new FileReader();
reader.onload = function (e) {
message = e.target.result;
};
reader.readAsText (file);
}
function readSecret (e) {
var file = e.target.files[0];
if (!file) {
secret = "";
}
var reader = new FileReader();
reader.onload = function (e) {
secret = e.target.result;
};
reader.readAsText (file);
}
function loadScript(url, callback)
{
var head = document.getElementsByTagName ("head")[0];
var script = document.createElement ("script");
script.type = "text/javascript";
script.src = url;
script.onreadystatechange = callback;
script.onload = callback;
head.appendChild(script);
}
document.addEventListener ("DOMContentLoaded", function() {
document.getElementById ("message").addEventListener("change", readMessage, false);
document.getElementById ("secret").addEventListener("change", readSecret, false);
var gen = function() {
document.getElementById ("decrypt").addEventListener ("click", function() {
var output = document.getElementById ("output");
output.style.color = "black";
if (document.getElementById ("message").value == "") {
output.innerHTML = "No message provided";
output.style.color = "red";
}
else if (document.getElementById ("secret").value == "") {
output.innerHTML = "No secret key provided";
output.style.color = "red";
}
else if (document.getElementById ("password").value == "") {
output.innerHTML = "No password for secret key provided";
output.style.color = "red";
}
else {
var privateKey = openpgp.key.readArmored (secret).keys[0];
var isCorrect = privateKey.decrypt (document.getElementById ("password").value);
if (isCorrect) {
output.innerHTML = "";
output.style.color = "black";
var img = document.createElement ("img");
img.src = "loading.gif";
img.id = "loading";
document.getElementById ("loadingDiv").appendChild (img);
message = openpgp.message.readArmored (message);
openpgp.decryptMessage (privateKey, message).then (function (plaintext) {
output.innerHTML = plaintext;
}).catch (function(error) {
output.innerHTML = "Error while decrypting";
output.style.color = "red";
});
}
else {
output.innerHTML = "Incorrect password";
output.style.color = "red";
}
}
});
}
loadScript ("openpgp.js", gen);
});
在 message = openpgp.message.readArmored (message);
上,openpgp.js 报错 Unknown ASCII armor type
。
那么这个问题有解决办法吗?如果有的话,我需要做些什么不同的操作呢?
-----BEGIN PGP MESSAGE-----
之类的内容开头,则使用的是ASCII装甲。我猜readArmored
只会读取ASCII装甲消息。 - Jens Eratmessage.fromBinary(bytes)
),但是转换后的消息无法解密(参数“utf8”不是字符串类型
)。 - Kudayar Pirimbaev