{"id":28404769,"url":"https://github.com/anvilco/node-encryption","last_synced_at":"2025-07-04T16:32:57.461Z","repository":{"id":39849869,"uuid":"146928816","full_name":"anvilco/node-encryption","owner":"anvilco","description":"RSA and AES Encryption helpers","archived":false,"fork":false,"pushed_at":"2024-09-11T21:33:47.000Z","size":618,"stargazers_count":2,"open_issues_count":2,"forks_count":1,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-06-26T09:56:46.845Z","etag":null,"topics":["aes","cryptography","encryption","js","node","rsa"],"latest_commit_sha":null,"homepage":"https://www.useanvil.com/open-source","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/anvilco.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2018-08-31T18:26:35.000Z","updated_at":"2025-05-20T05:37:42.000Z","dependencies_parsed_at":"2023-02-11T16:05:25.627Z","dependency_job_id":"8c03dcd4-c14c-49a3-91d2-ea7ba0feac04","html_url":"https://github.com/anvilco/node-encryption","commit_stats":{"total_commits":44,"total_committers":5,"mean_commits":8.8,"dds":0.5909090909090908,"last_synced_commit":"14e549c21e2fff7d46f7d27c948d122448f556fd"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/anvilco/node-encryption","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anvilco%2Fnode-encryption","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anvilco%2Fnode-encryption/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anvilco%2Fnode-encryption/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anvilco%2Fnode-encryption/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anvilco","download_url":"https://codeload.github.com/anvilco/node-encryption/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anvilco%2Fnode-encryption/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263294795,"owners_count":23444461,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aes","cryptography","encryption","js","node","rsa"],"created_at":"2025-06-01T20:37:56.593Z","updated_at":"2025-07-04T16:32:57.449Z","avatar_url":"https://github.com/anvilco.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Anvil Encryption\n\nThis library is a small wrapper around node's `crypto` library. It offers convenience methods for encrypting and decrypting arbitrary payloads in both AES and RSA.\n\nFor use encrypting / decrypting payloads in Anvil, you can generate an RSA keypair from your [organization's settings page](https://www.useanvil.com/docs/api/getting-started#encryption).\n\n![Horizontal Lockupblack](https://user-images.githubusercontent.com/293079/169453889-ae211c6c-7634-4ccd-8ca9-8970c2621b6f.png#gh-light-mode-only)\n![Horizontal Lockup copywhite](https://user-images.githubusercontent.com/293079/169453892-895f637b-4633-4a14-b997-960c9e17579b.png#gh-dark-mode-only)\n\n[Anvil](www.useanvil.com/developers) provides easy APIs for all things paperwork.\n1. [PDF filling API](https://www.useanvil.com/products/pdf-filling-api/) - fill out a PDF template with a web request and structured JSON data.\n2. [PDF generation API](https://www.useanvil.com/products/pdf-generation-api/) - send markdown or HTML and Anvil will render it to a PDF.\n3. [Etch E-sign with API](https://www.useanvil.com/products/etch/) - customizable, embeddable, e-signature platform with an API to control the signing process end-to-end.\n4. [Anvil Workflows (w/ API)](https://www.useanvil.com/products/workflows/) - Webforms + PDF + E-sign with a powerful no-code builder. Easily collect structured data, generate PDFs, and request signatures.\n\nLearn more about Anvil on our [Anvil developer page](https://www.useanvil.com/developers).\n\n## Install\n\n```sh\nyarn add @anvilco/encryption\n```\n\nThen use it\n\n```js\n// For RSA functions\nimport { encryptRSA, decryptRSA } from '@anvilco/encryption'\n\n// For AES functions\nimport { generateAESKey, encryptAES, decryptAES } from '@anvilco/encryption'\n\n// Could grab the keys from file (or preferably from environment variables)\nimport fs from 'fs'\nconst privateKey = fs.readFileSync('path/to/privateKey.pem')\nconst publicKey = fs.readFileSync('path/to/publicKey.pem')\n\n\n// RSA\nconst message = 'Super secret message'\nconst encryptedRSAMessage = encryptRSA(publicKey, message)\nconst origRSAMessage = decryptRSA(privateKey, encryptedRSAMessage)\nconsole.log(origRSAMessage === message) // =\u003e true\n\n// AES\nconst aesKey = generateAESKey()\nconst encryptedAESMessage = encryptAES(aesKey, message)\nconst origAESMessage = decryptAES(aesKey, encryptedAESMessage)\nconsole.log(origAESMessage === message) // =\u003e true\n```\n\n## API\n\nThere are two functions for RSA: `encryptRSA`, `decryptRSA`, and three for AES: `generateAESKey`, `encryptAES`, `decryptAES`.\n\n### AES\n\nThere are three functions for AES:\n\n* `key = generateAESKey()`\n* `encryptedMessage = encryptAES(key, plainMessage)`\n* `plainMessage = decryptAES(key, encryptedMessage)`\n\n`encryptAES` creates a unique [IV](https://en.wikipedia.org/wiki/Initialization_vector) for each message encrypted and prepends it to the resulting encrypted payload. So `encryptedMessage` will be in the format\n\n```\n\u003civ\u003e:\u003caesEncryptedPayload\u003e\n```\n\n### RSA\n\nThere are two functions for RSA:\n\n* `encryptedMessage = encryptRSA(publicKey, plainMessage)`\n* `plainMessage = decryptRSA(privateKey, encryptedMessage)`\n\nThese functions encrypt / decrypt with RSA _and_ AES. RSA has an upper limit on how much data it can encrypt. So we create an AES key, encrypt the AES key with RSA, then encrypt the actual message with AES.\n\nNote the encrypted payload (result of `encryptRSA`) is in the format:\n\n```\n\u003crsaEncryptedAESKey\u003e:\u003caesEncryptedMessage\u003e\n```\n\n#### Generating RSA keys\n\nThis library does not have a way to generate RSA keys.\n\nYou could use openssl:\n```sh\n# This generates a 2048 bit private key to file\nopenssl genrsa -out privateKey.pem 2048\n\n# This extracts the public key to file\nopenssl rsa -in privateKey.pem -outform PEM -pubout -out publicKey.pem\n```\n\n\nYou could also use something like [node-rsa](https://github.com/rzcoder/node-rsa):\n\n```js\n// Example generating a RSA keypair with node-rsa\nimport NodeRSA from 'node-rsa'\nfunction generateRSAKeypair () {\n  const key = new NodeRSA()\n  key.generateKeyPair(2048, 65537)\n  return {\n    publicKey: key.exportKey('pkcs8-public-pem'),\n    privateKey: key.exportKey('pkcs8-private-pem'),\n  }\n}\n```\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanvilco%2Fnode-encryption","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanvilco%2Fnode-encryption","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanvilco%2Fnode-encryption/lists"}