Getting started with Node Crypto CreateCipher method with basic examples and more
In todays post I will be writing about the CreateCipher method in the Nodejs Crypto module. This method and the corresponding createDecipher method is a great starting point when it comes to getting started with encryption using nodejs, however when it comes to making an actual real project it might be best to go with the createCipheriv method as that gives more control over the creation of the key, and iv variable. In addition in late versions of nodejs it would appear that this method is now depreciated in favor of createCipheriv. Still in this post I will be going over some quick examples when it comes to simple encryption using nodejs.
1 - Use the Node Crypto method CreateCipheriv if you aim to make a real project that makes use of encryption in node.js
Depending on your experience with node, the crypto module, and the version of node that you are using. The createCipher method might be a good starting point because of the ease of making a key. The reason why is I just need to give a password and then the createCipher method makes a key and iv value for me based off of that password. However this is also its downfall as well, and the CreateCipheriv method gives a greater deal of control over these values. So in any kind of actual professional software package it would be a better move to go with that method. I have a corresponding post on the CreateCipheriv that might be worth checking out.
2 - The Node Crypto CreateCipher basic example using the update and final methods
The create cipher method can be used by just passing the algorithm that you want to use as the first argument followed by a password. The update and final methods can then be used as a way to create an encrypted string using the given algorithm and password.
2.1 - Now to decipher
When you have an encrypted string, and know the algorithm and password it is possible to decipher the string using the createDecipter method. This method works in more or less the same way as createCipher however the the encodings used must be inverted when updating and concatenating.
3 - Streams and Node Crypto Create Cipher
The CreateCipher method can also be used when working with streams. In this section I will be going over some examples of the crypto module createCipher and createDecipher methods using streams rather than using the update and final methods.
3.1 - createCipher
When calling the createCipher method what is returned can be used as a stream, and with other streams as well. The createCipher method is an example of what can be called a duplex stream that is a kind of combination of a readable and writable stream.
In this example I am using the createCipher method as a stream, and once the stream is completed it results in a resolved promise, or a rejected one if something goes wrong.
3.2 - CreateCipher and CreateDecipher
Because the createDecipher method is so similar when it comes to making some kind of method that wraps them it is possible to make some kind of method that actually uses both methods, and not much needs to change. Just use the right method depending if you want to encrypt or decrypt. On top of that make sure that the algorithm and password are the same, and that the encodings used are appropriate depending if you are encrypting to hex or deciphering to plain old text.