title: Decrypt AES in C++ Example
body: <p>I need some help with decrypt a char array in C++ using AES decrypt with Open SSL library. I already done encryption mode and works fine, but decryption is not working.</p>
  
  <p>This is the Encrypt Function:</p>
  
  <pre><code>string Encrypt(char *Key, char *Msg, int size)
  {
      static char* Res;
      static const char* const lut = "0123456789ABCDEF";
      string output;
      AES_KEY enc_key;
  
      Res = (char *)malloc(size);
  
      AES_set_encrypt_key((unsigned char *)Key, 128, &amp;enc_key);
  
      for(int vuelta = 0; vuelta &lt;= size; vuelta += 16)
      {
          AES_ecb_encrypt((unsigned char *)Msg + vuelta, (unsigned char *)Res + vuelta, &amp;enc_key, AES_ENCRYPT);
      }        
  
      output.reserve(2 * size);
  
      for (size_t i = 0; i &lt; size; ++i)
      {
          const unsigned char c = Res[i];
          output.push_back(lut[c &gt;&gt; 4]);
          output.push_back(lut[c &amp; 15]);
      }
  
      free(Res);
  
      return output;
  }
  </code></pre>
  
  <p>This is the Decrypt Function (not working):</p>
  
  <pre><code>char * Decrypt( char *Key, char *Msg, int size)
  {
      static char* Res;
      AES_KEY dec_key;
  
      Res = ( char * ) malloc( size );
  
      AES_set_decrypt_key(( unsigned char * ) Key, 128, &amp;dec_key);
  
      for(int vuelta= 0; vuelta&lt;=size; vuelta+=16)
      {
          AES_ecb_encrypt(( unsigned char * ) Msg+vuelta, ( unsigned char * ) Res+vuelta, &amp;dec_key, AES_DECRYPT); 
      }
  
      return (Res);
  }
  </code></pre>
  
  <p>This is an Example of the Main function that call the methods, the problem is thar no mather how i print the "Res" variable in the Decrypt function, it always show random ASCII values, and i like to show the result in a string like the Encrypt function:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  
  #include "openSSL/aes.h"
  
  using namespace std;
  
  int main(int argc, char const *argv[])
  {
      char key[16];
      char message[128];
      char enc_message[128];
  
      string s_key = "THIS_IS_THE_KEY_";
      string s_message = "Hello World !!!";
  
      memset(key, 0, sizeof(key));
      strcpy(key, s_key.c_str()); 
  
      memset(message, 0, sizeof(message));
      strcpy(message, s_message.c_str()); 
  
      string response = Encrypt(key, message, sizeof(message));
  
      cout&lt;&lt;"This is the Encrypted Message: "&lt;&lt;response&lt;&lt;endl;
  
      memset(enc_message, 0, sizeof(enc_message));
      strcpy(enc_message, response.c_str());
  
      Decrypt(key, enc_message, sizeof(enc_message));
  
      return 0;
  }
  </code></pre>
  
  <p>Any improve in this methods?</p>