title: Simple AES encryption decryption with openssl library in C
body: <p>I want to encrypt a struct containing few String and then decrypt it. I tried following code. The original code is found from the web and it was working perfectly. I change the input of it to a struct. following is the code.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;time.h&gt;
  #include &lt;openssl/aes.h&gt;
  #include &lt;openssl/rand.h&gt;
  
  typedef struct ticket { /* test field */
  int ticketId;
  char username[20];
      char date[20];
  } USR_TICKET;
  
  // a simple hex-print routine. could be modified to print 16 bytes-per-line
  static void hex_print(const void* pv, size_t len)
  {
  const unsigned char * p = (const unsigned char*)pv;
  if (NULL == pv)
      printf("NULL");
  else
  {
      size_t i = 0;
      for (; i&lt;len;++i)
          printf("%02X ", *p++);
  }
  printf("\n");
  }
  
  // main entrypoint
  int main(int argc, char **argv)
  {
      int keylength;
      printf("Give a key length [only 128 or 192 or 256!]:\n");
      scanf("%d", &amp;keylength);
  
      /* generate a key with a given length */
      unsigned char aes_key[keylength/8];
      memset(aes_key, 0, keylength/8);
      if (!RAND_bytes(aes_key, keylength/8))
          exit(-1);
  
      /* input struct creation */
      size_t inputslength = sizeof(USR_TICKET);
      USR_TICKET ticket;
      ticket.ticketId = 1;
      time_t now = time(NULL);
      strftime(ticket.date, 20, "%Y-%m-%d", localtime(&amp;now));
      strcpy(ticket.username, "ravinda");
  
      printf("Username - %s\n", ticket.username);
      printf("Ticket Id - %d\n", ticket.ticketId);
      printf("Date - %s\n", ticket.date);
  
      /* init vector */
      unsigned char iv_enc[AES_BLOCK_SIZE], iv_dec[AES_BLOCK_SIZE];
      RAND_bytes(iv_enc, AES_BLOCK_SIZE);
      memcpy(iv_dec, iv_enc, AES_BLOCK_SIZE);
  
      // buffers for encryption and decryption
      const size_t encslength = ((inputslength + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
      unsigned char enc_out[encslength];
      unsigned char dec_out[inputslength];
      memset(enc_out, 0, sizeof(enc_out));
      memset(dec_out, 0, sizeof(dec_out));
  
      // so i can do with this aes-cbc-128 aes-cbc-192 aes-cbc-256
      AES_KEY enc_key, dec_key;
      AES_set_encrypt_key(aes_key, keylength, &amp;enc_key);
      AES_cbc_encrypt((unsigned char *)&amp;ticket, enc_out, inputslength, &amp;enc_key, iv_enc, AES_ENCRYPT);
  
      AES_set_decrypt_key(aes_key, keylength, &amp;dec_key);
      AES_cbc_encrypt(enc_out, dec_out, encslength, &amp;dec_key, iv_dec, AES_DECRYPT);
  
      printf("original:\t");
      hex_print((unsigned char *)&amp;ticket, inputslength);
  
      printf("encrypt:\t");
      hex_print(enc_out, sizeof(enc_out));
  
      printf("decrypt:\t");
      hex_print(dec_out, sizeof(dec_out));
  
      USR_TICKET * dyc = (USR_TICKET *)dec_out;
      printf("Username - %s\n", dyc-&gt;username);
      printf("Ticket Id - %d\n", dyc-&gt;ticketId);
      printf("Date - %s\n", dyc-&gt;date);
      return 0;
  }
  </code></pre>
  
  <p>The problem is only first two members of the struct is decrypting correctly. After that data get currupted. What am I doing wrong here?</p>