title: OpenSSL AES 128 CBC \0 crash enrypted char*
body: <p>I have problem with OpenSSL AES ( I use aes.h ) :</p>
  
  <ol>
  <li><p>get binary file (.pdf , .jpg ) or some .xml , .txt about 5000 chars , then i enrypt base64.</p></li>
  <li><p>When I try enrypt AES i get bad size ( Random 400 , 200 , 50 ) , my AESKey is random 128bits from chars : [abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ,.-@#&amp;*oeOE¯_ ]</p></li>
  </ol>
  
  <p>I think that problem is '\0' enrypted char but i don't know can I extort entry
  (string can save char array with \0 element but unsigned char* and char* is aborted )</p>
  
  <p>this is my code : </p>
  
  <pre><code>std::string PFHelper::ASE_encode(std::string in, wchar_t* KS)
  {
  //const unsigned char* aes_input = reinterpret_cast&lt;const unsigned char *&gt; (in.c_str());
  unsigned char* aes_input = new unsigned char[in.length()];
  strcpy((char*)aes_input, in.c_str());
  std::string KS_string = PFHelper::ConvertFromUtf8ToString(KS);
  unsigned char* aes_key = new unsigned char[16];
  strcpy((char*)aes_key, KS_string.c_str());
  /* Input data to encrypt */
  unsigned char iv[AES_BLOCK_SIZE];
  memset(iv, 0x00, AES_BLOCK_SIZE);
  const size_t encslength = ((in.length() + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
  /* Buffers for Encryption and Decryption */
  unsigned char * enc_out = new unsigned char [encslength];
  //unsigned char * dec_out = new unsigned char[in.length()];
  memset(enc_out, 0, encslength);
  //memset(dec_out, 0, in.length());
  AES_KEY enc_key;
  AES_set_encrypt_key(aes_key, 128, &amp;enc_key);
  AES_cbc_encrypt(aes_input, enc_out, encslength, &amp;enc_key, iv, AES_ENCRYPT);
  
  //AES_KEY decrypt;
  //memset(iv, 0x00, AES_BLOCK_SIZE);
  
  
  //AES_cbc_encrypt((unsigned char*)enc_out, dec_out, encslength, &amp;decrypt, iv, AES_DECRYPT);
  //std::string returned = ConvertFromUnsignedCharToString(enc_out);
  memset(aes_key, 0x00, 16);
  memset(aes_input, 0x00, in.length());
  return ConvertFromUnsignedCharToString(enc_out);
  }
  </code></pre>
  
  <p>Sample value :</p>
  
  <p>KS (AESKey) :  L"F-ZTNW meOJLK1s5"</p>
  
  <p>in(5464chars) : PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVR.....</p>
  
  <p>out(51chars) : "©¦ľ‘Ň·rnoŚ8nžęwřëůl2ěY ßJ2¨ßňO× ohX­‚Ž~ŚČ”E</p>
  
  <p>I try EVP and typical char key and problem is the same </p>
  
  <pre><code>//set back to normal
  unsigned char* aes_input = new unsigned char[in.length()];
  strcpy((char*)aes_input, in.c_str());
  
  unsigned char* dec_out = new unsigned char[in.length()];
  memset(dec_out, 0, in.length());
  dec_out[in.length()] = '\0';
  
  /* A 256 bit key */
  unsigned char *key = (unsigned char *)"01234567890123456789012345678901";
  
  /* A 128 bit IV */
  unsigned char *iv = (unsigned char *)"01234567890123456";
  
  int lenght;
  int c_len = in.length() + AES_BLOCK_SIZE;
  //Set up encryption
  int f_len = 0;
  EVP_CIPHER_CTX *ctx;
  ctx = EVP_CIPHER_CTX_new();
  if (EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv) != 1)
  {
      wcout &lt;&lt; L"1";
  }
  if (EVP_EncryptUpdate(ctx, dec_out, &amp;lenght, aes_input, in.length()) != 1)
  {
      wcout &lt;&lt; L"2";
  }
  if (EVP_EncryptFinal_ex(ctx, dec_out, &amp;lenght) != 1)
  {
      wcout &lt;&lt; L"3";
  }
  return ConvertFromUnsignedCharToString(dec_out);
  }
  </code></pre>