{"id":26863085,"url":"https://github.com/jwill9999/crypto_node","last_synced_at":"2025-10-12T06:16:30.534Z","repository":{"id":127600688,"uuid":"79965879","full_name":"jwill9999/crypto_node","owner":"jwill9999","description":"Nodejs Crypto module code examples","archived":false,"fork":false,"pushed_at":"2017-02-10T19:46:16.000Z","size":576,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-07-20T04:57:19.426Z","etag":null,"topics":["aes-256-ctr","buffer","buffer-encyption","crypto","decryption","decrypts","encrypted-data","encryption","hash","node","nodejs","password-checker","password-hash","pipe","salt","streams-encryption","tutorial"],"latest_commit_sha":null,"homepage":"","language":"HTML","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/jwill9999.png","metadata":{"files":{"readme":"README.md","changelog":null,"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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2017-01-24T23:43:56.000Z","updated_at":"2017-02-18T03:48:31.000Z","dependencies_parsed_at":null,"dependency_job_id":"b5712c18-f6ac-42ed-bcbb-4507066d4e9f","html_url":"https://github.com/jwill9999/crypto_node","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/jwill9999/crypto_node","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwill9999%2Fcrypto_node","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwill9999%2Fcrypto_node/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwill9999%2Fcrypto_node/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwill9999%2Fcrypto_node/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jwill9999","download_url":"https://codeload.github.com/jwill9999/crypto_node/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwill9999%2Fcrypto_node/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279010480,"owners_count":26084757,"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","status":"online","status_checked_at":"2025-10-12T02:00:06.719Z","response_time":53,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-256-ctr","buffer","buffer-encyption","crypto","decryption","decrypts","encrypted-data","encryption","hash","node","nodejs","password-checker","password-hash","pipe","salt","streams-encryption","tutorial"],"created_at":"2025-03-31T02:39:41.951Z","updated_at":"2025-10-12T06:16:30.492Z","avatar_url":"https://github.com/jwill9999.png","language":"HTML","readme":"#Crypto module in Node JS\n\n##Crypto module for text string\n\n\u003ch4\u003eEncryption\u003c/h4\u003e\n\nHere we look at converting text string to an encrypted string with the use of the crypto module in Nodejs. Here we call the function encryptMyText(textString) and pass in a text string to convert.\n\n```javascript\n\n  //Node modules required\n\n  var crypto = require('crypto');\n  const fs = require('fs');\n\n  //variables\n\n  const algorithm = 'aes-256-ctr';\n  const key = 'mMj6UXSrXbSNCGDw4aQIxXBjqIIaWTVTfUm5n5ztIdUyntR906wsvW5QCjuL';\n  \n  \n  function encryptMyText(textString){\n\n  var cipher = crypto.createCipher(algorithm,key);\n  var encrypted = cipher.update(textString,'utf8','hex');\n  encrypted += cipher.final('hex');\n  return encrypted; \n\n}\n\n\n```\n\n```javascript\n\n######function called\n  \n            \n######Variables  \n\nvar textString = 'Trial string to encrypt';\n\n            \nvar result = encryptMyText(textString)\n\n\n```\n\n```\n######Results from function\n\n            \nUnencryption textString :  Trial string to encrypt\n\n            \nThis is the encrypted string(result) : a4120c584cf3093c1b2123a4d611414022a7a6d28fd151\n\n```\n\nThe file is returned encrypted.You can now save it if you so wish.\n\n\n\u003ch4\u003eDecryption\u003c/h4\u003e\n\nHere we look at converting encrypted text string to an unencrypted text string with the use of the crypto module in Nodejs. Here we call the function decryptMyText(encrypted) and pass in an encrypted string to convert.\n\n\n```javascript\n\n  // node modules required \n\n  var crypto = require('crypto');\n  const fs = require('fs');\n\n  //variables\n\n  const algorithm = 'aes-256-ctr';\n  const key = 'mMj6UXSrXbSNCGDw4aQIxXBjqIIaWTVTfUm5n5ztIdUyntR906wsvW5QCjuL';\n\n\n  function decryptMyText(encrypted){\n\n  var decipher = crypto.createDecipher(algorithm,key);\n  var decrypted = decipher.update(encrypted,'hex','utf8');\n  decrypted += decipher.final('utf8');           \n  return decrypted;\n\n  }\n\n\n```\n\n```javascript\n\n############function called  \n            \nVariables\n            \nvar result = a4120c584cf3093c1b2123a4d611414022a7a6d28fd151            \nvar encryptedString = result;\n\n      \nvar unencryptedString = decryptMyText(encryptedString)\n\n\n```\n\n\n```\n\n############Results from function\n\n\n            \nThis is the encryptedString : a4120c584cf3093c1b2123a4d611414022a7a6d28fd151\n\n         \n            \nThis is the unencryptedString : Trial string to encrypt\n\n```\n\n\n##Crypto module for Streams\n\n\u003ch4\u003eEncryption\u003c/h4\u003e\n\nHere we look at converting Streams to an encrypted Stream with the use of the crypto module in Nodejs.\nHere we call the function encryptMyStream(fileIn, fileout) and pass in a fileIn, fileOut to convert then save. \nThis method creates a read stream with the functions first argument passed, fileIn. This\nis then piped into encryptIt function. This function converts it into an encrpted stream. We then need to output\nthis file somewhere. Here we pipe it into creatWriteStream function and pass a fileout. This then writes the data\nto a file to be saved.\n\n```javascript\n //node_modules\n\n        const crypto = require('crypto');\n        var fs = require('fs');\n        var zlib = require('zlib');\n\n        //variables\n\n        const algorithm = 'aes-256-ctr';\n        const key = '14189dc35ae35e75ff31d7502e245cd9bc7803838fbfd5c773cdcd79b8a28bbd';\n\n\n\n        function encryptMyStream(fileIn, fileout) {\n          var createReadStream = fs.createReadStream(fileIn);\n          var encryptit = crypto.createCipher(algorithm, key);\n          var zipit = zlib.createGzip();\n          var writestream = fs.createWriteStream(fileout);\n\n\n        /* \n          OPTIONAL\n          add to gzip file\n          createReadStream.pipe(encryptit).pipe(zipit).pipe(writestream);\n\n        */\n          \n          createReadStream.pipe(encryptit).pipe(writestream);\n\n          //delete old file optional include below  \n\n          writestream.on('finish', function () {\n            console.log('Encrypted stream written to disk!');\n            fs.exists(fileIn, function (exists) {\n              if (exists) {\n                console.log(fileIn + ' still exists. Deleting it now ...');\n                fs.unlink(fileIn, function (done) {\n                  console.log('Unencrypted file removed Successful');\n                });\n              } else {\n                console.log(fileIn + ' not found, so not deleting.');\n              }\n            }); //end of fs.exists\n          \n          }); //end of output.on  \n          \n        }; // end of encryptMyStream \n\n```\n\n```javascript\n\n######function called\n  \n            \n######Variables  \n\nconst streamdatain = 'files/stream.txt';\nconst streamdataout = 'files/stream.enc';\n\n######Function\n            \nencryptMyStream(streamdatain, streamdataout);\n\n\n```\n\n```javascript\n\n######Results from function\n\nThis take an unencrypted stream = test stream text from file.\n\nencrypted output before gzip = ��|V�/%�X?���F@3�4\u0019\\�w�\n\n```\n\nOnce the file is output we then emit finish. Once emitted this function checks to see if the original unencrypted\nfile is still there. If it is we call fs.unlink function. This function deletes that file leaving only the encrypted data.\n\n\u003ch4\u003eDecryption\u003c/h4\u003e\n\nHere we look at converting encrypted stream to an unencrypted stream with the use of the crypto\nmodule in Nodejs. Here we call the function decryptMyStream(fileIn, fileout) and pass in an encrypted\nstream to convert. This method creates a read stream with the functions first argument passed, fileIn. This is\nthen piped into decryptIt function. This function converts it into an decrypted stream. We then need to output\nthis file somewhere. Here we pipe it into creatWriteStream function and pass a fileout. This then writes the data\nto a file to be saved.\n\n```javascript\nDecryption code\n\n  //node_modules\n\n        const crypto = require('crypto');\n        var fs = require('fs');\n        var zlib = require('zlib');\n\n        //variables\n\n        const algorithm = 'aes-256-ctr';\n        const key = '14189dc35ae35e75ff31d7502e245cd9bc7803838fbfd5c773cdcd79b8a28bbd';\n\n\n        function decryptMyStream(fileIn, fileout) {\n\n          var createReadStream = fs.createReadStream(fileIn);\n          var decryptit = crypto.createDecipher(algorithm, key);\n          var unzipit = zlib.createGunzip();  \n          var writestream = fs.createWriteStream(fileout);\n\n          /* \n          OPTIONAL\n          add gunzip pipe to file\n          createReadStream.pipe(unzipit).pipe(decryptit).pipe(writestream);\n\n        */\n\n          createReadStream.pipe(decryptit).pipe(writestream);\n\n\n          //delete old file optional include \n\n          writestream.on('finish', function () {\n            console.log('encrypted stream written to disk!');\n            fs.exists(fileIn, function (exists) {\n              if (exists) {\n                console.log(fileIn + ' still exists. Deleting it now ...');\n                fs.unlink(fileIn, function (done) {\n                  console.log('encrypted file removed Successful');\n                });\n              } else {\n                console.log(fileIn + ' not found, so not deleting.');\n              }\n            }); //end of fs.exists\n          }); //end of output.on\n        }; // end of decryptMyStream  \n\n\n```\n\n```javascript\n######function called\n  \n            \n######Variables  \n\nconst streamdatain = 'files/stream.txt';\nconst streamdataout = 'files/stream.enc';\n\n######Function\n            \ndecryptMyStream(streamdataout, streamdatain);\n\n\n```\n\n```javascript\n\n############Results from function\n            \nThis take an encrypted stream = ��|V�/%�X?���F@3�4\u0019\\�w�         \n            \nUnencrypted output before gzip = test stream text from file\n\n\n```\n\nOnce the file is output we then emit finish. Once emitted this function checks to see if the original unencrypted\nfile is still there. If it is we call fs.unlink function. This function deletes that file leaving only the encrypted\ndata.\n\n\n##Crypto module for Buffers\n\n\u003ch4\u003eEncryption\u003c/h4\u003e\n\nHere we look at converting a Buffer to an encrypted Buffer with the use of the crypto module in\nNodejs. Here we call the function encryptMyBuffer(buffer) and pass a buffer.\nThis method takes a buffer and encrypts it. A cipher is created with crypto.createCipher(algorithm, key), and we\npass in an algorithm and key to use. The buffer is then passed into the cipher function created and the we call\nbuffer.concat on it producing an encrypted buffer which is returned to the function calling it.\n\n```javascript\n //node_modules\n\n          const crypto = require('crypto');\n          const fs = require('fs');\n\n          //variables\n\n          const algorithm = 'aes-256-ctr';\n          const key = 'mMj6UXSrXbSNCGDw4aQIxXBjqIIaWTVTfUm5n5ztIdUyntR906wsvW5QCjuL';\n\n\n          function encryptMyBuffer(buffer) {\n\n            var cipher = crypto.createCipher(algorithm, key);\n            var crypted = Buffer.concat([cipher.update(buffer),cipher.final()]); \n            \n              return crypted;        \n\n```\n\n```javascript\n\n######function called\n            \n \n\nvar result = encryptMyBuffer((new Buffer(\"hello world\", \"utf8\"));\n\n```\n\n```javascript\n\n######Output\n\nThis take a buffer = Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64\n\nencrypted buffer out(result) = \u001b$): �\tUO�\n\n```\n\n\u003ch4\u003eDecryption\u003c/h4\u003e\n\nHere we look at converting an encrypted Buffer to an encrypted Buffer with the use of the crypto\nmodule in Nodejs. Here we call the function decryptMyBuffer(buffer) and pass in a buffer         to convert (an encrypted one). This method takes an encypted buffer and decrypts it. A decipher is created with\ncrypto.createDecipher(algorithm, key), and we pass in an algorithm and key to use. The buffer is then passed into\nthe decipher function created and then we call buffer.concat on it producing an unencrypted buffer which is returned\nto the function calling it.\n\n```javascript\nDecryption code\n\n //node_modules\n\n        const crypto = require('crypto');\n        const fs = require('fs');\n\n        //variables\n\n        const algorithm = 'aes-256-ctr';\n        const key = 'mMj6UXSrXbSNCGDw4aQIxXBjqIIaWTVTfUm5n5ztIdUyntR906wsvW5QCjuL';\n\n\n        function decryptMyBuffer(buffer) {\n\n          var decipher = crypto.createDecipher(algorithm, key);\n          var decrypted = Buffer.concat([decipher.update(buffer),decipher.final()]);\n          \n          return decrypted;\n\n        }\n\n```\n\n```javascript\n######function called\n  \n            \n######Variables  \n\nvar results = decryptMyBuffer(data);\n\n\n```\n\n```javascript\n\n############Output\n            \nThis take an encrypted buffer = '\u001b$): �\tUO�;\n            \nencrypted buffer out to buffer(results) = Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64;\n\n\n```\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjwill9999%2Fcrypto_node","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjwill9999%2Fcrypto_node","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjwill9999%2Fcrypto_node/lists"}