JavaScript OpenPGP.js passing PGP block to javascript variable

I have setup a file using OpenPGP.js which encrypts a message. I need to set the PGP blocks to a javascript variable so i can pass it to the encrypt/decrypt functions. When I paste it in normally it breaks the script as its multiple lines. I have tried using php json_encode - this passes to the encrypt function but readArmored gets an error "Unknown ASCII armor type".

What is the best way to pass this variable from php to javascript, or how can i decode it properly for use in javascript?

Here is the code:

<!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>
Answer:1

I am having trouble getting the text 'Vegetable' from the DOM <div class="input-group"> <ul> <li> <span class="input-checkbox"> <...

I am having trouble getting the text 'Vegetable' from the DOM <div class="input-group"> <ul> <li> <span class="input-checkbox"> <...

I have a timer in a template, which updates every second using angular's $interval service. var now = moment(); var startActual = moment(scope.vm.start_actual); var duration = ...

I have a timer in a template, which updates every second using angular's $interval service. var now = moment(); var startActual = moment(scope.vm.start_actual); var duration = ...

  1. using angular material
  2. using angular cli
  3. using angular in visual studio
  4. using angularjs
  5. using angular with django
  6. using angular and react together
  7. using angular material icons
  8. using angular with asp.net mvc
  9. using angular with java
  10. using angular material with bootstrap
  11. using angular with flask
  12. using angular with express
  13. using angular elements
  14. using angular in visual studio 2019
  15. using angular router
  16. using angular with electron
  17. using angular with react
  18. using angular in visual studio code
  19. using angular with .net
  20. using angular directives

I have a simple set of statements that actually work (I have run that code and it does what I expect of it) that looks as follow: result_xml = result.jqxhr.responseXML; a = jQuery("data[name='...

I have a simple set of statements that actually work (I have run that code and it does what I expect of it) that looks as follow: result_xml = result.jqxhr.responseXML; a = jQuery("data[name='...

  1. dealing with scrollbar jquery

I am using WebGL to do hardware skinning, however updating my model node hierarchies is causing a huge hit for performance. Every node needs to query its current location/rotation/scale keyframes, ...

I am using WebGL to do hardware skinning, however updating my model node hierarchies is causing a huge hit for performance. Every node needs to query its current location/rotation/scale keyframes, ...