title: How to store iv used to create cipher in nodejs's createcipheriv, for future decryption?
body: <p>I'm new to cryptography and I'm using nodejs to serve an app.</p>
  
  <p>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.</p>
  
  <p>I'm using <code>crypto.createCipheriv(algo, key, iv)</code> to encrypt. The algo is: 'aes-256-ctr'. The key is created with a password:</p>
  
  <pre><code>const key=crypto.createHash('sha256').update(String(password)).digest('base64').substr(0,32)
  </code></pre>
  
  <p>The iv is generated with: </p>
  
  <pre><code>const iv = crypto.randomBytes(16)
  </code></pre>
  
  <p>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?</p>
  
  <p>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:</p>
  
  <blockquote>
    <p>Error: Invalid key length</p>
  </blockquote>
  
  <p>even though when I generate and use the iv in the server it encrypts/decrypts without any issue.</p>
  
  <p>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?</p>
  
  <p>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.</p>
  
  <p>Is my way a good one or am I in the wrong direction? I will appreciate is some encryption savvy user can help me... </p>
  
  <p>Thanks </p>