title: openssl c++ 3DES file encryption decryption fail
body: <p>I'm using openssl c lib to encrypt decrypt a file using 3DES, If the file contains a few words, the file will be encrypted and decrypted ok, But if the source plain file contains more words, it decrypted incorrectly. Any suggestions would be kind!</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  #include &lt;openssl/pem.h&gt;
  #include &lt;openssl/conf.h&gt;
  #include &lt;openssl/x509v3.h&gt;
  #include &lt;openssl/pkcs12.h&gt;
  #include &lt;openssl/evp.h&gt;
  #include &lt;openssl/rsa.h&gt;
  #include &lt;openssl/md5.h&gt;
  #include &lt;openssl/rc4.h&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  #define MAX_PATH 512
  
  #define BUFLEN  2048
  void DoEncrypt(const char* srcfile, const char* enc_file)
  {
      char mykey[EVP_MAX_KEY_LENGTH] = "this's my key";
      char iv[EVP_MAX_IV_LENGTH] = "my iv"; 
      char ciphertext[BUFLEN*2] = {0};
      char plaintext[BUFLEN] = {0};
      FILE* fpread = fopen(srcfile, "rb");
      int iReadLen = 0;
      if (NULL == fpread)
      {
          cout&lt;&lt;"do encrypt read src file fail" &lt;&lt;endl;
          return;
      }
      FILE* fpwrite = fopen(enc_file, "w+");
      if (NULL == fpwrite)
      {
          cout&lt;&lt;"enc_file to create fail" &lt;&lt;endl;
          fclose(fpread);
          return;
      }
      const EVP_CIPHER* cipherType = EVP_des_ede3_ecb();
      EVP_CIPHER_CTX ctx;
      EVP_CIPHER_CTX_init(&amp;ctx);
  
      int out_len;
      EVP_EncryptInit(&amp;ctx, cipherType, (const unsigned char*)mykey, (const unsigned char*)iv);
      while ( (iReadLen = fread(plaintext, 1, BUFLEN, fpread)) &gt; 0 )
      {
          memset(ciphertext, 0x00, sizeof(ciphertext));
          EVP_EncryptUpdate(&amp;ctx, (unsigned char*)ciphertext, &amp;out_len, (const unsigned char*)plaintext, iReadLen);
          fwrite(ciphertext, 1, out_len, fpwrite);
          memset(plaintext, 0x00, sizeof(plaintext));
      }
      EVP_EncryptFinal(&amp;ctx, (unsigned char*)ciphertext, &amp;out_len);
      fwrite(ciphertext, 1, out_len, fpwrite);
      EVP_CIPHER_CTX_cleanup(&amp;ctx);
  
      fclose(fpread);
      fclose(fpwrite);
      cout&lt;&lt; "DoEncrypt finish" &lt;&lt;endl;
  }
  void DoDecrypt(const char* enc_file, const char* dec_file)
  {
      char mykey[EVP_MAX_KEY_LENGTH] = "this's my key";
      char iv[EVP_MAX_IV_LENGTH] = "my iv"; 
      char ciphertext[BUFLEN*2] = {0};
      char plaintext[BUFLEN] = {0};
      FILE* fpread = fopen(enc_file, "rb");
      if (NULL == fpread)
      {
          cout&lt;&lt;"DoDecrypt enc file open fail" &lt;&lt;endl;
          return;
      }
      FILE* fpwrite = fopen(dec_file, "w+");
      if (NULL == fpwrite)
      {
          cout&lt;&lt; "DoDecrypt open dec file create fail" &lt;&lt;endl;
          fclose(fpread);
          return;
      }
  
      const EVP_CIPHER* cipherType = EVP_des_ede3_ecb();
      EVP_CIPHER_CTX ctx;
      EVP_CIPHER_CTX_init(&amp;ctx);
      EVP_DecryptInit(&amp;ctx, (const EVP_CIPHER*)cipherType, (const unsigned char*)mykey, (const unsigned char*)iv);
      int iReadLen, out_len;
      while ( (iReadLen = fread(plaintext, 1, BUFLEN, fpread)) &gt; 0 )
      {
          memset(ciphertext, 0x00, sizeof(ciphertext));
          EVP_DecryptUpdate(&amp;ctx, (unsigned char*)ciphertext, &amp;out_len, (const unsigned char*)plaintext, iReadLen);
          fwrite(ciphertext, 1, out_len, fpwrite);
          memset(plaintext, 0x00, sizeof(plaintext));
      }
      EVP_DecryptFinal(&amp;ctx, (unsigned char*)ciphertext, &amp;out_len);
      fwrite(ciphertext, 1, out_len, fpwrite);
      EVP_CIPHER_CTX_cleanup(&amp;ctx);
  
      fclose(fpread);
      fclose(fpwrite);
      cout&lt;&lt; "DoDecrypt finished" &lt;&lt;endl;
  }
  
  int main(void) 
  {
      const char* srcfile = "abc.txt";
      const char* enc_file = "abc.txt.enc";
      const char* dec_file = "abc.txt.dec";
      DoEncrypt(srcfile, enc_file);
      DoDecrypt(enc_file, dec_file);
      return 0;
  }
  </code></pre>