I'm new to cryptography and I'm using nodejs to serve an app.
My app uses config files that I want to encrypt them before production, and when nodejs server needs these files (in production) it will decrypt them.
I'm using crypto.createCipheriv(algo, key, iv)
to encrypt. The algo is: 'aes-256-ctr'. The key is created with a password:
const key=crypto.createHash('sha256').update(String(password)).digest('base64').substr(0,32)
The iv is generated with:
const iv = crypto.randomBytes(16)
So if I want to decrypt the file, I need the key and the iv. I store the password used to generate the key (hashed), and I match it against user given password, but how I store and retrieve the iv?
When I write the iv (using fs.writefile and the iv const) to file and try to read it, when I try to decrypt I see the error:
Error: Invalid key length
even though when I generate and use the iv in the server it encrypts/decrypts without any issue.
So my question is: how do I store the IV that I used in order to decrypt the files later? How do I use it later when trying to decrypt?
Is there a better way/practice to encrypt configuration files and decrypt them? I want to serve my app with encrypted/hashed personal data, such as configuration data, user/password etc.
Is my way a good one or am I in the wrong direction? I will appreciate is some encryption savvy user can help me...
Thanks