title: OpenSSL Encryption in Debug and Release
body: <p>I have the following code (modified <code>ik</code> and <code>iv</code>, of course):</p>
  
  <pre><code>#include "openssl/evp.h"
  
  #include &lt;iostream&gt;
  #include &lt;sstream&gt;
  #include &lt;string&gt;
  
  using std::cout;
  using std::endl;
  using std::string;
  using std::ostringstream;
  
  void Encryption(bool encrypt, const string &amp;in, string &amp;out)
  {
      int ik[17] = {  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16 };
      int iv[17] = { 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 };
      ik[16] = iv[16] = '\0';
      unsigned char ckey[17], ivec[17];
      for (int i = 0; i &lt; 16; ++i)
      {
          ckey[i] = ik[i];
          ivec[i] = iv[i];
      }
      EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
      EVP_CipherInit(ctx, EVP_aes_256_cbc(), ckey, ivec, encrypt);
      unsigned blocksize = EVP_CIPHER_CTX_block_size(ctx);
  
      int out_len;
      int n = in.length() + blocksize;
      unsigned char *cipher_buf = new unsigned char[n];
      for (int i = 0; i &lt; n; ++i)
          cipher_buf[i] = ' ';
      EVP_CipherUpdate(ctx, cipher_buf, &amp;out_len,
                       (const unsigned char*)in.c_str(), in.length());
      std::ostringstream ss;
      ss.write((char*)cipher_buf, out_len);
      EVP_CipherFinal(ctx, cipher_buf, &amp;out_len);
      ss.write((char*)cipher_buf, out_len);
      delete cipher_buf;
      out = ss.str();
      EVP_CIPHER_CTX_free(ctx);
  }    
  
  int main()
  {
      string out;
      string in = "TEST";
      string back;
      Encryption(true, in, out);
      cout &lt;&lt; "---------------" &lt;&lt; endl;
      Encryption(false, out, back);
      cout &lt;&lt; "TEST &gt; " &lt;&lt; out &lt;&lt; " &gt; " &lt;&lt; back &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  
  <p>Basically, this code encrypts a string, decrypts it and then prints out the result from both steps.</p>
  
  <p>If I run this code in Debug, it works just fine, outputting <code>TEST &gt; *gibberish A* &gt; TEST</code>. If I run it on Release, however, it fails, printing only <code>TEST &gt; *gibberish B (not A)* &gt;</code>, because <code>back</code> remains empty. Seeing <a href="https://stackoverflow.com/questions/1998684/aes-encryption-problem-in-debug-and-release-mode">this question</a> and another one I can't find now, I made a copy of my Release configuration and went changing one parameter at a time to the equivalent value in the Debug configuration. When I changed <code>Configuration Properties &gt; C/C++ &gt; Code Generation &gt; Basic Runtime Checks</code> from <code>Default</code> to <code>Both (/RTC1, equiv. to /RTCsu) (/RTC1)</code>, this new Release configuration worked, printing exactly the same values as in the Debug configuration. Obviously, by then I'd already changed many other parameters, but it was only when I changed the value of <code>/RTC</code> that any change was clear in the execution.</p>
  
  <p>I also noticed something else. If I removed the <code>cout &lt;&lt; "---------------" &lt;&lt; endl;</code> line in <code>main()</code>, the default Release configuration also works, printing <code>TEST &gt; *gibberish C* &gt; TEST</code>. The encryption gibberish is different to both the Debug result and the Release with the <code>cout</code> line. Also, if I place that <code>cout</code> call anywhere other than between my <code>Encryption</code> calls, it works. </p>
  
  <p>So I have two questions I believe may be interrelated:</p>
  
  <ul>
  <li>Why does the value of <code>/RTC</code> affect the encryption result? Given that <code>/RTC</code> should be disabled in Release builds, is it possible to generate equivalent encrypted data (aka gibberish) in both Debug and Release, so as to make them interchangeable, without losing <code>/RTC</code> in Debug?
  
  <ul>
  <li>Why does the use of <code>cout</code> in between my <code>Encryption()</code> calls modify the result in Release mode and only in Release mode?</li>
  </ul></li>
  </ul>
  
  <p>And, should it be worth mentioning, everything is built with the static runtime library (<code>/MT[d]</code>).</p>