1
我已經使用加密消息的OpenPGP.js設置了一個文件。我需要將PGP塊設置爲一個JavaScript變量,所以我可以將它傳遞給加密/解密函數。當我通常將其粘貼時,它將腳本分成多行。我已經嘗試使用php json_encode - 它傳遞給加密函數,但是readArmored得到一個錯誤「未知的ASCII裝甲類型」。OpenPGP.js將PGP塊傳遞給javascript變量
將這個變量從php傳遞給javascript的最佳方式是什麼,或者我該如何正確解碼才能在javascript中使用?
下面是代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="jquery.min.js"></script>
<script src="openpgp.js"></script>
</head>
<body>
<p>
Private:<br>
<textarea id="private" style="width: 500px; height: 100px;"></textarea>
</p>
<p>
Passphrase:<br>
<input id="passphrase" style="width: 500px;">
</p>
<p>
Public:<br>
<textarea id="public" style="width: 500px; height: 100px;"></textarea>
</p>
<p>
Encrypted:<br>
<textarea id="encrypted" style="width: 500px; height: 100px;"></textarea>
</p>
<p>
Decrypted:<br>
<textarea id="decrypted" style="width: 500px; height: 100px;"></textarea>
</p>
<?php
$privateKey = "-----BEGIN PGP PRIVATE KEY BLOCK-----
...
-----END PGP PRIVATE KEY BLOCK-----";
$privateKey = json_encode($privateKey);
$publicKey = "-----BEGIN PGP PUBLIC KEY BLOCK-----
...
-----END PGP PUBLIC KEY BLOCK-----";
$publicKey = json_encode($publicKey);
?>
<script>
var privateKey = '<?php echo $privateKey; ?>';
var publicKey = '<?php echo $publicKey; ?>';
var passcode = 'pass1234';
$('#passphrase').val(passcode);
$('#private').val(privateKey);
$('#public').val(publicKey);
encrypt(publicKey, 'Hello!!!');
function encrypt(pubkey, message){
var publicKey = openpgp.key.readArmored(pubkey);
console.log(publicKey);
openpgp.encryptMessage(publicKey.keys, message).then(function(pgpMessage) {
// success
$('#encrypted').val(pgpMessage);
}).catch(function(error) {
// failure
console.log(error);
});
}
</script>
</body>
</html>