{"id":3526,"date":"2022-12-16T12:43:24","date_gmt":"2022-12-16T12:43:24","guid":{"rendered":"https:\/\/metaschool.so\/articles\/?p=3526"},"modified":"2023-08-31T10:26:00","modified_gmt":"2023-08-31T10:26:00","slug":"createdecipheriv-method-node-js-crypto","status":"publish","type":"post","link":"https:\/\/metaschool.so\/articles\/createdecipheriv-method-node-js-crypto\/","title":{"rendered":"What is the createDecipheriv() method in Node.js crypto?"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_56_1 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/metaschool.so\/articles\/createdecipheriv-method-node-js-crypto\/#The_createDecipheriv_method_in_Nodejs_crypto_takes_three_arguments\" title=\"The createDecipheriv() method in Node.js crypto takes three arguments\">The createDecipheriv() method in Node.js crypto takes three arguments<\/a><ul class='ez-toc-list-level-3'><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/metaschool.so\/articles\/createdecipheriv-method-node-js-crypto\/#1_algorithm\" title=\"1) algorithm\">1) algorithm<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/metaschool.so\/articles\/createdecipheriv-method-node-js-crypto\/#2_key\" title=\"2) key\">2) key<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/metaschool.so\/articles\/createdecipheriv-method-node-js-crypto\/#3_iv\" title=\"3) iv\">3) iv<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/metaschool.so\/articles\/createdecipheriv-method-node-js-crypto\/#Here_is_an_example_of_how_to_use_the_createDecipheriv_method_to_decrypt_some_data\" title=\"Here is an example of how to use the createDecipheriv() method to decrypt some data\">Here is an example of how to use the createDecipheriv() method to decrypt some data<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n<p>The <code>crypto.createDecipheriv()<\/code> method is a method in the Node js crypto module that creates a <code>Decipher<\/code> object that can be used to decrypt data that has been encrypted using a specific cipher algorithm and initialization vector (IV). It is used for the decryption of data that was encrypted using an encryption algorithm and a symmetric key. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"the-createdecipheriv-method-in-node-js-crypto-takes-three-arguments\"><span class=\"ez-toc-section\" id=\"The_createDecipheriv_method_in_Nodejs_crypto_takes_three_arguments\"><\/span>The <code>createDecipheriv()<\/code> method in Node.js crypto takes three arguments<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-algorithm\"><span class=\"ez-toc-section\" id=\"1_algorithm\"><\/span>1) <code>algorithm<\/code><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>A string that specifies the cipher algorithm to use for decryption. This should be the same algorithm that was used to encrypt the data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-key\"><span class=\"ez-toc-section\" id=\"2_key\"><\/span>2) <code>key<\/code><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>A <code>Buffer<\/code>, <code>TypedArray<\/code>, or <code>DataView<\/code> object that contains the secret key to use for decryption. This should be the same key that was used to encrypt the data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-iv\"><span class=\"ez-toc-section\" id=\"3_iv\"><\/span>3) <code>iv<\/code><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>A <code>Buffer<\/code>, <code>TypedArray<\/code>, or <code>DataView<\/code> object that contains the initialization vector (IV) to use for decryption. This should be the same IV that was used to encrypt the data.<\/p>\n\n\n\n<p>The algorithm and key must be used in the encryption process. The initialization vector is used to randomize the encryption process and the authentication tag is used to verify the authenticity of the data being decrypted. This method returns a Decipher object which can be used to decrypt the data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"here-is-an-example-of-how-to-use-the-createdecipheriv-method-to-decrypt-some-data\"><span class=\"ez-toc-section\" id=\"Here_is_an_example_of_how_to_use_the_createDecipheriv_method_to_decrypt_some_data\"><\/span>Here is an example of how to use the <code>createDecipheriv()<\/code> method to decrypt some data<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\"><code>const crypto = require('crypto');\n\n\/\/ The data to be decrypted, encoded as a base64 string\nconst encryptedData = 'ZW5jcnlwdGVkRGF0YQ==';\n\n\/\/ The secret key used to encrypt the data\nconst key = Buffer.from('mySecretKey', 'utf8');\n\n\/\/ The initialization vector (IV) used to encrypt the data\nconst iv = Buffer.from('myIV', 'utf8');\n\n\/\/ Create a Decipher object using the AES-256-CBC algorithm\nconst decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);\n\n\/\/ Decrypt the data\nconst decrypted = decipher.update(encryptedData, 'base64', 'utf8');\ndecrypted += decipher.final('utf8');\n\nconsole.log(decrypted); \/\/ Output: \"decryptedData\"\n<\/code><\/pre>\n\n\n\n<p>Note that the <code>createDecipheriv()<\/code> method is just one of the ways you can decrypt data in <a href=\"https:\/\/nodejs.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">Node.js<\/a> crypto module. There are other methods, such as <code>createDecipher()<\/code>, that can be used for decryption as well.<\/p>\n\n\n\n<p>Explore <a href=\"https:\/\/metaschool.so\/courses\" target=\"_blank\" rel=\"noreferrer noopener\">web3 learning projects<\/a> by Metaschool.<\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":3815,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[292,17],"tags":[5,305],"class_list":["post-3526","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-answers","category-blockchain","tag-crypto","tag-node-js"],"_links":{"self":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/3526","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/comments?post=3526"}],"version-history":[{"count":8,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/3526\/revisions"}],"predecessor-version":[{"id":6821,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/3526\/revisions\/6821"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media\/3815"}],"wp:attachment":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media?parent=3526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/categories?post=3526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/tags?post=3526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}