From 65be3ed77ad14ec25e1d70f76ba9b8daaf1b1943 Mon Sep 17 00:00:00 2001 From: Viet Nguyen Date: Wed, 9 May 2018 21:48:00 -0400 Subject: [PATCH] new Encrypt and decrypt String data with PGP keys example --- README.md | 67 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 3eb1c5a5..a62a6641 100644 --- a/README.md +++ b/README.md @@ -135,36 +135,49 @@ openpgp.decrypt(options).then(function(plaintext) { #### Encrypt and decrypt *String* data with PGP keys ```js -var options, encrypted; +const openpgp = require('openpgp') // use as CommonJS, AMD, ES6 module or via window.openpgp + +openpgp.initWorker({ path:'openpgp.worker.js' }) // set the relative web worker path -var pubkey = '-----BEGIN PGP PUBLIC KEY BLOCK ... END PGP PUBLIC KEY BLOCK-----'; -var privkey = '-----BEGIN PGP PRIVATE KEY BLOCK ... END PGP PRIVATE KEY BLOCK-----'; //encrypted private key -var passphrase = 'secret passphrase'; //what the privKey is encrypted with +// put keys in backtick (``) to avoid errors caused by spaces or tabs +const pubkey = `-----BEGIN PGP PUBLIC KEY BLOCK----- +... +-----END PGP PUBLIC KEY BLOCK-----` +const privkey = `-----BEGIN PGP PRIVATE KEY BLOCK----- +... +-----END PGP PRIVATE KEY BLOCK-----` //encrypted private key +const passphrase = `yourPassphrase` //what the privKey is encrypted with -var privKeyObj = openpgp.key.readArmored(privkey).keys[0]; -await privKeyObj.decrypt(passphrase); +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!' + }) + + }) +} -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(function(ciphertext) { - encrypted = ciphertext.data; // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----' -}); -``` - -```js -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(function(plaintext) { - return plaintext.data; // 'Hello, World!' -}); +encryptDecryptFunction() ``` #### Encrypt with compression