title: openssl command encrypt data does not match EVP_aes_128_cbc
body: <p>I tried the following C implementation of Openssl EVP function for AES-128-CBC encryption but the results I am getting are incorrect compared to the command line OpenSSL result.</p>
  <p>I referenced the code on the site below.</p>
  <p><a href="https://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption" rel="nofollow noreferrer">https://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption</a></p>
  <p>C code implementation of AES-128-CBC:</p>
  <pre><code>#include &lt;openssl/conf.h&gt;
  #include &lt;openssl/evp.h&gt;
  #include &lt;openssl/err.h&gt;
  #include &lt;string.h&gt;
  
  void handleErrors(void)
  {
      ERR_print_errors_fp(stderr);
      abort();
  }
  
  int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
              unsigned char *iv, unsigned char *ciphertext)
  {
      EVP_CIPHER_CTX *ctx;
  
      int len;
      int ciphertext_len;
  
      if(!(ctx = EVP_CIPHER_CTX_new()))
          handleErrors();
  
      if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv))
          handleErrors();
  
      if(1 != EVP_EncryptUpdate(ctx, ciphertext, &amp;len, plaintext, plaintext_len))
          handleErrors();
      ciphertext_len = len;
  
      if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &amp;len))
          handleErrors();
      ciphertext_len += len;
  
      /* Clean up */
      EVP_CIPHER_CTX_free(ctx);
  
      return ciphertext_len;
  }
  
  int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
              unsigned char *iv, unsigned char *plaintext)
  {
      EVP_CIPHER_CTX *ctx;
  
      int len;
      int plaintext_len;
  
      /* Create and initialise the context */
      if(!(ctx = EVP_CIPHER_CTX_new()))
          handleErrors();
  
      if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv))
          handleErrors();
  
      if(1 != EVP_DecryptUpdate(ctx, plaintext, &amp;len, ciphertext, ciphertext_len))
          handleErrors();
      plaintext_len = len;
  
      if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &amp;len))
          handleErrors();
      plaintext_len += len;
  
      /* Clean up */
      EVP_CIPHER_CTX_free(ctx);
  
      return plaintext_len;
  }
  
  int main (void)
  {
      /* A 128 bit key */
      unsigned char *key = (unsigned char *)&quot;A[u@AD22ZAWm/3=E&quot;;
  
      /* A 128 bit IV */
      unsigned char *iv = (unsigned char *)&quot;0&quot;;
  
      /* Message to be encrypted */
      unsigned char *plaintext =
          (unsigned char *)&quot;The quick brown fox jumps over the lazy dog&quot;;
  
      /*
       * Buffer for ciphertext. Ensure the buffer is long enough for the
       * ciphertext which may be longer than the plaintext, depending on the
       * algorithm and mode.
       */
      unsigned char ciphertext[128];
  
      /* Buffer for the decrypted text */
      unsigned char decryptedtext[128];
  
      int decryptedtext_len, ciphertext_len;
  
      /* Encrypt the plaintext */
      ciphertext_len = encrypt (plaintext, strlen ((char *)plaintext), key, iv,
                                ciphertext);
  
      /* Do something useful with the ciphertext here */
      printf(&quot;Ciphertext is:\n&quot;);
      BIO_dump_fp (stdout, (const char *)ciphertext, ciphertext_len);
  
      /* Decrypt the ciphertext */
      decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv,
                                  decryptedtext);
  
      /* Add a NULL terminator. We are expecting printable text */
      decryptedtext[decryptedtext_len] = '\0';
  
      /* Show the decrypted text */
      printf(&quot;Decrypted text is:\n&quot;);
      printf(&quot;%s\n&quot;, decryptedtext);
  
  
      return 0;
  }
  </code></pre>
  <p>Output:</p>
  <pre><code>Ciphertext is:
  0000 - 4f 21 68 b5 e9 90 b8 82-71 f2 d2 cd 60 2e fd f1   O!h.....q...`...
  0010 - 48 10 d0 e4 f2 58 86 96-22 fa a5 d2 b6 9a 66 87   H....X..&quot;.....f.
  0020 - e8 e6 bd 9e 73 ad 1e 0e-ec 0a f0 8b 11 83 9d 04   ....s...........
  Decrypted text is:
  The quick brown fox jumps over the lazy dog
  </code></pre>
  <p>OpenSSL command line:</p>
  <pre><code>echo &quot;The quick brown fox jumps over the lazy dog&quot; | openssl enc -e -aes-128-cbc -K 415b7540414432325a41576d2f333d45 -iv 30000000000000000000000000000000 | xxd
  
  Updated WRT suggestion : 
  echo -n &quot;The quick brown fox jumps over the lazy dog&quot; | openssl enc -e -aes-128-cbc -K 415b7540414432325a41576d2f333d45 -iv 30 | xxd
  
  </code></pre>
  <p>OpenSSL Output:</p>
  <pre><code>00000000: 29d1 cf44 9246 fd7c 48a9 2261 afc1 6b34  )..D.F.|H.&quot;a..k4
  00000010: ca5c c44d eddd 27a0 d64d 9679 81e4 fcb1  .\.M..'..M.y....
  00000020: 9f10 0e47 ef73 37fa 5aa8 1a85 ec70 05c6  ...G.s7.Z....p..
  
  
  </code></pre>
  <p>Why are the output results different ( key and iv are both in hex) ?
  I understand that the usage of iv &quot;0&quot; isnt recommended.</p>
  <p>But if the iv is &quot;0&quot; what should be the hex value considered to match the encryption in command line.</p>
  <p>PS : changing IV in code isnt an option for me :(</p>