1

I am trying to implement openpgpjs on my application because I need to encrypt a string using a public key (PGP). I tested this jsfiddle (https://jsfiddle.net/gu72bzm8/) which encrypts a string using a public key, it works very well. I even tested it with different keys and strings.

var message = "secret message";
const encryptMessage  = async() => {
if(window.crypto.getRandomValues){
    if(message != ""){
      const publicKeyString = document.getElementById("pubkey").innerHTML;
      var options = {
          message: openpgp.message.fromText(message),
          publicKeys: (await openpgp.key.readArmored(publicKeyString)).keys
     };


     openpgp.encrypt(options).then(ciphertext => {
         alert(ciphertext.data);
     })
  }
} else{
    window.alert("This browser does not support basic cryptography!");
  }
}

encryptMessage();

However, if I copy exactly that code and try to run it locally (using the same cdn of that fiddle) I get the following error:

Uncaught (in promise) Error: Error encrypting message: No keys, passwords, or session key provided.

How can I fix it?

brunoelyg
  • 206
  • 1
  • 6
  • Your script is probably running before the DOM elements exist. Check [Why does jQuery or a DOM method such as getElementById not find the element?](https://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element) – Phil May 03 '20 at 02:22

0 Answers0