title: How to encrypt data using AES(openssl)?
body: <p>I need to encrypt my data,so i encrypt them using AES. And I can encrypt short data.But I need to encrypt long data, it can't work.What can I do to fix this problem.This is my code.</p>
  
  <pre><code>#include "cooloi_aes.h"
  CooloiAES::CooloiAES()
    : MSG_LEN(0)
  {
    for(int i = 0; i &lt; AES_BLOCK_SIZE; i++)
    {
       key[i] = 32 + i;
    }
  }
  
  CooloiAES::~CooloiAES()
  {
  
  }
  
  std::string CooloiAES::aes_encrypt(std::string msg)
  {
       int i = msg.size() / 1024;
       MSG_LEN = ( i + 1 ) * 1024;
  
  char in[MSG_LEN];
  char out[MSG_LEN];
  memset((char*)in,0,MSG_LEN);
  memset((char*)out,0,MSG_LEN);
  
  strncpy((char*)in,msg.c_str(),msg.size());
  
  unsigned char iv[AES_BLOCK_SIZE];
  for(int j = 0; j &lt; AES_BLOCK_SIZE; ++j)
  {
      iv[j] = 0;
  }
  
  AES_KEY aes;
  if(AES_set_encrypt_key((unsigned char*)key, 128, &amp;aes) &lt; 0)
  {
      return NULL;
  }
  int len = msg.size();
  
  AES_cbc_encrypt((unsigned char*)in,(unsigned char*)out,len,&amp;aes,iv,AES_ENCRYPT);
  
  std::string encrypt_msg(&amp;out[0],&amp;out[MSG_LEN+16]);
  std::cout &lt;&lt; std::endl;
  return encrypt_msg;
  }
  
  std::string CooloiAES::aes_decrypt(std::string msg)
  {
     MSG_LEN = msg.size();
  
     char in[MSG_LEN];
  char out[MSG_LEN+16];
  memset((char*)in,0,MSG_LEN);
  memset((char*)out,0,MSG_LEN+16);
  
  strncpy((char*)in,msg.c_str(),msg.size());
  
  std::cout &lt;&lt; std::endl;
  
  unsigned char iv[AES_BLOCK_SIZE];
  for(int j = 0; j &lt; AES_BLOCK_SIZE; ++j)
  {
      iv[j] = 0;
  }
  
  AES_KEY aes;
  if(AES_set_decrypt_key((unsigned char*)key, 128, &amp;aes) &lt; 0)
  {
      return NULL;
  }
  int len = msg.size();
  AES_cbc_encrypt((unsigned char*)in,(unsigned char*)out,len,&amp;aes,iv,AES_DECRYPT);
  std::string decrypt_msg = out;
  return decrypt_msg;
  }
  </code></pre>
  
  <p>When i encrypt data which has 96 byte, it will failed.I get this error "terminate called after throwing an instance of 'std::length_error'
    what():  basic_string::_S_create
  ".But I don't think this string is longer than max length.And I don't where is wrong.</p>