{"id":1508,"date":"2022-11-08T11:10:20","date_gmt":"2022-11-08T11:10:20","guid":{"rendered":"https:\/\/www.workfall.com\/learning\/blog\/?p=1508"},"modified":"2025-09-24T10:49:27","modified_gmt":"2025-09-24T10:49:27","slug":"how-to-perform-encryption-and-decryption-of-messages-using-crypto-in-node-js","status":"publish","type":"post","link":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-perform-encryption-and-decryption-of-messages-using-crypto-in-node-js\/","title":{"rendered":"How to perform encryption and decryption of messages using Crypto in Node.js?"},"content":{"rendered":"<span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">9<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span>\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/VoOVPZ7vg8BgXGyBQwMgn1QOjjC8lvyJGbnjqMK_bpPnwWhYs4C_fFLD8MN1Zf7O_Hmk5Mc5Vnlf4lokwAEBWl3jJ3zIfENeAItITDvyjX5dD1XdkjiccZcwOS3RW5NxeA3BxHK4aSGfhF7vrYtcCpB6HZdYem_XEupcsZdEowfKXXkLDBA_75bMMF1SGA9i\" alt=\"How to perform encryption and decryption of messages using Crypto in Node.js?\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">Web applications have access to massive amounts of data belonging to individuals, organizations, and governments. The more data that is accessed, the greater the risk to data security. Cryptography and encryption techniques are used by software developers to protect sensitive data from malicious parties.<\/p>\n\n\n\n<p class=\"has-text-align-justify\">Cryptography is used to protect data stored in a database or transferred over a network in the software development industry. You must handle, move, and store data in a safe and secure manner.<\/p>\n\n\n\n<p class=\"has-text-align-justify\">As a result, as a Node.js developer, you must understand how to encrypt and decrypt data in order to secure the data processed by your system. For data encryption and decryption, Node.js includes a built-in library called crypto.<\/p>\n\n\n\n<p class=\"has-text-align-justify\">The goal of encryption and decryption is to increase security. This blog will show you how to use Crypto in Node.js to encrypt and decrypt messages with step-by-step instructions. Let\u2019s get started! \ud83d\ude80<\/p>\n\n\n\n<h2>Cryptography in Node.js<\/h2>\n\n\n\n<p class=\"has-text-align-justify\">For software development, cryptography is essential. The process of converting plain text into unreadable text and vice versa is known as cryptography. Only the sender and receiver of the information understand its content in this manner.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/NwFsIGWzbT6PUriyUbjBkrzIv5P0pMd0nV2BJiQEBlyV8sZVSeXAXskgYkdX_qMJ6U7EclezmsTWuRyCFfPS7X9GcYhRLzNlh6ZYN9C4iGAJ77435YNmUmyqzy1ldHrNS0zO6mTr_dv5gQu5SQTNoiEuHdjZ73XmOr-xGqHNpdjeA_jZxMqeqQqtRrkOfgVQ\" alt=\"Cryptography in Node.js\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">You can hash passwords in Node.js and store them in the database so that data cannot be converted to plain text after it has been hashed; it can only be verified. When malicious actors gain access to your database, they will be unable to decode the encrypted data. Other user data can also be encrypted and decrypted during transmission.<\/p>\n\n\n\n<h2>What exactly is a Node.js crypto module?<\/h2>\n\n\n\n<p class=\"has-text-align-justify\">The <a href=\"https:\/\/nodejs.org\/api\/crypto.html\">Node.js crypto<\/a> module provides cryptographic functions to aid in the security of your Node.js application. It includes wrappers for the hash, HMAC, cypher, decipher, sign, and verify functions of OpenSSL.<\/p>\n\n\n\n<p class=\"has-text-align-justify\">Because crypto is built into Node.js, it does not necessitate a lengthy implementation process or configurations. Unlike other modules, Crypto does not require installation before being used in a Node.js application.<\/p>\n\n\n\n<h3>Crypto Classes in Node.js<\/h3>\n\n\n\n<p>Let&#8217;s take a look at the crypto classes that allow us to implement cryptography.<\/p>\n\n\n\n<ul><li><strong>Cipher: <\/strong>The <em>Cipher<\/em> class is in charge of encrypting data. The<em> Cipher<\/em> class is invoked when a user enters a password during registration to encrypt the password.<\/li><\/ul>\n\n\n\n<ul><li><strong>Decipher:<\/strong> Decrypting encrypted texts is the responsibility of the <em>Decipher<\/em> class. When sending information securely to another developer, you must encrypt it. The only way for the recipient to read the information is to decrypt it. This is precisely what the<em> Decipher<\/em> class accomplishes.<\/li><\/ul>\n\n\n\n<ul><li><strong>Hash:<\/strong> The <em>Hash<\/em> class is used for hashing plain text. Hashing is simply the conversion of plain text into hash functions.<\/li><\/ul>\n\n\n\n<ul><li><strong>Certificate: <\/strong>A <em>Certificate<\/em> consists of a key pair and other information used to encrypt electronic documents. A certificate can generate a session key for the purpose of securely transmitting data over the internet. With the crypto <em>Certificate<\/em> class, you can use OpenSSL&#8217;s SPKAC implementation to work with Signed Public Key and Challenge (SPKAC).<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/7qxcWgLAsMElFprKNkeCQwwkdVYzkN4JEV6rAB9xVbqu4kAL0P9kkYlvImYDe2xI3zvon-yBTnGAXcZqZy4uKrtfIVoMEsrW4zisQFtvX03cOu726ed0qRZvYrXDC9N0G-SEDG1OBYrSgP9rQFEdiSIFjhONy72Vi_NoHVE77d1yrwvePXWEWEpmSHkDH1bz\" alt=\"Crypto Classes in Node.js\"\/><\/figure>\n\n\n\n<ul><li><strong>DiffieHellman:<\/strong> Diffie-Hellman key exchanges are used by Crypto&#8217;s <em>DiffieHellman<\/em> class. Diffie-Hellman key exchange is a technique for securely transmitting cryptographic keys over public networks. This protects keys that are only used by information senders and receivers.<\/li><\/ul>\n\n\n\n<ul><li><strong>ECDH:<\/strong> Elliptic-curve The Diffie-Hellman (ECDH) algorithm is used to create a shared public-private key pair with an elliptic curve.<\/li><\/ul>\n\n\n\n<ul><li><strong>HMAC: <\/strong>With the use of a shared secret, hash-based message authentication code (HMAC) allows you to provide digital signatures. The HMAC method is used by Crypto&#8217;s <em>HMAC<\/em> class for digital signing.<\/li><\/ul>\n\n\n\n<ul><li><strong>sign: <\/strong>The <em>sign <\/em>class is used to generate signatures. Cryptographs must be signed and later verified for authentication in order for cryptography to be efficient. When the receiver receives a cryptograph, they can check the signature to see if it is genuine.<\/li><\/ul>\n\n\n\n<ul><li><strong>verify:<\/strong> The only way to determine the value of a hashed cryptography is to use the <em>verify <\/em>method.<\/li><\/ul>\n\n\n\n<h2>Hands-on<\/h2>\n\n\n\n<p><strong>Required installations for the process?<\/strong><\/p>\n\n\n\n<ul><li><strong>Node.js:<\/strong> It is a JavaScript runtime environment that executes JavaScript code outside the browsers.<\/li><li><strong>Crypto: <\/strong>It is an inbuilt Node.js package that provides cryptographic operations to help secure Node.js applications.<\/li><\/ul>\n\n\n\n<p class=\"has-text-align-justify\">In this hands-on, we will have a look at how we can make use of the crypto package in node.js to encrypt and decrypt a secret message. To do so, we will first begin with creating the package.json file and then initialize npm within the newly created directory. Then we will install the crypto package for using it for encryption and decryption. Verifying the package installation, we will then proceed further and import the package into our codebase. We will then define the type of algorithm that we will be used for encryption and the decryption purpose.<\/p>\n\n\n\n<p class=\"has-text-align-justify\">Then we will make use of the package to generate 16 bytes and 32 bytes of random data to be able to use it as a secret key for the encryption purpose. Defining the input and the output type, we will test out the encrypted output. On success, we will use the same package to perform the decryption process. Once done, we will then test out the decrypted message as well and verify the same with the message that we encrypted.<\/p>\n\n\n\n<p class=\"has-text-align-justify\"><strong><em>Note:<\/em><\/strong><em> Please ensure that Node.js is properly installed onto the local machine or else it might display multiple errors while following the process.<\/em><\/p>\n\n\n\n<p>Create a new directory on your local machine.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/BPJvfvIW5u5sc78FYhMQJgyKILdEh4c6yYwa4PRz2lD9DF16LT3Pxm_b8LipRMbgAF3NOl4eyD3L-EQR_firQtzmXtQF6aHCgIIqBe0gfo-wy9CFHJTNRSooeNOZJTIzeBBjJDE9_5ABgxcCfqFKLD9UulY2sdRg0Y7JxCBeui1nzFpKmtQfbgW0v06SRGs4F6AI8EtG3A\" alt=\"IMG_256\"\/><\/figure>\n\n\n\n<p>Open the newly created directory into a code editor.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/tLOgnoF5mqZ2cS0pXaAxi-ehFZFaQpQhRMzQ9ClmH_eYo8qefdM1wAx0nnKVvXhiXSePnM9vbhGqHtARO20mWew4_uoLC43j-wlgIoWE9sxcz8LtNfrZqcObbt2e59Cv78zHzUXRvA7KuC3CqSrQMG9L2cRa24Cbw-J8zrxAE9P3s5z0RX8rAg3MXvlg__IR7WVpoz2SuA\" alt=\"IMG_257\"\/><\/figure>\n\n\n\n<p>Right-click in the left navigation pane and click on New file.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/57lA8Qtve1l34gZS8oXml8xq-ZmaN9i1oIfVip0fwAbVWLAgIu15WTRAoXIs1HubuSORId_RMhZsp1YqafOCf-5EFPVCjaIDJsEnkfRzDYuyXUUFtJPJ3NSg_Tajl91SMlO59UB3mFgsc9l5WvfthNahu6llRXek1kLWmICn3UeDAVkPpi0X5TjY6sg82vTGxNSIsfI8Wg\" alt=\"\"\/><\/figure>\n\n\n\n<p>Create a new file with the name <em>package.json<\/em>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/AcjMLP1BP0wIIQofznIwHxDbURDY4Pc-tKm67ObLynw4aS3hQEh2xFUauqX3-hnyqUQ2rn2HwQKDhifB1kbY7MA_ZHCeZ8ppKZk8gcDOsrxFVUwWInYllo6-EU9pCh60ap8C8F3VCHtIjM2NKXL5cCaT-o3KH5tPF5aVrQnzz0D78CeVNI6YDoFOnDSmYtyRm9XdlvNJyw\" alt=\"\"\/><\/figure>\n\n\n\n<p>Right-click in the newly created directory and click on Git Bash Here.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/DlVSQ_ewvx-ofaJaqPgVPVAv6aGVOWNEKISnKwec4Pi8EB6VmQaHzBw_nK5tVLYjRF61c8IYBRBA7cTPiyYcLROif_oHDt3YGwTOYnwlS1hZgx0iUqGW2g0tCTMNgluG-n32vzQEQgI76Px9PqYOWYaOZISZyDdzfpVCjgakmOUgYJad4F5InwYcqKo5XRHs5LqCY_7Z2Q\" alt=\"encryption and decryption of messages using Crypto in Node.js\"\/><\/figure>\n\n\n\n<p>Run the command: <code>npm init<\/code><\/p>\n\n\n\n<p>Enter a name for the pkg and press enter to continue with the default settings.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/MbVl8yA-gW9iVSacGz_AiZYwZ2bfoObh24wyrGrgN3SeVrqXYrv3odI_7Mz6nwLL-r-yD69WCesknIZy8zhCnT69kW7txCGPeyU2I--03QigyGqR5_KaB6UiOFkCZrXa67DLyJ7B8_zSeZSxVEkZ1gogwVrcTdt1nrsPkja6KJ5o5YOKD0T91-MC6eG8h-rwCyCjAG_iyQ\" alt=\"encryption and decryption of messages using Crypto in Node.js\"\/><\/figure>\n\n\n\n<p>Finally, you will see the structure as shown in the image below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/QnQ2RwznD39eo2R75ewIXMbMS0ZxXfD-UfUPqYnOOT6NVJGb1aMe65KwyaKsccdVa2aK0x_xm82qZrHphWF3quMYDZ2utV0INCrsS7sJ2ld_WkhgFm5y0-lQ9ITrApnCVoTATT23iWGEouLFZqcxoMoPTCE7uqgIhiV7lb3cGT54t_bn9Mf3ZcaWc_2x9UedhAXkMuq9DQ\" alt=\"\"\/><\/figure>\n\n\n\n<p>You can cross-verify the same by looking at the <em>package.json<\/em> file.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/akT8EGclDFhZTFVe3oxrHWkXChbd0-voL_GQWD7_yisCUtfXWUlBSJrvlRo4p3BQqfJKdb1YditlgIYveMhTGdngOno4UtCx0gClJsG3XMfIRUQZHBlkYlzy_k69a3iymm1CfZrDzN68UOsnoo_Xs6wgINY3ks2jsys9m-fnpOgBElk5A-9g5PjJ7pgfKFV453LT_msx9Q\" alt=\"encryption and decryption of messages using Crypto in Node.js\"\/><\/figure>\n\n\n\n<p>Now, we need to install the crypto package. To do the same:<\/p>\n\n\n\n<p>Execute the command: <code>npm install crypto --save<\/code><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/J3ziZtOVMUYC695CYwQTXPepYcor6fYUv_kNBOqmIetEyc5YRgHS1yZ1TDxao6mLN4r3gU9gKIMnSaXZkIVeQWKnyC3ZeSk-2IOURT028GMAbm8ppyq_-FvTwBrPbP1jEfSYYHmPi1McirkvrYJriUlRsSGpDiz130DPLJ3LrqN2FEEjE8z-3FAcGF56NMP5ggxx6b25tg\" alt=\"\"\/><\/figure>\n\n\n\n<p>On successful installation, you will see the result as shown in the image below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/ox39lNkYkEibpvOTD9_E3gjo-Bj6LHaTKX440oJ1ZRmKgmQY30kNVJvknfX3SqUczjWfHdhB6Ej64Lua0P3Qdl20TM-ugLtuP_tzALZoOk7aT4EY5McsHDq_aWaguv9XEHZDp3sqeR6dCRPn8tRVRBiHT543TWDTe920_oZg_oni_G2l3jInADN3N6NCWO-1h16D8ofKnQ\" alt=\"\"\/><\/figure>\n\n\n\n<p>If you look at the file structure, you will get to see the following structure.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/M0Kt6j9ySY1-UxMaUjRqhv_gu1jf7FJnvx2ppZM0eXqEMTi3QgLJqhhitpxruJeina7TbvR6mOyxgLvtgaeQRJPDl8EFoeVHddnkZm1G2AoXLGAD14SGwUQkpq24N700SXNdi6WfKlPMTSN7cICSni_spg-CFpoBU8DXJ3elcr94ET2NVtm-L2O_8iiP6m9KCLrcETG5vQ\" alt=\"\"\/><\/figure>\n\n\n\n<p>Open the <em>package.json<\/em> file to verify the installation of the crypto package.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/V7bcPeoqDZ3WSuI0PzZJKexfs-5kgzcW2vmEx6TXnkmYIdMVDyrBwYw04rob-gJ0n6p26Ll9CB9fvzri8PAscBg_NlxXpxZYcSfHbk-uify6Ov5YATu79PlnkaQpNRw_yzkj4OHl1X2j7jWM45CxT9lhwQ0BFEqYjLdsFdd0o8OlpOqnmbTDwGyD_7nBcJYyxPyP6q5UnQ\" alt=\"encryption and decryption of messages using Crypto in Node.js\"\/><\/figure>\n\n\n\n<p>Now, in the file structure, create a new file with the name <em>index.js<\/em>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/jKFRUC71t9iqeKgqYb-9QTp1PzJoPaNiibr3OkOnY9_JJl4awbAJ4Vh3KAHG8_RnnYrgt6tShIGFEPBp3k7CqNlVkvkBJvrr8x-fy2_zoRnKqGSy7yhIV5MUuJiaNw6-fPUxScmFidlKhLwf4kiH-FfODwhJuh9-sozbVX8B4tUfW2yP4O3Q4rJ-kHCW5SIwF_F5wgCayA\" alt=\"\"\/><\/figure>\n\n\n\n<p>Open the <em>index.js<\/em> file and import the crypto package using the code as shown in the image below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/D7arlFU0ZmSCfS2mBpTm0dCUvYsR5nDItPFkIjzRIha3R3DEtcMbjGUty2fYOhTPWNMWibDxWf2IpTV_vtPwbySVuqEXQKPADxsBQ35wr7VvsQWoUm7WeoaV4a9YtVkI-loCE6ZT4NaIBwr-TsjHyuH5ohrLIah6jcBa_ySyZQjoKs0n_nuHL5BMO52jpHc84YnSAhSGzA\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">Now, to perform the encryption process, we need to use an algorithm. Use the code as shown in the image below. We will be using the \u2018<em>aes-256-cbc<\/em>\u2019 algorithm.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/4Yjy8GhmNC07ObCfBU-ufSj5prH3a9giaqYUsHqEn5aX3OgZkfitsYoswB7aHMnSeU6DWOrGo5uR4-mX1LRgn-WN3fQicvD1fY8Lt-UCbWi9PwXjDFLCSU3MvQ90QxlPyPV-PRp6URwKWsenNWULmBelNRJ3ZDSXJTPpns9SvtIHDG0Kn9j3OndUMXIbRQB81MBByA9Tdw\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">The&nbsp;<em>crypto.randomBytes()<\/em> inbuilt&nbsp;method of the crypto package is used to generate random data of 16 bytes or 32 bytes as provided in its argument. The&nbsp;inVec&nbsp;(ie. The initialization vector) is used to store the 16 bytes of random data from the&nbsp;<em>crypto.randomBytes()<\/em> inbuilt&nbsp;method.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/OIVfx2xPYDKUka4kfuNucovc2wZkUSAbS7nUa-3L9I8dwf4bM6pcJ2EByy3FxKpbD6VuHyzFnwuSGqGRJNkIWjZXqxcCvOuD_RWOKqzLPwjUzU_uiT3Spcte0zyRnZc2CWo54mLpxs7LwUtT1czWP6vNdtnHjohtbrw1XXn8Bk6OiTKHPXHmYl10rxawYCiENBkeppnG4w\" alt=\"\"\/><\/figure>\n\n\n\n<p>Initialize a variable consisting of the secret message.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/WPjU2edg7l1sGHZFUXieI19846Ibx4i0BQn0sEBkIFXafScKwo0JEG4WOE9HEAeQc_yf2SaInavOa36b9T-1us7OWBoGVfzmXYSl5Lzd16BandDMCcsJcyHCWXVo1qc4hNNiBtPgD9BOpswQh7L7IxJGnTO68FWmgW0KIxXJnUUWHXAGv8iSePoYfSOFweUwT7-VFbdusA\" alt=\"encryption and decryption of messages using Crypto in Node.js\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">The secKey (ie. Securitykey) stored the 32 bytes of random data generated using <em>crypto.randomBytes()<\/em> inbuilt method.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/nnReXQDzaCTa0ktJuckUmB_DHhzrT1d3c2-p7X2zDdTVLLNgKIQ2WdgTTVCHk_JeE2zL8TEZ27e_yYFJVRBb_vuo7XjEhCp8ZqEVAgeZ0FAwy-SWTf_70a5oOd4ddgGBhvDVb_NtNYSjILgbCxiBhyXDpauTVVNNPnE2L1lXjCRYiMNKA6i4h1gy5ENjeBn14A54cTemZg\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">We need to use the inbuilt cipher function (ie. <em>createCipheriv()<\/em>) for encrypting the data. It takes three arguments as follows:<\/p>\n\n\n\n<ul><li>The type of algorithm to be used for encryption<\/li><li>The security key (variable holding 32 bytes of randomly generated data)<\/li><li>The initialization vector (variable holding 16 bytes of randomly generated data)<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/M6TC2f9vB9qoAwHcSlhzPTHch6CAyUIJs7Qd_J7ZnMepJudttMnEMrH48gYsrvtXtuZ1k_JI504Ozwvmw4pzYNy59D-tplpymBco6HCQTkcvEZZoifYs12HxYl-BGyBtRFqdTCuXr_dhr5ccam3EkDOwReW8dE1H718mkxmdDx2-wkt8ST47IoVu4Ucb6i6vTpOdXwbSJQ\" alt=\"encryption and decryption of messages using Crypto in Node.js\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">Finally, in order to encrypt the message, we need to use the <em>update()<\/em> inbuilt method of the crypto package which takes the argument as follows:<\/p>\n\n\n\n<ul><li>The message to be encrypted<\/li><li>The input encoding style (utf-8)<\/li><li>The output encoding style (hex)<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/Hd8VkPQVsBun6sK7redwpPMe4Pne7_onTZtfwnKe1MjrNLNTaYtCvhwgNJSsAh05OMwYOV6rOhF7IEj7nnQvY5JZimWu6m8J67Bg13r_c8j5U4b6of1B6GZAGKSRtoVTzW7uHPCKKVu136gKtUemdFajVS4f4TxAvXuM6rCOXHmdLszZ403gEiVbYNZQLA1Aq0uwjkGh7g\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">Now, when encryption is being performed, we need a way to stop the execution of the algorithm once the message is encrypted. To do so, we can use the <em>final()<\/em> inbuilt method using the code as shown in the image below and finally console out the encrypted message.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/R4GA_7YSfNNTT07rLIdbQNUD6DUmTSxupZ8eU4sPihcPqEXt-gnQzVMXXpTDyMQHC52T_oOyhbQCbjBAmQIO16OA5Wm6fjBBkig4PXm6d5mKdXMM2Qpx6UFfh8ruvTs-DndSzjUugx2NNdyEn1BK8WQbDmEhVE9tQEdue2lbYeZq9JXGDhiawqMLuXG2r0pjjT69QCDv6g\" alt=\"encryption and decryption of messages using Crypto in Node.js\"\/><\/figure>\n\n\n\n<p>Run the application using the command:<\/p>\n\n\n\n<p><code>node index.js<\/code><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/FYUZmhgCqzK66KlBE9oyZLdndgNAEVvJZKouVjl4pIm91dv6NAHa3t8j20lJCDGQS_Y1sKietc3MZNETbti4Fvy5ebDj8wKWBc0N1JfXM92n9fLtRHHucJHz5NSZxjlODek8UuLdeycfmCLu0-8BBnNks6jc7Hir73atWGboEpjJ5u3fVUDE9Lz4ngBA-tMG8-vrd1gO3g\" alt=\"\"\/><\/figure>\n\n\n\n<p>On successful execution, you will see the encrypted message as the output as shown in the image below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/A614n8tkhLsRYZfNtLAZUYLrnWK0LhmFas_BYUwVtfT2_gFI6qHqiOJHKYJxl2Pts-NxQvpAlaAG7HV6a4H_doZ_EhKBn2dGUOiuSIPFSPP7lpOiEDo7HKdqaSHtpoOPehzncT5JmGWDz3pNhVF6qXdAfm3d4J-KN72ttwCIqnYIO96w1dMPtQPS2SXnikhwhsCl_MrJNQ\" alt=\"\"\/><\/figure>\n\n\n\n<p>Now, since the encryption is done, we need to perform the decryption of the message as well. To do so, we will make use of the <em>createDecipheriv()<\/em> inbuilt function of the crypto package. It takes the following as the arguments:<\/p>\n\n\n\n<ul><li>The type of algorithm to be used for decryption<\/li><li>The security key (variable holding 32 bytes of randomly generated data)<\/li><li>The initialization vector (variable holding 16 bytes of randomly generated data)<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/Jp4UkXSMhgllXUTApgmJN0GkZH0JNrQjpAdQBSzcE0LWCzL4ZeqWVbIAi-pbVtlSGEw8C9iajknVoZ0pEpmjuTRCuX9c_XXY5UqdcsJRLOVVyKzUFTUr2lqkEbg8q-rHjCQWtpoptV11g3ZO48EN4Vq6IabF72r7lcOpyBrvNQU64Gd2ejuI8ub6VxYnSNF-FyChkAfi6w\" alt=\"\"\/><\/figure>\n\n\n\n<p>To decipher the message, we will make use of the <em>update()<\/em> inbuilt method that takes in the following arguments:<\/p>\n\n\n\n<ul><li>The encrypted message<\/li><li>The input encoding style (hex)<\/li><li>The output encoding style (utf-8)<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/nBZMLz_Jea3zrII-UkNh0xMZcv82Xy5RCmVrgd90yNLQ2eMw36-1F6yO8ErUoVkONUsFg1IygLte-7HIU44MRuRWpe9fMipafDVcMDjxva5snrZHESJqPIn4rLSYLdIj5GEvQmTuLwtwPpxzuMF0Dk4pf0DMiJthFVe6l8wPqiY3TBESfPvtFQGeYOvmU8S18XnfkldtkQ\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">Finally, once the decryption is performed, we will have to stop the execution of the algorithm. To do so, we will again make use of the <em>final()<\/em> inbuilt method using the code as shown in the image below and output the decrypted message.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/tw8Jv8psnBcnZPwr2YF0CfgfKSujiQvD9HValWDfvgvXMhUwyxPmXpjmHrd5V8IUCGEwUCrZd36qXTOF6m4z_zYkYdnULMIlcjL2OSR8ofyVgNTWnJumTqbv9wsEp90zHI00tw3YuMka-X2DtUpvo7OgwsMN-09-0gER3koLTts8hcy5M6_vlwjwr8mkaEkjLBdeoImWdA\" alt=\"\"\/><\/figure>\n\n\n\n<p>The entire code along with the encryption and decryption of the message is shown in the image below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/NGxoE_yiIxW59rD62_pjfjWXus-8itg1mP14RzvK0LO5gnsB6eIutjYp6tq7ln6CXYT_om0up-lAJ7lO6Vwa1SStDCw7jETL-QMomS_L5RDZF_HHt12IjRdHmmT_Krcq-R-OOU9Bao10dBvY5UbDMmaWoYZrd8o9lDuXJGFwW8K6FDE5h6uPXO0uJ7Oe8pi1m4tf89fZQQ\" alt=\"encryption and decryption of messages using Crypto in Node.js\"\/><\/figure>\n\n\n\n<p>Run the application using the command:<\/p>\n\n\n\n<p><code>node index.js<\/code><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/FYUZmhgCqzK66KlBE9oyZLdndgNAEVvJZKouVjl4pIm91dv6NAHa3t8j20lJCDGQS_Y1sKietc3MZNETbti4Fvy5ebDj8wKWBc0N1JfXM92n9fLtRHHucJHz5NSZxjlODek8UuLdeycfmCLu0-8BBnNks6jc7Hir73atWGboEpjJ5u3fVUDE9Lz4ngBA-tMG8-vrd1gO3g\" alt=\"\"\/><\/figure>\n\n\n\n<p>On successful execution, you will see the encrypted and the decrypted message as shown in the image below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/Z80PrP09SyJbtD8hCUvlHICS-tREwTEpTIR5uhYHROuBRFWvntgxgI7mLhtY1xuBe9JFicBs2lawTnLDZusAuzprwlZXQgZhNacQZ-XKjQCWubmzKSYZXBZNru5c2yDmX8Yz7lvexrk0OBZGn0cb9wHP981fM5b1ZXDasM5SC3odwdgCMCFlkmD8WSvd7_KEQdXGcKlxIQ\" alt=\"\"\/><\/figure>\n\n\n\n<h2>Conclusion<\/h2>\n\n\n\n<p class=\"has-text-align-justify\">In this hands-on, we had a look at how we can make use of the crypto package in Node.js to encrypt and decrypt a secret message.<\/p>\n\n\n\n<p class=\"has-text-align-justify\">To do so, we first began with creating the package.json file and then initialized npm within the newly created directory. Then we installed the crypto package for using it for encryption and decryption.<\/p>\n\n\n\n<p class=\"has-text-align-justify\">Verifying the package installation, we then proceeded further and imported the package into our codebase. We then defined the type of algorithm that we would use for encryption and the decryption purpose.<\/p>\n\n\n\n<p class=\"has-text-align-justify\">Then we made use of the package to generate 16 bytes and 32 bytes of random data to be able to use it as a secret key for the encryption purpose. Defining the input and the output type, we tested out the encrypted output.<\/p>\n\n\n\n<p class=\"has-text-align-justify\">On success, we used the same package to perform the decryption process. Once done, we then tested out the decrypted message as well and verified the same with the message that we encrypted. We will come up with more such use cases in our upcoming blogs.<\/p>\n\n\n\n<p><strong>Meanwhile\u2026<\/strong><\/p>\n\n\n\n<p class=\"has-text-align-justify\">If you are an aspiring Node Lover and want to explore more about the above topics, here are a few of our blogs for your reference:<\/p>\n\n\n\n<ul><li><a href=\"https:\/\/www.workfall.com\/learning\/blog\/pm2-daemon-process-manager-for-a-node-js-application\/\">How to set up a PM2 daemon process manager for a Node.js application?<\/a><\/li><li><a href=\"https:\/\/www.workfall.com\/learning\/blog\/how-to-upload-single-or-multiple-images-to-a-local-directory-with-multer-in-node-js\/\">How to upload single or multiple images to a local directory with Multer in Node.js?<\/a><\/li><li><a href=\"https:\/\/www.workfall.com\/learning\/blog\/how-to-show-flash-messages-in-node-js-using-the-connect-flash-package\/\">How to Show Flash Messages in Node.js using the Connect-flash Package?<\/a><\/li><li><a href=\"https:\/\/www.workfall.com\/learning\/blog\/how-to-send-customized-emails-by-integrating-sendgrid-with-a-node-js-application\/\">How to send customized emails by integrating SendGrid with a Node.js application?<\/a><\/li><\/ul>\n\n\n\n<p><strong>Keep Exploring -&gt; Keep Learning -&gt; Keep Mastering&nbsp;<\/strong><\/p>\n\n\n\n<p class=\"has-text-align-justify\">At <a href=\"https:\/\/www.workfall.com\/\">Workfall<\/a>, we strive to provide the best tech and pay opportunities to kickass coders around the world. If you\u2019re looking to work with global clients, build cutting-edge products and make big bucks doing so, give it a shot at <a href=\"https:\/\/www.workfall.com\/partner\/\">workfall.com\/partner<\/a> today!<\/p>\n\n\n\n<p><\/p>\n\n\n<style type=\"text\/css\"><\/style><section id='' \n                class='helpie-faq accordions faq-toggle open-first groupSettings-518__enabled' \n                data-collection='' \n                data-pagination='0' \n                data-search='0' \n                data-pagination-enabled='0'\n                role='region'\n                aria-label='FAQ Section'\n                aria-live='polite'><h3 class=\"collection-title\">Frequently Asked Questions:<\/h3><article class=\"accordion \"><div class='helpie-faq-row'><div class='helpie-faq-col helpie-faq-col-12' ><ul><li class=\"accordion__item \"><div class=\"accordion__header \" \r\n                id=\"accordion-header-post-2949\"\r\n                role=\"button\"\r\n                aria-expanded=\"false\"\r\n                aria-controls=\"accordion-content-post-2949\"\r\n                data-id=\"post-2949\" \r\n                data-item=\"hfaq-post-2949\" \r\n                style=\"background:transparent;\" \r\n                data-tags=\"\"\r\n                tabindex=\"0\"><div class=\"accordion__title\">Q. Which crypto library should I use in Node.js?<\/div><\/div><div id=\"accordion-content-post-2949\" \r\n                class=\"accordion__body\" \r\n                role=\"region\"\r\n                aria-labelledby=\"accordion-header-post-2949\"\r\n                style=\"background:transparent;\"><p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">9<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span><span style=\"font-weight: 400\">\u00a0<\/span><b>A: <\/b><span style=\"font-weight: 400\">You can use Node.js\u2019s built-in <\/span><span style=\"font-weight: 400\">crypto<\/span><span style=\"font-weight: 400\"> module (which supports many primitives: symmetric, asymmetric, hashing, HMAC) or use higher-level wrappers (e.g. <\/span><span style=\"font-weight: 400\">crypto-js<\/span><span style=\"font-weight: 400\">, <\/span><span style=\"font-weight: 400\">node-forge<\/span><span style=\"font-weight: 400\">). For simplicity and security, the built-in <\/span><span style=\"font-weight: 400\">crypto<\/span><span style=\"font-weight: 400\"> is preferred because it\u2019s maintained and optimized.<\/span><\/p>\n<\/div><\/li><li class=\"accordion__item \"><div class=\"accordion__header \" \r\n                id=\"accordion-header-post-2950\"\r\n                role=\"button\"\r\n                aria-expanded=\"false\"\r\n                aria-controls=\"accordion-content-post-2950\"\r\n                data-id=\"post-2950\" \r\n                data-item=\"hfaq-post-2950\" \r\n                style=\"background:transparent;\" \r\n                data-tags=\"\"\r\n                tabindex=\"0\"><div class=\"accordion__title\">Q. Symmetric vs asymmetric encryption \u2014 when to use which?<\/div><\/div><div id=\"accordion-content-post-2950\" \r\n                class=\"accordion__body\" \r\n                role=\"region\"\r\n                aria-labelledby=\"accordion-header-post-2950\"\r\n                style=\"background:transparent;\"><p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">9<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span><span style=\"font-weight: 400\">\u00a0<\/span><b>A:<\/b><\/p>\n<ul>\n<li><b>Symmetric (same key for encrypt\/decrypt):<\/b><span style=\"font-weight: 400\"> faster; good for encrypting large payloads where both parties share a secret key (e.g., AES).<\/span><span style=\"font-weight: 400\">\n<p><\/span><\/li>\n<li><b>Asymmetric (public\/private key):<\/b><span style=\"font-weight: 400\"> used for key exchange, digital signatures, or when parties don\u2019t share a secret. Performance is slower, so often used only to encrypt small data (e.g. symmetric key).<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\"> A common pattern: generate a symmetric key, encrypt data with it, then encrypt the symmetric key with the recipient\u2019s public key.<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><\/li>\n<\/ul>\n<\/div><\/li><li class=\"accordion__item \"><div class=\"accordion__header \" \r\n                id=\"accordion-header-post-2951\"\r\n                role=\"button\"\r\n                aria-expanded=\"false\"\r\n                aria-controls=\"accordion-content-post-2951\"\r\n                data-id=\"post-2951\" \r\n                data-item=\"hfaq-post-2951\" \r\n                style=\"background:transparent;\" \r\n                data-tags=\"\"\r\n                tabindex=\"0\"><div class=\"accordion__title\">Q. How do I handle initialization vectors (IV) and salts correctly?<\/div><\/div><div id=\"accordion-content-post-2951\" \r\n                class=\"accordion__body\" \r\n                role=\"region\"\r\n                aria-labelledby=\"accordion-header-post-2951\"\r\n                style=\"background:transparent;\"><p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">9<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span><b>A:<\/b><\/p>\n<ul>\n<li><span style=\"font-weight: 400\">Use a unique, random IV per encryption (but IVs need not be secret, just unpredictable).<\/span><span style=\"font-weight: 400\">\n<p><\/span><\/li>\n<li><span style=\"font-weight: 400\">Use a salt when deriving keys from passwords (e.g. with PBKDF2).<\/span><span style=\"font-weight: 400\">\n<p><\/span><\/li>\n<li><span style=\"font-weight: 400\">Store the IV (and salt) alongside the ciphertext (e.g. prefix it).<\/span><span style=\"font-weight: 400\">\n<p><\/span><\/li>\n<li><span style=\"font-weight: 400\">Use authenticated encryption modes (e.g. AES-GCM) so you can verify integrity and detect tampering.<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><\/li>\n<\/ul>\n<\/div><\/li><li class=\"accordion__item \"><div class=\"accordion__header \" \r\n                id=\"accordion-header-post-2952\"\r\n                role=\"button\"\r\n                aria-expanded=\"false\"\r\n                aria-controls=\"accordion-content-post-2952\"\r\n                data-id=\"post-2952\" \r\n                data-item=\"hfaq-post-2952\" \r\n                style=\"background:transparent;\" \r\n                data-tags=\"\"\r\n                tabindex=\"0\"><div class=\"accordion__title\">Q. How to deal with encoding (binary, base64, hex) when encrypting strings?<\/div><\/div><div id=\"accordion-content-post-2952\" \r\n                class=\"accordion__body\" \r\n                role=\"region\"\r\n                aria-labelledby=\"accordion-header-post-2952\"\r\n                style=\"background:transparent;\"><p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">9<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span><span style=\"font-weight: 400\">\u00a0<\/span><b>A: <\/b><span style=\"font-weight: 400\">Data encryption yields binary output (buffers). You\u2019ll often convert them to base64 or hex before sending through JSON or URLs. On decryption, convert back to Buffer and decrypt. Be consistent between encryption and decryption steps.<\/span><\/p>\n<\/div><\/li><li class=\"accordion__item \"><div class=\"accordion__header \" \r\n                id=\"accordion-header-post-2953\"\r\n                role=\"button\"\r\n                aria-expanded=\"false\"\r\n                aria-controls=\"accordion-content-post-2953\"\r\n                data-id=\"post-2953\" \r\n                data-item=\"hfaq-post-2953\" \r\n                style=\"background:transparent;\" \r\n                data-tags=\"\"\r\n                tabindex=\"0\"><div class=\"accordion__title\">Q. What security pitfalls should I watch out for?<\/div><\/div><div id=\"accordion-content-post-2953\" \r\n                class=\"accordion__body\" \r\n                role=\"region\"\r\n                aria-labelledby=\"accordion-header-post-2953\"\r\n                style=\"background:transparent;\"><p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">9<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span><b>A:<\/b><\/p>\n<ul>\n<li><span style=\"font-weight: 400\">Reusing IVs (especially in CTR\/GCM) is catastrophic.<\/span><span style=\"font-weight: 400\">\n<p><\/span><\/li>\n<li><span style=\"font-weight: 400\">Using weak keys or passwords without proper key derivation (use PBKDF2 \/ scrypt \/ Argon2).<\/span><span style=\"font-weight: 400\">\n<p><\/span><\/li>\n<li><span style=\"font-weight: 400\">Ignoring authentication (don\u2019t use plain AES-CBC without HMAC or use GCM).<\/span><span style=\"font-weight: 400\">\n<p><\/span><\/li>\n<li><span style=\"font-weight: 400\">Hardcoding keys or secrets in code.<\/span><span style=\"font-weight: 400\">\n<p><\/span><\/li>\n<li><span style=\"font-weight: 400\">Leaking crypto errors.<\/span><span style=\"font-weight: 400\">\n<p><\/span><\/li>\n<li><span style=\"font-weight: 400\">Not checking ciphertext length or format before decrypting (to avoid padding oracle attacks).<\/span><span style=\"font-weight: 400\">\n<p><\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400\">Failing to rotate keys over time or upon compromise.<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><\/p>\n<\/div><\/li><\/ul><\/div><\/div><\/article><\/section>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">9<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span> Web applications have access to massive amounts of data belonging to individuals, organizations, and governments. The more data that is accessed, the greater the risk to data security. Cryptography and encryption techniques are used by software developers to protect sensitive data from malicious parties. Cryptography is used to protect data stored in a database or [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1509,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[288],"tags":[278,354,356,355,265,250,6],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to perform encryption and decryption of messages using Crypto in Node.js? - The Workfall Blog<\/title>\n<meta name=\"description\" content=\"For data encryption &amp; decryption, NodeJS includes a built-in library called crypto as the goal is to increase security.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-perform-encryption-and-decryption-of-messages-using-crypto-in-node-js\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to perform encryption and decryption of messages using Crypto in Node.js? - The Workfall Blog\" \/>\n<meta property=\"og:description\" content=\"For data encryption &amp; decryption, NodeJS includes a built-in library called crypto as the goal is to increase security.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-perform-encryption-and-decryption-of-messages-using-crypto-in-node-js\/\" \/>\n<meta property=\"og:site_name\" content=\"The Workfall Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/facebook.com\/workfall\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-08T11:10:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-09-24T10:49:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.18-141-20-153.plesk.page\/learning\/blog\/wp-content\/uploads\/2022\/11\/Cover-Images_Part2-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@workfall\" \/>\n<meta name=\"twitter:site\" content=\"@workfall\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Workfall\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"15 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/18-141-20-153.plesk.page\/learning\/blog\/#organization\",\"name\":\"Workfall - Hire #Kickass Coders On Demand\",\"url\":\"https:\/\/18-141-20-153.plesk.page\/learning\/blog\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/workfall\/\",\"https:\/\/www.linkedin.com\/company\/workfall\/\",\"https:\/\/facebook.com\/workfall\",\"https:\/\/twitter.com\/workfall\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/18-141-20-153.plesk.page\/learning\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/i1.wp.com\/18.141.20.153\/learning\/blog\/wp-content\/uploads\/2021\/10\/cropped-WF_logo.png?fit=400%2C400\",\"contentUrl\":\"https:\/\/i1.wp.com\/18.141.20.153\/learning\/blog\/wp-content\/uploads\/2021\/10\/cropped-WF_logo.png?fit=400%2C400\",\"width\":400,\"height\":400,\"caption\":\"Workfall - Hire #Kickass Coders On Demand\"},\"image\":{\"@id\":\"https:\/\/18-141-20-153.plesk.page\/learning\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/18-141-20-153.plesk.page\/learning\/blog\/#website\",\"url\":\"https:\/\/18-141-20-153.plesk.page\/learning\/blog\/\",\"name\":\"The Workfall Blog\",\"description\":\"#Tech #Remote #Jobs\",\"publisher\":{\"@id\":\"https:\/\/18-141-20-153.plesk.page\/learning\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/18-141-20-153.plesk.page\/learning\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-perform-encryption-and-decryption-of-messages-using-crypto-in-node-js\/#primaryimage\",\"url\":\"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/11\/Cover-Images_Part2-1.png\",\"contentUrl\":\"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/11\/Cover-Images_Part2-1.png\",\"width\":1200,\"height\":628,\"caption\":\"How to perform encryption and decryption of messages using Crypto in Node.js?\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-perform-encryption-and-decryption-of-messages-using-crypto-in-node-js\/#webpage\",\"url\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-perform-encryption-and-decryption-of-messages-using-crypto-in-node-js\/\",\"name\":\"How to perform encryption and decryption of messages using Crypto in Node.js? - The Workfall Blog\",\"isPartOf\":{\"@id\":\"https:\/\/18-141-20-153.plesk.page\/learning\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-perform-encryption-and-decryption-of-messages-using-crypto-in-node-js\/#primaryimage\"},\"datePublished\":\"2022-11-08T11:10:20+00:00\",\"dateModified\":\"2025-09-24T10:49:27+00:00\",\"description\":\"For data encryption & decryption, NodeJS includes a built-in library called crypto as the goal is to increase security.\",\"breadcrumb\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-perform-encryption-and-decryption-of-messages-using-crypto-in-node-js\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-perform-encryption-and-decryption-of-messages-using-crypto-in-node-js\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-perform-encryption-and-decryption-of-messages-using-crypto-in-node-js\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/18-141-20-153.plesk.page\/learning\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to perform encryption and decryption of messages using Crypto in Node.js?\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-perform-encryption-and-decryption-of-messages-using-crypto-in-node-js\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-perform-encryption-and-decryption-of-messages-using-crypto-in-node-js\/#webpage\"},\"author\":{\"@id\":\"https:\/\/18-141-20-153.plesk.page\/learning\/blog\/#\/schema\/person\/cab8236044692bc5b27606b13167794a\"},\"headline\":\"How to perform encryption and decryption of messages using Crypto in Node.js?\",\"datePublished\":\"2022-11-08T11:10:20+00:00\",\"dateModified\":\"2025-09-24T10:49:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-perform-encryption-and-decryption-of-messages-using-crypto-in-node-js\/#webpage\"},\"wordCount\":1843,\"publisher\":{\"@id\":\"https:\/\/18-141-20-153.plesk.page\/learning\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-perform-encryption-and-decryption-of-messages-using-crypto-in-node-js\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/11\/Cover-Images_Part2-1.png\",\"keywords\":[\"crypto\",\"cryptography\",\"decryption\",\"encryption\",\"node\",\"nodeJS\",\"workfall\"],\"articleSection\":[\"Backend Development\"],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/18-141-20-153.plesk.page\/learning\/blog\/#\/schema\/person\/cab8236044692bc5b27606b13167794a\",\"name\":\"Workfall\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/18-141-20-153.plesk.page\/learning\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/09\/avatar_user_1_1693914404-96x96.png\",\"contentUrl\":\"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/09\/avatar_user_1_1693914404-96x96.png\",\"caption\":\"Workfall\"},\"sameAs\":[\"https:\/\/www.workfall.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to perform encryption and decryption of messages using Crypto in Node.js? - The Workfall Blog","description":"For data encryption & decryption, NodeJS includes a built-in library called crypto as the goal is to increase security.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-perform-encryption-and-decryption-of-messages-using-crypto-in-node-js\/","og_locale":"en_US","og_type":"article","og_title":"How to perform encryption and decryption of messages using Crypto in Node.js? - The Workfall Blog","og_description":"For data encryption & decryption, NodeJS includes a built-in library called crypto as the goal is to increase security.","og_url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-perform-encryption-and-decryption-of-messages-using-crypto-in-node-js\/","og_site_name":"The Workfall Blog","article_publisher":"https:\/\/facebook.com\/workfall","article_published_time":"2022-11-08T11:10:20+00:00","article_modified_time":"2025-09-24T10:49:27+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/www.18-141-20-153.plesk.page\/learning\/blog\/wp-content\/uploads\/2022\/11\/Cover-Images_Part2-1.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_creator":"@workfall","twitter_site":"@workfall","twitter_misc":{"Written by":"Workfall","Est. reading time":"15 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/18-141-20-153.plesk.page\/learning\/blog\/#organization","name":"Workfall - Hire #Kickass Coders On Demand","url":"https:\/\/18-141-20-153.plesk.page\/learning\/blog\/","sameAs":["https:\/\/www.instagram.com\/workfall\/","https:\/\/www.linkedin.com\/company\/workfall\/","https:\/\/facebook.com\/workfall","https:\/\/twitter.com\/workfall"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/18-141-20-153.plesk.page\/learning\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/i1.wp.com\/18.141.20.153\/learning\/blog\/wp-content\/uploads\/2021\/10\/cropped-WF_logo.png?fit=400%2C400","contentUrl":"https:\/\/i1.wp.com\/18.141.20.153\/learning\/blog\/wp-content\/uploads\/2021\/10\/cropped-WF_logo.png?fit=400%2C400","width":400,"height":400,"caption":"Workfall - Hire #Kickass Coders On Demand"},"image":{"@id":"https:\/\/18-141-20-153.plesk.page\/learning\/blog\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/18-141-20-153.plesk.page\/learning\/blog\/#website","url":"https:\/\/18-141-20-153.plesk.page\/learning\/blog\/","name":"The Workfall Blog","description":"#Tech #Remote #Jobs","publisher":{"@id":"https:\/\/18-141-20-153.plesk.page\/learning\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/18-141-20-153.plesk.page\/learning\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-perform-encryption-and-decryption-of-messages-using-crypto-in-node-js\/#primaryimage","url":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/11\/Cover-Images_Part2-1.png","contentUrl":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/11\/Cover-Images_Part2-1.png","width":1200,"height":628,"caption":"How to perform encryption and decryption of messages using Crypto in Node.js?"},{"@type":"WebPage","@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-perform-encryption-and-decryption-of-messages-using-crypto-in-node-js\/#webpage","url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-perform-encryption-and-decryption-of-messages-using-crypto-in-node-js\/","name":"How to perform encryption and decryption of messages using Crypto in Node.js? - The Workfall Blog","isPartOf":{"@id":"https:\/\/18-141-20-153.plesk.page\/learning\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-perform-encryption-and-decryption-of-messages-using-crypto-in-node-js\/#primaryimage"},"datePublished":"2022-11-08T11:10:20+00:00","dateModified":"2025-09-24T10:49:27+00:00","description":"For data encryption & decryption, NodeJS includes a built-in library called crypto as the goal is to increase security.","breadcrumb":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-perform-encryption-and-decryption-of-messages-using-crypto-in-node-js\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/learning.workfall.com\/learning\/blog\/how-to-perform-encryption-and-decryption-of-messages-using-crypto-in-node-js\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-perform-encryption-and-decryption-of-messages-using-crypto-in-node-js\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/18-141-20-153.plesk.page\/learning\/blog\/"},{"@type":"ListItem","position":2,"name":"How to perform encryption and decryption of messages using Crypto in Node.js?"}]},{"@type":"Article","@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-perform-encryption-and-decryption-of-messages-using-crypto-in-node-js\/#article","isPartOf":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-perform-encryption-and-decryption-of-messages-using-crypto-in-node-js\/#webpage"},"author":{"@id":"https:\/\/18-141-20-153.plesk.page\/learning\/blog\/#\/schema\/person\/cab8236044692bc5b27606b13167794a"},"headline":"How to perform encryption and decryption of messages using Crypto in Node.js?","datePublished":"2022-11-08T11:10:20+00:00","dateModified":"2025-09-24T10:49:27+00:00","mainEntityOfPage":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-perform-encryption-and-decryption-of-messages-using-crypto-in-node-js\/#webpage"},"wordCount":1843,"publisher":{"@id":"https:\/\/18-141-20-153.plesk.page\/learning\/blog\/#organization"},"image":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-perform-encryption-and-decryption-of-messages-using-crypto-in-node-js\/#primaryimage"},"thumbnailUrl":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/11\/Cover-Images_Part2-1.png","keywords":["crypto","cryptography","decryption","encryption","node","nodeJS","workfall"],"articleSection":["Backend Development"],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/18-141-20-153.plesk.page\/learning\/blog\/#\/schema\/person\/cab8236044692bc5b27606b13167794a","name":"Workfall","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/18-141-20-153.plesk.page\/learning\/blog\/#\/schema\/person\/image\/","url":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/09\/avatar_user_1_1693914404-96x96.png","contentUrl":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/09\/avatar_user_1_1693914404-96x96.png","caption":"Workfall"},"sameAs":["https:\/\/www.workfall.com"]}]}},"jetpack_featured_media_url":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/11\/Cover-Images_Part2-1.png","jetpack-related-posts":[{"id":1545,"url":"https:\/\/learning.workfall.com\/learning\/blog\/roadmap-to-become-a-blockchain-developer-in-2023\/","url_meta":{"origin":1508,"position":0},"title":"Roadmap to Become a Blockchain Developer in 2023","date":"January 10, 2023","format":false,"excerpt":"Blockchain technology has emerged as a key component of Industry 4.0. It is no longer limited to cryptocurrencies and digital payments. The blockchain technology market was valued at USD 5.7 billion in 2021 and is expected to exceed USD 1.59 trillion by 2030, with a CAGR of 87.1% from 2022\u2026","rel":"","context":"In &quot;Data Engineering&quot;","img":{"alt_text":"Roadmap to Become a Blockchain Developer in 2023","src":"https:\/\/i0.wp.com\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/01\/Cover-Images_Part2-1-1.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1116,"url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-easily-analyze-data-and-run-different-queries-using-amazon-redshift-serverless-without-having-to-manage-the-infrastructure\/","url_meta":{"origin":1508,"position":1},"title":"How to easily analyze data and run different queries using Amazon Redshift Serverless without having to manage the infrastructure?","date":"August 3, 2022","format":false,"excerpt":"To know about what\u2019s new in Amazon Redshift Serverless, refer to Part 1 of the blog here: What\u2019s new in Amazon Redshift \u2013 Serverless? Hands-on In this hands-on, we will see how we can analyze data and run the different queries using Amazon Redshift Serverless without the need of managing\u2026","rel":"","context":"In &quot;AWS Cloud Computing&quot;","img":{"alt_text":"Analyze data and run different queries using Amazon Redshift Serverless","src":"https:\/\/i1.wp.com\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/08\/Cover-Images_Part2-1-1.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1525,"url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-configure-cors-in-node-js-with-express\/","url_meta":{"origin":1508,"position":2},"title":"How to Configure CORS in Node.js With Express?","date":"December 6, 2022","format":false,"excerpt":"Internet browsers typically deny access to unknown websites from your application programming interfaces and services. Doing this allows your server to share its resources only with clients that are on the same domain as yours and nobody else. However, there are times when you would like to relax this guard\u2026","rel":"","context":"In &quot;Backend Development&quot;","img":{"alt_text":"How to Configure CORS in Node.js With Express?","src":"https:\/\/i0.wp.com\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/12\/Cover-Images_Part2-1.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1559,"url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-use-ngrx-store-in-an-angular-15-application\/","url_meta":{"origin":1508,"position":3},"title":"How to Use NgRx Store in an Angular 15 Application?","date":"January 24, 2023","format":false,"excerpt":"With reference to the previous blog on state management with React and Redux, we will look at state management in an Angular 15 application using the NgRx store in this blog. NgRx is derived from Ng(the conventional name for Angular tools and ecosystem) and Rx(Reactive Extensions). Moreover, for anyone who\u2026","rel":"","context":"In &quot;Frontend Development&quot;","img":{"alt_text":"NgRx Store in an Angular 15 Application","src":"https:\/\/i0.wp.com\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/01\/Cover-Images_Part2-1-3.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":626,"url":"https:\/\/learning.workfall.com\/learning\/blog\/orchestrate-queue-based-microservices-with-aws-step-functions-and-amazon-sqspart1\/","url_meta":{"origin":1508,"position":4},"title":"How to orchestrate Queue-based Microservices with AWS Step Functions and Amazon SQS (Part 1)?","date":"November 11, 2021","format":false,"excerpt":"Assume that you are developing a distributed application and looking for a solution to transmit a large volume of data, at any level of throughput, without losing messages or requiring other services to be available, you can think about Amazon SQS! Using Amazon SQS you can decouple application components so\u2026","rel":"","context":"In &quot;AWS Cloud Computing&quot;","img":{"alt_text":"Orchestrate Queue based Microservices - Amazon SQS","src":"https:\/\/i2.wp.com\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2021\/11\/CoverImages_1200x628px-3.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":542,"url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-create-on-demand-backups-and-restore-the-backup-for-amazon-rds-using-aws-backuppart-1\/","url_meta":{"origin":1508,"position":5},"title":"How to create on-demand backups and restore the backup for Amazon RDS using AWS Backup(Part 1)?","date":"November 10, 2021","format":false,"excerpt":"When we are using various data services like RDS, EBS, EFS, and DynamoDB to store data in the AWS Cloud, we also have to use some kind of backup solution to meet the data retention requirements. We need a centralized and effective solution for scheduling events like CloudWatch events, cleaning\u2026","rel":"","context":"In &quot;AWS Cloud Computing&quot;","img":{"alt_text":"AWS Backup","src":"https:\/\/i0.wp.com\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2021\/11\/backup1.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/posts\/1508"}],"collection":[{"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/comments?post=1508"}],"version-history":[{"count":5,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/posts\/1508\/revisions"}],"predecessor-version":[{"id":2955,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/posts\/1508\/revisions\/2955"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/media\/1509"}],"wp:attachment":[{"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/media?parent=1508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/categories?post=1508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/tags?post=1508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}