我正在尝试使用openpgp.js在JavaScript中为GnuPG加密文件编写示例解密器。
所以我天真地尝试过,甚至没有询问它是否可能。我做了以下几页。
popup.html
X >= 0 && X <= 1 && Y >= 0 && Y <= 1
popup.js
<!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>
openpgp.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);
});
上出现Unknown ASCII armor type
错误。
有可能吗?如果是的话,我应该做些不同的事吗?
答案 0 :(得分:2)
OpenPGP知道消息的编码,
openpgp.message.readArmored (message)
只能识别ASCII装甲信息。请改用openpgp.message.fromBinary (message)
。作为替代方案,在加密时使用--armor
选项通过GnuPG对消息进行编码,或者gpg --enarmor
已加密的二进制消息。
答案 1 :(得分:0)
使用Openpgpjs版本3.x,我发现必须创建一个私钥对象,并与options变量中的publicKey和message一起使用。使用私钥创建私钥对象。首先创建私钥对象,然后使用&#34; secret&#34;解密它。短语,然后解密消息。
以下是使用变量的示例。
privKeyObj.decrypt(secret).then(function(oBoolean) {
//Name oBoolean anything you want.
//It will be true or false indicating
//whether the secret phrase is right.
if(!oBoolean) {
output.innerHTML = "Incorrect password.";
output.style.color = "red";
}
else {
var privateKey = "your openpgpjs private key created with your secret phrase";
var privKeyObj = openpgp.key.readArmored(privateKey).keys[0];
var options = {
message: openpgp.message.readArmored(message),
publicKeys: openpgp.key.readArmored(publickey).keys,
privateKeys: [privKeyObj]
};
openpgp.decrypt(options).then(function(plaintext) {
output.innerHTML = plaintext.data;
}, function(error) {
output.innerHTML = "Error while decrypting";
output.style.color = "red";
});
}
});