{"componentChunkName":"component---src-pages-markdown-remark-fields-slug-js","path":"/engineering/using-pgp-encryption-with-nodejs/","result":{"data":{"markdownRemark":{"id":"92200a97-74be-5423-a4f1-a30f7eb0bea6","excerpt":"What is PGP? PGP (Pretty Good Privacy) is a cryptographic process used to encrypt and decrypt information. It combines concepts from symmetric and asymmetric…","html":"<h2 id=\"what-is-pgp\" style=\"position:relative;\"><a href=\"#what-is-pgp\" aria-label=\"what is pgp permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is PGP?</h2>\n<p>PGP (Pretty Good Privacy) is a cryptographic process used to encrypt and decrypt information. It combines concepts from symmetric and asymmetric key encryption, maintaining some of the best security and usability aspects of both.</p>\n<p>One way PGP can be used is to protect the confidentiality of information. Once the information is encrypted, nobody will be able to decrypt it unless they have the right key. In practice, PGP is commonly used in sending and receiving emails, sharing information on the Dark Web, and others. This is because both on and off the Internet, there are ways to intercept information being sent, making encryption using PGP or similar critical.</p>\n<p>On a high-level the process between a sender and receiver looks like this:</p>\n<ol>\n<li>The recipient generates public and private keys.</li>\n<li>The recipient sends its public key to the sender.</li>\n<li>The sender encrypts the message using the given public key.</li>\n<li>The sender sends the encrypted message to the recipient.</li>\n<li>The recipient decrypts the message using its private key.</li>\n</ol>\n<h2 id=\"pgp-examples-in-nodejs\" style=\"position:relative;\"><a href=\"#pgp-examples-in-nodejs\" aria-label=\"pgp examples in nodejs permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>PGP Examples in Node.js</h2>\n<p>Now, let's go over some examples in Node.js using the <a href=\"https://www.npmjs.com/package/openpgp\">openpgp library</a>.</p>\n<ul>\n<li>OpenPGP is a protocol that defines the standards for PGP. OpenPGP.js implements the OpenPGP protocol in JavaScript.</li>\n</ul>\n<p>We'll go over some basic examples and show how to encrypt &#x26; decrypt large files using Node.js streams.</p>\n<p>First, set up your Node.js project and install openpgp.js:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">mkdir pgp-tutorial && cd pgp-tutorial && npm init</span>\n<span class=\"grvsc-line\">npm i openpgp --save</span></code></pre>\n<p>Note: examples use openpgp v4.10.8</p>\n<h3 id=\"generating-keys\" style=\"position:relative;\"><a href=\"#generating-keys\" aria-label=\"generating keys permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Generating keys</h3>\n<p>When generating private and public PGP keys with OpenPGP, you can define which curve to use in Elliptic-curve cryptography. In this example, we use Ed25519 for its performance and small key size. For the full list of curves, you can choose from, refer to OpenPGP.js docs.</p>\n<p>You also need to define a passphrase used to decrypt files and the private key. In practice, this should be a strong, randomized secret generated for a single-use.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">// generate-keys.js</span>\n<span class=\"grvsc-line\">const openpgp = require(&quot;openpgp&quot;);</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">generate();</span>\n<span class=\"grvsc-line\">async function generate() {</span>\n<span class=\"grvsc-line\">  const { privateKeyArmored, publicKeyArmored } = await openpgp.generateKey({</span>\n<span class=\"grvsc-line\">    userIds: [{ name: &quot;person&quot;, email: &quot;person@somebody.com&quot; }],</span>\n<span class=\"grvsc-line\">    curve: &quot;ed25519&quot;,</span>\n<span class=\"grvsc-line\">    passphrase: &quot;qwerty&quot;,</span>\n<span class=\"grvsc-line\">  });</span>\n<span class=\"grvsc-line\">  console.log(privateKeyArmored);</span>\n<span class=\"grvsc-line\">  console.log(publicKeyArmored);</span>\n<span class=\"grvsc-line\">}</span></code></pre>\n<p>Running the above gives us our private key:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">-----BEGIN PGP PRIVATE KEY BLOCK-----</span>\n<span class=\"grvsc-line\">Version: OpenPGP.js v4.10.8</span>\n<span class=\"grvsc-line\">Comment: https://openpgpjs.org</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">xYYEX6iKVxYJKwYBBAHaRw8BAQdANJ6JIXuMMZV3NIlwq0POS7xsF2N7+kAE</span>\n<span class=\"grvsc-line\">7KQjAtfIuqj+CQMI4CUgW9jPsGPgJvQnnCWFf1s7lO/5+D5ZQ9JK25fUtmQo</span>\n<span class=\"grvsc-line\">WyHX0Ja1ryOoFnvq7u+7fUC0+RAzt8S1xv3eDzazfgNuLtEmufwMyR6wMi78</span>\n<span class=\"grvsc-line\">Kc0ccGVyc29uIDxwZXJzb25Ac29tZWJvZHkuY29tPsKPBBAWCgAgBQJfqIpX</span>\n<span class=\"grvsc-line\">BgsJBwgDAgQVCAoCBBYCAQACGQECGwMCHgEAIQkQVrbGpNEnCPUWIQQb8YRJ</span>\n<span class=\"grvsc-line\">hw7DjekU68lWtsak0ScI9UM7AQDv4YRbIdU2ErPf8MobreeLiXXjYZ6fas8E</span>\n<span class=\"grvsc-line\">zW0KoTZWEQD+NHDY2YYByMF1mWusPkdPDpyBzqMJrlMeihMzZ+PE8AfHiwRf</span>\n<span class=\"grvsc-line\">qIpXEgorBgEEAZdVAQUBAQdARY37/Vys4Sj6DvwN6TRjxrIqiMIngxQgvOb6</span>\n<span class=\"grvsc-line\">wi+tQzEDAQgH/gkDCJ2xNZ1OXxv94E8fTLQ3gYHFQuebn/PSijD8CqlvHNB/</span>\n<span class=\"grvsc-line\">/Z9sIxSFt7rzorW+9v6Awfe+pQwXW5iEyJkdiGu3BM91GMwMvMmZ+rBNlBvq</span>\n<span class=\"grvsc-line\">iX7CeAQYFggACQUCX6iKVwIbDAAhCRBWtsak0ScI9RYhBBvxhEmHDsON6RTr</span>\n<span class=\"grvsc-line\">yVa2xqTRJwj17W0BAI5MuCWHrqjSRcdjLTwxa++jYv+Yxq4tODj8oh27T86v</span>\n<span class=\"grvsc-line\">AQCfb3lij9JGlIMNDQgceeougl+Lw4Gb0kQCnsNQRggTDw==</span>\n<span class=\"grvsc-line\">=yzT4</span>\n<span class=\"grvsc-line\">-----END PGP PRIVATE KEY BLOCK-----</span></code></pre>\n<p>And the public key:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">-----BEGIN PGP PUBLIC KEY BLOCK-----</span>\n<span class=\"grvsc-line\">Version: OpenPGP.js v4.10.8</span>\n<span class=\"grvsc-line\">Comment: https://openpgpjs.org</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">xjMEX6iKVxYJKwYBBAHaRw8BAQdANJ6JIXuMMZV3NIlwq0POS7xsF2N7+kAE</span>\n<span class=\"grvsc-line\">7KQjAtfIuqjNHHBlcnNvbiA8cGVyc29uQHNvbWVib2R5LmNvbT7CjwQQFgoA</span>\n<span class=\"grvsc-line\">IAUCX6iKVwYLCQcIAwIEFQgKAgQWAgEAAhkBAhsDAh4BACEJEFa2xqTRJwj1</span>\n<span class=\"grvsc-line\">FiEEG/GESYcOw43pFOvJVrbGpNEnCPVDOwEA7+GEWyHVNhKz3/DKG63ni4l1</span>\n<span class=\"grvsc-line\">42Gen2rPBM1tCqE2VhEA/jRw2NmGAcjBdZlrrD5HTw6cgc6jCa5THooTM2fj</span>\n<span class=\"grvsc-line\">xPAHzjgEX6iKVxIKKwYBBAGXVQEFAQEHQEWN+/1crOEo+g78Dek0Y8ayKojC</span>\n<span class=\"grvsc-line\">J4MUILzm+sIvrUMxAwEIB8J4BBgWCAAJBQJfqIpXAhsMACEJEFa2xqTRJwj1</span>\n<span class=\"grvsc-line\">FiEEG/GESYcOw43pFOvJVrbGpNEnCPXtbQEAjky4JYeuqNJFx2MtPDFr76Ni</span>\n<span class=\"grvsc-line\">/5jGri04OPyiHbtPzq8BAJ9veWKP0kaUgw0NCBx56i6CX4vDgZvSRAKew1BG</span>\n<span class=\"grvsc-line\">CBMP</span>\n<span class=\"grvsc-line\">=C6S6</span>\n<span class=\"grvsc-line\">-----END PGP PUBLIC KEY BLOCK-----</span></code></pre>\n<h3 id=\"file-encryption\" style=\"position:relative;\"><a href=\"#file-encryption\" aria-label=\"file encryption permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>File Encryption</h3>\n<p>Now we can start encrypting information.</p>\n<p>Create a text file:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">echo &#39;This file contains secret information&#39; &gt; secrets.txt</span></code></pre>\n<p>Here, we act as the sender who received a public key from the intended recipient. We use their public key to encrypt the confidential information:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">// encrypt-file.js</span>\n<span class=\"grvsc-line\">const openpgp = require(&quot;openpgp&quot;);</span>\n<span class=\"grvsc-line\">const fs = require(&quot;fs&quot;);</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">const publicKeyArmored = &lt;PUBLIC KEY GIVEN BY RECIPIENT&gt;</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">encrypt();</span>\n<span class=\"grvsc-line\">async function encrypt() {</span>\n<span class=\"grvsc-line\">  const plainData = fs.readFileSync(&quot;secrets.txt&quot;);</span>\n<span class=\"grvsc-line\">  const encrypted = await openpgp.encrypt({</span>\n<span class=\"grvsc-line\">    message: openpgp.message.fromText(plainData),</span>\n<span class=\"grvsc-line\">    publicKeys: (await openpgp.key.readArmored(publicKeyArmored)).keys,</span>\n<span class=\"grvsc-line\">  });</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">  fs.writeFileSync(&quot;encrypted-secrets.txt&quot;, encrypted.data);</span>\n<span class=\"grvsc-line\">}</span></code></pre>\n<p>In the newly created <code>encrypted-secrets.txt</code> file, we have the contents encrypted like so:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">-----BEGIN PGP MESSAGE-----</span>\n<span class=\"grvsc-line\">Version: OpenPGP.js v4.10.8</span>\n<span class=\"grvsc-line\">Comment: https://openpgpjs.org</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">wV4DUsPKVnc3UHMSAQdAey4TJiEOrZQIrx6q2zBLgmPkbnhPMt1WR+jCWX5x</span>\n<span class=\"grvsc-line\">Gn8wEim8W4OhDVMwfhtgVIClBCGPhvdeZ1zvVUAJGDdl8+S+DUynKhPNcN8m</span>\n<span class=\"grvsc-line\">Kb9TRGYs0sAlAaXcTChBHSS5kDHV/8Hgjcn0OIs6v2mbCkz/bHs/shwf8WMI</span>\n<span class=\"grvsc-line\">ov711iEkgcXnXIX+ZDGyDFnAKftoygzAf0aZy82g7ejAD9SX13wNmO6TK8Gw</span>\n<span class=\"grvsc-line\">wr9Xj8F6XBV0yHvdsm2uzRY9W03tTSqAf0anEs+ZWyVR/ha9ddnZJPFKtUbC</span>\n<span class=\"grvsc-line\">BEF4AMavsIN0CcqpA4q69I3E6GEtkAzgBWfJOOO8mQsNQ1vJWcJocinryBE6</span>\n<span class=\"grvsc-line\">Kbhznoe+R69qmUaJXPpe5scF6tfCYuQtPz4uhOljT+OUP6qss5Nz4zBs4JLq</span>\n<span class=\"grvsc-line\">nUlyynLLSSgdVr4Hvg==</span>\n<span class=\"grvsc-line\">=5tyF</span>\n<span class=\"grvsc-line\">-----END PGP MESSAGE-----</span></code></pre>\n<p>Now, as the sender, we can send the encrypted file to the recipient.</p>\n<h3 id=\"file-decryption\" style=\"position:relative;\"><a href=\"#file-decryption\" aria-label=\"file decryption permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>File Decryption</h3>\n<p>Here, we act as the reciever. To decrypt the <code>encrypted-secrets.txt</code> file, we use our private key and passphrase:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"7\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">// decrypt-file.js</span>\n<span class=\"grvsc-line\">const openpgp = require(&quot;openpgp&quot;);</span>\n<span class=\"grvsc-line\">const fs = require(&quot;fs&quot;);</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">const privateKeyArmored = &lt;PRIVATE KEY&gt;</span>\n<span class=\"grvsc-line\">const passphrase = &lt;PASS PHRASE&gt;;</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">decrypt();</span>\n<span class=\"grvsc-line\">async function decrypt() {</span>\n<span class=\"grvsc-line\">  const privateKey = (await openpgp.key.readArmored([privateKeyArmored])).keys[0];</span>\n<span class=\"grvsc-line\">  await privateKey.decrypt(passphrase);</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">  const encryptedData = fs.readFileSync(&quot;encrypted-secrets.txt&quot;);</span>\n<span class=\"grvsc-line\">  const decrypted = await openpgp.decrypt({</span>\n<span class=\"grvsc-line\">    message: await openpgp.message.readArmored(encryptedData),</span>\n<span class=\"grvsc-line\">    privateKeys: [privateKey],</span>\n<span class=\"grvsc-line\">  });</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">  console.log(decrypted.data);</span>\n<span class=\"grvsc-line\">}</span></code></pre>\n<p>Which logs the decrypted file contents:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"8\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">This file contains secret information.</span></code></pre>\n<h3 id=\"using-streams-for-large-files\" style=\"position:relative;\"><a href=\"#using-streams-for-large-files\" aria-label=\"using streams for large files permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Using Streams for Large Files</h3>\n<p>If you plan on encrypting or decrypting large files, you won't be able to fit the entire file contents in memory. In this case, you can use Node.js streams.</p>\n<p>Here, we encrypt a large file called <code>dataset-1mill.json</code> using streams:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"9\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">encrypt();</span>\n<span class=\"grvsc-line\">async function encrypt() {</span>\n<span class=\"grvsc-line\">  const encrypted = await openpgp.encrypt({</span>\n<span class=\"grvsc-line\">    message: openpgp.message.fromText(fs.createReadStream(&quot;dataset-1mill.json&quot;)),</span>\n<span class=\"grvsc-line\">    publicKeys: (await openpgp.key.readArmored(publicKeyArmored)).keys,</span>\n<span class=\"grvsc-line\">  });</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">  let readStream = encrypted.data;</span>\n<span class=\"grvsc-line\">  let writeStream = fs.createWriteStream(&quot;encrypted-dataset.txt&quot;, { flags: &quot;a&quot; });</span>\n<span class=\"grvsc-line\">  readStream.pipe(writeStream);</span>\n<span class=\"grvsc-line\">  readStream.on(&quot;end&quot;, () =&gt; console.log(&quot;done!&quot;));</span>\n<span class=\"grvsc-line\">}</span></code></pre>\n<p>And then, we decrypt the newly created <code>encrypted-dataset.txt</code> using streams:</p>\n<ul>\n<li>Notice that we set the flag allow<em>unauthenticated</em>stream to true, which allows streaming data before the message integrity has been checked. This is because, in our case, our OpenPGP message only has a single integrity tag at the end. This means the entire message gets loaded into memory, and we get a heap out of memory error since our file is too large to fit into memory at once.</li>\n</ul>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"10\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">openpgp.config.allow_unauthenticated_stream = true;</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">decrypt();</span>\n<span class=\"grvsc-line\">async function decrypt() {</span>\n<span class=\"grvsc-line\">  const privateKey = (await openpgp.key.readArmored([privateKeyArmored])).keys[0];</span>\n<span class=\"grvsc-line\">  await privateKey.decrypt(passphrase);</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">  const decrypted = await openpgp.decrypt({</span>\n<span class=\"grvsc-line\">    message: await openpgp.message.readArmored(fs.createReadStream(&quot;encrypted-dataset.txt&quot;)),</span>\n<span class=\"grvsc-line\">    privateKeys: [privateKey],</span>\n<span class=\"grvsc-line\">  });</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">  let readStream = decrypted.data;</span>\n<span class=\"grvsc-line\">  let writeStream = fs.createWriteStream(&quot;decrypted-dataset.json&quot;, { flags: &quot;a&quot; });</span>\n<span class=\"grvsc-line\">  readStream.pipe(writeStream);</span>\n<span class=\"grvsc-line\">  readStream.on(&quot;end&quot;, () =&gt; console.log(&quot;done!&quot;));</span>\n<span class=\"grvsc-line\">}</span></code></pre>\n<p>Now, <code>decrypted-dataset.json</code> will have the same contents as our original <code>dataset-1mill.json</code> file.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n</style>","headings":[{"value":"What is PGP?","depth":2},{"value":"PGP Examples in Node.js","depth":2},{"value":"Generating keys","depth":3},{"value":"File Encryption","depth":3},{"value":"File Decryption","depth":3},{"value":"Using Streams for Large Files","depth":3}],"fields":{"slug":"/engineering/using-pgp-encryption-with-nodejs/"},"frontmatter":{"metatitle":null,"metadescription":null,"description":"Starter guide on Pretty Good Privacy(PGP) with Nodejs. PGP, a cryptographic process used to encrypt and decrypt information.","title":"Using PGP Encryption with Nodejs","canonical":null,"date":"November 10, 2020","updated_date":null,"tags":["Security","NodeJs","Encryption"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.7699115044247788,"src":"/static/a427e4132564cc095bcc9015faa67da4/03979/cover.png","srcSet":"/static/a427e4132564cc095bcc9015faa67da4/f5f11/cover.png 200w,\n/static/a427e4132564cc095bcc9015faa67da4/6d133/cover.png 400w,\n/static/a427e4132564cc095bcc9015faa67da4/03979/cover.png 800w,\n/static/a427e4132564cc095bcc9015faa67da4/9d953/cover.png 1080w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Andy Yeung","github":null,"bio":"Software Developer at LoginRadius with an interest in big data and basketball..","avatar":null}}}},"pageContext":{"id":"92200a97-74be-5423-a4f1-a30f7eb0bea6","fields__slug":"/engineering/using-pgp-encryption-with-nodejs/","__params":{"fields__slug":"engineering"}}},"staticQueryHashes":["1171199041","1384082988","1711371485","1753898100","2100481360","229320306","23180105","528864852"]}