The Crypto CreateCipheriv method in node.js
In node.js there is the crypto.createCipheriv method than can be used to create an return a cipher object for the purpose of encrypting data. It is typically used as a way to better secure web traffic, but it can also be used as a way to encrypt files on your computer as well. In this post I will be coving some examples of using this method to do just this.
1 - What to know before hand
This is a post on the crypto.createCipheriv method in the node.js built in crypto module, as well as some related methods, and other topics. In this post I am also using streams as well. This is not a comprehensive post on the crypto module as a whole, as well as with related skills necessary to get something of value from this post. I also have a post on the createCipher method as well, this post might be a good starting point with the crypto module, but it is best to use the CreateCipheriv method.
2 - A basic File encryption tool example
For an example of the createCipheriv method I made two quick scripts that can be used to encrypt, and decrypt a file using crypto.createCiper, and crypto.createDecipheriv. These will be simple cli scripts than can be used to encrypt, and decrypt these files from the command line.
So this example will contain two files code-file.js, and decode-file.js. As the names suggest one will be used to encrypt a file, and the other to decode it. The code-file.js will generate a key, and iv to be used for the encryption, and the keys will be saved in a jsone file for easy decryption by decode-file.js.
2.1 - My code-file.js file that uses crypto.createCipheriv to encode a file
The code-file.js workw by giving a filename of a file that I want to encrypt, as well as a string that will be used for the key. This file will then use the string that is given to make a key, and generate an iv as well. The key, and iv will then be used to encode the file with the crypto.createCipheriv method.
|
|
2.2 - The decode-file.js file that can be used to decode a file that was encoded with code=file.js
So then there is the decode-file.js file as well that can be used to decode a file back into its original state. This file works by reading the corresponding json file that should exist depending on the filename.
|
|