Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I encrypt and decrypt a gpg file using this instead of plain data? #744

Closed
DeveloperXMA opened this issue Jul 25, 2018 · 2 comments
Closed

Comments

@DeveloperXMA
Copy link

DeveloperXMA commented Jul 25, 2018

As you can see, in the example, we are using the dummy data ' Hello World' to encrypt and decrypt.

But in my real case, I have a public key and a private key, and a passphrase, I have a gpg file I need to decrypt as a txt file.

Any idea can I do that using this openpgpjs? If not, can you teach me how to do that in nodeJS?

Right now it's giving me error Error encrypting message: Could not find valid key packet for signing in key ***************

const openpgp = require('openpgp');
const fs = require('fs');
openpgp.initWorker({ path: 'openpgp.worker.js'});

const pubKey = fs.readFileSync('./*****-Pub.asc', 'utf-8');

const privKey = fs.readFileSync('./********-pri.asc', 'utf-8');

const passphrase = `mypassphrase`;

const encryptDecryptFunction = async() => {
  const privKeyObj = openpgp.key.readArmored(privKey).keys[0];
  await privKeyObj.decrypt(passphrase);

  const options = {
    data: 'Hello, World!',                             // input as String (or Uint8Array)
    publicKeys: openpgp.key.readArmored(pubKey).keys,  // for encryption
    privateKeys: [privKeyObj]                          // for signing (optional)
  }

  openpgp.encrypt(options).then(ciphertext => {
    encrypted = ciphertext.data // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'
    return encrypted
  })
    .then(encrypted => {
      const options = {
        message: openpgp.message.readArmored(encrypted),     // parse armored message
        publicKeys: openpgp.key.readArmored(pubkey).keys,    // for verification (optional)
        privateKeys: [privKeyObj]                            // for decryption
      }

      openpgp.decrypt(options).then(plaintext => {
        console.log(plaintext.data)
        return plaintext.data // 'Hello, World!'
      })

    })
}

encryptDecryptFunction();
@twiss
Copy link
Member

twiss commented Jul 26, 2018

Your code looks correct; the error is specific to the key you're using. Without having an example key, it's quite hard to figure out what the issue with it is exactly. How did you generate the key? Could you post an example key generated that way?

@twiss
Copy link
Member

twiss commented Nov 1, 2018

Let me know if you still have issues.

@twiss twiss closed this as completed Nov 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants