{"id":2299,"date":"2026-07-20T12:41:37","date_gmt":"2026-07-20T10:41:37","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/how-toend-to-end-encryption-encrypted-message-with-node-js-step-by-step-tutorial\/"},"modified":"2026-08-26T21:14:57","modified_gmt":"2026-08-26T19:14:57","slug":"how-toend-to-end-encryption-encrypted-message-with-node-js-step-by-step-tutorial","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/how-toend-to-end-encryption-encrypted-message-with-node-js-step-by-step-tutorial\/","title":{"rendered":"How to End-to-End Encryption Encrypted Message with Node.js \u2013 Step by Step Tutorial"},"content":{"rendered":"<p><!-- ktg-updated-banner --><\/p>\n<p><em>Updated August 2026 \u2014 full tutorial restored for this URL.<\/em><\/p>\n<p>In this tutorial you learn how to <strong>encrypt and decrypt messages<\/strong> in <strong>Node.js<\/strong> using the built-in <code>crypto<\/code> module. We use <strong>AES-256-GCM<\/strong> (authenticated encryption). The URL slug keeps the historical spelling; the title is cleaned up for readers.<\/p>\n<p><em>Note:<\/em> True end-to-end encryption also needs key exchange (e.g. X25519) so the server never sees plaintext keys. Here we focus on the encrypt\/decrypt building blocks.<\/p>\n<ol>\n<li><a href=\"#t1\">Generate a key<\/a><\/li>\n<li><a href=\"#t2\">Encrypt a message<\/a><\/li>\n<li><a href=\"#t3\">Decrypt a message<\/a><\/li>\n<li><a href=\"#t4\">Wire a tiny CLI \/ API<\/a><\/li>\n<li><a href=\"#t5\">Security tips<\/a><\/li>\n<\/ol>\n<p><strong id=\"t1\">1. Generate a key<\/strong><\/p>\n<pre><code>const crypto = require('crypto');\nconst key = crypto.randomBytes(32); \/\/ store securely!\nconsole.log(key.toString('base64'));\n<\/code><\/pre>\n<p><strong id=\"t2\">2. Encrypt a message<\/strong><\/p>\n<pre><code>function encrypt(plaintext, key) {\n  const iv = crypto.randomBytes(12);\n  const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);\n  const ciphertext = Buffer.concat([\n    cipher.update(plaintext, 'utf8'),\n    cipher.final(),\n  ]);\n  const tag = cipher.getAuthTag();\n  return {\n    iv: iv.toString('base64'),\n    tag: tag.toString('base64'),\n    data: ciphertext.toString('base64'),\n  };\n}\n<\/code><\/pre>\n<p><strong id=\"t3\">3. Decrypt a message<\/strong><\/p>\n<pre><code>function decrypt(payload, key) {\n  const decipher = crypto.createDecipheriv(\n    'aes-256-gcm',\n    key,\n    Buffer.from(payload.iv, 'base64')\n  );\n  decipher.setAuthTag(Buffer.from(payload.tag, 'base64'));\n  const plaintext = Buffer.concat([\n    decipher.update(Buffer.from(payload.data, 'base64')),\n    decipher.final(),\n  ]);\n  return plaintext.toString('utf8');\n}\n<\/code><\/pre>\n<p><strong id=\"t4\">4. Wire a tiny CLI \/ API<\/strong><\/p>\n<pre><code>const key = Buffer.from(process.env.APP_KEY, 'base64');\nconst sealed = encrypt('Hello Kindson', key);\nconsole.log(sealed);\nconsole.log(decrypt(sealed, key));\n<\/code><\/pre>\n<p><strong id=\"t5\">5. Security tips<\/strong><\/p>\n<ul>\n<li>Never reuse IV with the same key.<\/li>\n<li>Prefer GCM\/ChaCha20-Poly1305 over ECB\/CBC without MAC.<\/li>\n<li>Do not log keys or plaintext.<\/li>\n<li>For passwords use <code>scrypt<\/code>\/<code>argon2<\/code>, not raw AES keys derived from plain strings without salt.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Updated August 2026 \u2014 full tutorial restored for this URL. In this tutorial you learn how to encrypt and decrypt messages in Node.js using the &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[35],"tags":[],"class_list":["post-2299","post","type-post","status-publish","format-standard","hentry","category-algorithms"],"acf":[],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2299","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=2299"}],"version-history":[{"count":2,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2299\/revisions"}],"predecessor-version":[{"id":2453,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2299\/revisions\/2453"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=2299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=2299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=2299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}