title: Encrypt and decrypt string with c++, Openssl and aes
body: <p>I'm trying to encrypt and decrypt a string with c++ openssl and aes cbc.</p>
  
  <p>The weird thing about this is, on one pc it works, and on the other pc it doesn't. There i have only 3/4 of the original string, so the ending is wrong.</p>
  
  <p>The other weird thing is, when i add a dll called "libeay32.dll" to the directory of the exe file on the second pc, it works, but not on the first pc.</p>
  
  <p>In summary, first pc only works without dll, second pc works only with dll.</p>
  
  <p>My question is, can this code be improved, and why this dll is needed on one computer but not on the other.</p>
  
  <p>Here's what i've already written:</p>
  
  <p>KEY and Iv are defines:</p>
  
  <pre><code>#define KEY "abc"
  #define Iv  "abc"
  </code></pre>
  
  <p>Encryption function:</p>
  
  <pre><code>string aes_encrypt(string _InStr)
  {
      string  EncKey,
              EncIV;
  
      AES_KEY enc_key;
  
      unsigned char * aes_key   = (unsigned char *) malloc (sizeof(unsigned char) * (32)),
                    * iv_enc    = (unsigned char *) malloc (sizeof(unsigned char) * AES_BLOCK_SIZE),
                    * aes_input = (unsigned char *) malloc (sizeof(unsigned char) * _InStr.size ()),
                    * enc_out   = (unsigned char *) malloc (sizeof(unsigned char) * ((_InStr.size () + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE);
  
  
  
      memcpy ((char *) aes_input, _InStr.c_str (), _InStr.size ());
  
      memset (aes_key, 0, 32);
  
      EncKey = KEY;
      EncIV  = Iv;
  
      for (int i = 0; i &lt; 50; i++)
          EncKey = md5 (EncKey.c_str ());
  
  
      for (int i = 0; i &lt; 305; i++)
          EncIV = md5 (EncIV.c_str ());
  
      EncIV.erase (16);
  
      memcpy (aes_key, EncKey.c_str (), 32);
      memcpy (iv_enc,  EncIV.c_str  (), 16);
  
  
      AES_set_encrypt_key (aes_key, 128, &amp;enc_key);
      AES_cbc_encrypt     (aes_input, enc_out, _InStr.size (), &amp;enc_key, iv_enc, AES_ENCRYPT);
  
  
      free (aes_key);
      free (aes_input);
      free (iv_enc); 
  
      aes_key     = NULL;
      aes_input   = NULL;
      iv_enc      = NULL;
  
      return string ((char *) enc_out);
  
  }
  </code></pre>
  
  <p>Decryption function:</p>
  
  <pre><code>string aes_decrypt (string _InStr)
  {
      string  EncKey,
              EncIV;
  
      AES_KEY dec_key;
  
      unsigned char * aes_key   = (unsigned char *) malloc (sizeof(unsigned char) * (32)),
                    * iv_dec    = (unsigned char *) malloc (sizeof(unsigned char) * AES_BLOCK_SIZE),
                    * enc_out   = (unsigned char *) malloc (sizeof(unsigned char) * _InStr.size ()),
                    * dec_out   = (unsigned char *) malloc (sizeof(unsigned char) * _InStr.size ());
  
  
      memcpy (enc_out, _InStr.c_str (), _InStr.size ());
  
      memset (aes_key, 0, 32);
  
      EncKey = KEY;
      EncIV  = Iv;
  
      for (int i = 0; i &lt; 50; i++)
          EncKey = md5 (EncKey.c_str ());
  
      for (int i = 0; i &lt; 305; i++)
          EncIV = md5 (EncIV.c_str ());
  
      EncIV.erase (16);
  
      memcpy (aes_key, EncKey.c_str (), 32);
      memcpy (iv_dec,  EncIV.c_str  (), 16);
  
      AES_set_decrypt_key(aes_key, 128, &amp;dec_key);
      AES_cbc_encrypt(enc_out, dec_out, _InStr.size (), &amp;dec_key, iv_dec, AES_DECRYPT);
  
      free (aes_key);
      free (iv_dec); 
      free (enc_out);  
  
      aes_key     = NULL;
      iv_dec      = NULL;
      enc_out     = NULL;
  
      return string ((char *) dec_out);
  
  }
  </code></pre>
  
  <p>Output of first pc:</p>
  
  <p>Input:</p>
  
  <blockquote>
    <p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</p>
  </blockquote>
  
  <p>Encrypted:</p>
  
  <blockquote>
    <p>S^Wo◄┘"â▼~¼\é╣$╨L╡`aC♠·ñZ½h╠∟≥ä°╪╥=αp╙IφoCYN°☺§)↨XwY+☼▀╤M▓÷√NÉk┼≡&lt;ák◄Ä┬÷∙z
    ¼üt@¥≈╟∙¶√Ñù°7å²²²²½½½½½½½½ε■ε■</p>
  </blockquote>
  
  <p>Decrypted:</p>
  
  <blockquote>
    <p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</p>
  </blockquote>
  
  <p>Output of second pc:</p>
  
  <p>Input:</p>
  
  <blockquote>
    <p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</p>
  </blockquote>
  
  <p>Encrypted:</p>
  
  <blockquote>
    <p>S^Wo?+"â?~¼\é¦$ðLÁ`aC?·ñZ½h¦?=ä°ÏÊ=ÓpËIÝoCYN°?§)?XwY+¤¯ÐM¦÷¹NÉk+­&lt;ák?Ä-÷¨zð+BñFb
    YÙ]?s</p>
  </blockquote>
  
  <p>Decrypted:</p>
  
  <blockquote>
    <p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa?WÎ»H+²²²²¦¦¦¦¦¦w$ö?ó:</p>
  </blockquote>