OpenPGP.js 将 PGP 块传递给 javascript 变量
OpenPGP.js passing PGP block to javascript variable
我已经使用 OpenPGP.js 设置了一个加密消息的文件。我需要将 PGP 块设置为 javascript 变量,以便我可以将其传递给 encrypt/decrypt 函数。当我正常粘贴它时,它会将脚本分解为多行。我尝试使用 php json_encode - 这会传递给加密函数,但 readArmored 收到错误 "Unknown ASCII armor type".
将此变量从 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>
删除“ ”解决了问题:
var privateKey = '<?php echo $privateKey; ?>';
var publicKey = '<?php echo $publicKey; ?>';
到
var privateKey = <?php echo $privateKey; ?>;
var publicKey = <?php echo $publicKey; ?>;
我已经使用 OpenPGP.js 设置了一个加密消息的文件。我需要将 PGP 块设置为 javascript 变量,以便我可以将其传递给 encrypt/decrypt 函数。当我正常粘贴它时,它会将脚本分解为多行。我尝试使用 php json_encode - 这会传递给加密函数,但 readArmored 收到错误 "Unknown ASCII armor type".
将此变量从 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>
删除“ ”解决了问题:
var privateKey = '<?php echo $privateKey; ?>';
var publicKey = '<?php echo $publicKey; ?>';
到
var privateKey = <?php echo $privateKey; ?>;
var publicKey = <?php echo $publicKey; ?>;