title: File encryption/decryption using AES 256 CBC Mode
body: <p>I am trying to encrypt/decrypt using AES 256 CBC mode and I am stuck on how you can do it with files. It is required that the file be read in blocks of 4096 bytes and encrypt/decrypt and store the result back to a file. Here is my code.</p>
  
  <p>Previously I was able to do it for random hex strings. This program can generate a random key and plain hex and encrypt/decrypt perfectly.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;openssl/aes.h&gt;
  #include &lt;openssl/rand.h&gt;
  
  int main(int argc, char **argv)
  {
      int keylength = 256;
  
      unsigned char *aes_key = (unsigned char*)malloc(sizeof(unsigned char)* (keylength / 8));
      memset(aes_key, 0, keylength / 8);
      if (!RAND_bytes(aes_key, keylength / 8))
          exit(-1);
  
      size_t inputslength = 0;
      printf("Give an input's length:\n");
      scanf("%lu", &amp;inputslength);
  
      unsigned char *aes_input = (unsigned char*)malloc(sizeof(unsigned char)*inputslength);
      memset(aes_input, 'X', inputslength);
  
      unsigned char *iv_enc = (unsigned char*)malloc(sizeof(unsigned char)*AES_BLOCK_SIZE), *iv_dec = (unsigned char*)malloc(sizeof(unsigned char)*AES_BLOCK_SIZE);
      RAND_bytes(iv_enc, AES_BLOCK_SIZE);
      memcpy(iv_dec, iv_enc, AES_BLOCK_SIZE);
  
      const size_t encslength = ((inputslength + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
      unsigned char *enc_out = (unsigned char*)malloc(sizeof(unsigned char)*encslength);
      unsigned char *dec_out = (unsigned char*)malloc(sizeof(unsigned char)*inputslength);
      memset(enc_out, 0, sizeof(enc_out));
      memset(dec_out, 0, sizeof(dec_out));
  
      AES_KEY enc_key, dec_key;
      AES_set_encrypt_key(aes_key, keylength, &amp;enc_key);
      AES_cbc_encrypt(aes_input, enc_out, inputslength, &amp;enc_key, iv_enc, AES_ENCRYPT);
  
      AES_set_decrypt_key(aes_key, keylength, &amp;dec_key);
      AES_cbc_encrypt(enc_out, dec_out, encslength, &amp;dec_key, iv_dec, AES_DECRYPT);
  
      printf("original:\t");
      hex_print(aes_input, inputslength);
  
      printf("encrypt:\t");
      hex_print(enc_out, encslength);
  
      printf("decrypt:\t");
      hex_print(dec_out, inputslength);
  
      // free memory here
  
      return 0;
  }
  </code></pre>
  
  <p>Now I need to pass a file in 4096 byte increments and be able to write the output back to an output file. I have the following</p>
  
  <pre><code>int main(int argc, char *argv[])
  {
      FILE *fp_in, *fp_out;
      int c;
      funct = argv[1];
      /* Check command-line */
      if (argc != 3) {
          printf("EXAMPLE USAGE: ./mycp enc/dec &lt;INFILEE&gt; &lt;OUTFILE&gt;\n");
          return 1;
      }
  
      /* Open files */
      if (!(fp_in = fopen(argv[2], "rb"))) {
          perror("input file open error");
          return 1;
      }
  
      if (!(fp_out = fopen(argv[3], "wb"))) {
          perror("output file open error");
          return 1;
      }
  
      fseek(storeFile, blocksToRead[i] * 4096, SEEK_SET);
      int n = fread(ptr, 4096, 1, fp_in);
      if (funct == "enc")
      {
          enc();          //encrypt
      }
      else if (funct == "dec")
      {
          dec();          //decrypt
      }
  
      if (!(funct = argv[1], "wb"))) {
          perror("output file open error");
          return 1;
      }
      if (fwrite(ptr, n, fp_out) != n)
      {
          printf("write error\n");
          return -1;
      }
      fclose(fp_out);
      fclose(fp_in);
      return 0;
  }
  </code></pre>