title: OpenSSL AES 256 CBC via EVP api in C
body: <p><strong>What I am trying to do:</strong>
  Write a program in C that opens a file of arbitrary size and reads its contents. Once The contents are read it will encrypt them in AES 256 CBC and save the ciphertext to a file called ciphertext. Once this is saved it will close both files. Then will open the cipher text from the file that was just saved and decrypt the cipher text and save it to a file called decrypted.</p>
  
  <p><strong>My Problem:</strong>
  It seems to never decrypt my cipher text. I get garbage, I have no idea what I am doing wrong. Please help.</p>
  
  <pre><code>#include &lt;string.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;openssl/evp.h&gt;
  #include &lt;openssl/aes.h&gt;
  
  void encrypt(FILE *ifp, FILE *ofp)
  {
    //Get file size
    fseek(ifp, 0L, SEEK_END);
    int fsize = ftell(ifp);
    //set back to normal
    fseek(ifp, 0L, SEEK_SET);
  
    int outLen1 = 0; int outLen2 = 0;
    unsigned char *indata = malloc(fsize);
    unsigned char *outdata = malloc(fsize*2);
    unsigned char ckey[] =  "thiskeyisverybad";
    unsigned char ivec[] = "dontusethisinput";
  
    //Read File
    fread(indata,sizeof(char),fsize, ifp);//Read Entire File
  
    //Set up encryption
    EVP_CIPHER_CTX ctx;
    EVP_EncryptInit(&amp;ctx,EVP_aes_256_cbc(),ckey,ivec);
    EVP_EncryptUpdate(&amp;ctx,outdata,&amp;outLen1,indata,fsize);
    EVP_EncryptFinal(&amp;ctx,outdata,&amp;outLen2);
    fwrite(outdata,sizeof(char),fsize,ofp);
  }
  
  void decrypt(FILE *ifp, FILE *ofp)
  {
    //Get file size
    fseek(ifp, 0L, SEEK_END);
    int fsize = ftell(ifp);
    //set back to normal
    fseek(ifp, 0L, SEEK_SET);
  
    int outLen1 = 0; int outLen2 = 0;
    unsigned char *indata = malloc(fsize);
    unsigned char *outdata = malloc(fsize*2);
    unsigned char ckey[] =  "thiskeyisverybad";
    unsigned char ivec[] = "dontusethisinput";
  
    //Read File
    fread(indata,sizeof(char),fsize, ifp);//Read Entire File
  
    //setup decryption
    EVP_CIPHER_CTX ctx;
    EVP_DecryptInit(&amp;ctx,EVP_aes_256_cbc(),ckey,ivec);
    EVP_DecryptUpdate(&amp;ctx,outdata,&amp;outLen1,indata,fsize);
    EVP_DecryptFinal(&amp;ctx,outdata,&amp;outLen2);
    fwrite(outdata,sizeof(char),fsize,ofp);
  }
  
  int main(int argc, char *argv[])
  {    
    FILE *fIN, *fOUT;
  
    fIN = fopen("plain.txt", "rb");//File to be encrypted; plain text
    fOUT = fopen("cyphertext.txt", "wb");//File to be written; cipher text    
    encrypt(fIN, fOUT);
  
    fclose(fIN);
    fclose(fOUT);
  
    //Decrypt file now
    fIN = fopen("cyphertext.txt", "rb");//File to be written; cipher text
    fOUT = fopen("decrypted.txt", "wb");//File to be written; cipher text
    decrypt(fIN,fOUT);
  
    fclose(fIN);
    fclose(fOUT);
  
    return 0;
  }
  </code></pre>
  
  <p>Note: there may be some misspellings.
  EDIT: Seems Like I made a mistake with the key and IV, both of which are 128 bit and I am trying to use the 256 bit CBC. This was my problem, seems to work once I changed it to </p>
  
  <pre><code>EVP_aes_128_cbc()
  </code></pre>