title: How to get cipher text as result of encryption using aes cbc 128 bit with open ssl in c language
body: <p>I am working on a computer security lab. The goal of the lab is to find the key used to encrypt some plain text, given initial vector, plaintext, ciphertext and a list of possible keys. I am currently stuck trying to get the raw cypher text out of the method below. This is my first experience with openssl.</p>
  
  <pre><code>
  void testKey(char * word){
          unsigned char key[128];
          char * out = malloc(1024);
          string2hexString(word, key);
          int successful_encrypt = encrypt(word,out);
  
          if (successful_encrypt){
              printf("Successful Encryption %02x, Word: %s, Key: %s \n\n", out, word,key);
          }}
  
  int encrypt(char * key, char* out){
      unsigned char outbuf[1024];
  
      int outlen, tmplen;
      EVP_CIPHER_CTX ctx;
      char intext[] = "This is a seeed lab.";
      unsigned char iv[] = {'0xaa', '0xbb', '0xcc', '0xdd', '0xee', '0xff', '0x0', '0x99', '0x88', '0x77', '0x66', '0x55', '0x44', '0x33', '0x22', '0x11'};
      EVP_CIPHER_CTX_init(&amp;ctx);
      EVP_EncryptInit_ex(&amp;ctx, EVP_aes_128_cbc(), NULL, key, iv);
  
      if(!EVP_EncryptUpdate(&amp;ctx, outbuf, &amp;outlen, intext, strlen(intext)))
          {
              /* Error */
              return 0;
          }
      if(!EVP_EncryptFinal_ex(&amp;ctx, outbuf + outlen, &amp;tmplen))
          {
              /* Error */
              return 0;
          }
      printf("%s", intext);
      outlen += tmplen;
      printf("\n\n%x\n\n", outbuf);
      EVP_CIPHER_CTX_cleanup(&amp;ctx);
      strcpy(out, outbuf);
      return 1;
  }
  </code></pre>
  
  <p>Basically i am looping through the list of keys and i want to compare the ouput of the encryption with the cipher text that was given to me. Below is the current output. The string after successful encryption is the output of the encryption <strong>that i suspect to be wrong</strong> or not what i am looking for atleast.</p>
  
  <pre><code>
  This is a seeed lab.
  
  bfc95fdc
  
  Successful Encryption a391ef0, Word: Zorn############, Key: 5A6F726E232323232323232323232323 
  
  This is a seeed lab.
  
  bfc95fdc
  
  Successful Encryption a3922f8, Word: Zoroaster#######, Key: 5A6F726F617374657223232323232323 
  
  This is a seeed lab.
  
  bfc95fdc
  
  Successful Encryption a392700, Word: Zoroastrian#####, Key: 5A6F726F6173747269616E2323232323 
  
  This is a seeed lab.
  
  bfc95fdc
  
  Successful Encryption a392b08, Word: zounds##########, Key: 7A6F756E647323232323232323232323 
  
  This is a seeed lab.
  
  bfc95fdc
  
  Successful Encryption a392f10, Word: z's#############, Key: 7A277323232323232323232323232323 
  
  This is a seeed lab.
  
  bfc95fdc
  
  Successful Encryption a393318, Word: zucchini########, Key: 7A75636368696E692323232323232323 
  
  This is a seeed lab.
  
  bfc95fdc
  
  Successful Encryption a393720, Word: Zurich##########, Key: 5A757269636823232323232323232323 
  
  This is a seeed lab.
  
  bfc95fdc
  
  Successful Encryption a393b28, Word: zygote##########, Key: 7A79676F746523232323232323232323
  </code></pre>
  
  <p>The given ciphertext is <code>"8d20e5056a8d24d0462ce74e4904c1b513e10d1df4a2ef2ad4540fae1ca0aaf9"</code> So i need the output of the encryption in some format that makes sense to compare to that string. <strong>How can i get the raw cipher text in openssl aes 128 cbc encryption using the c library?</strong></p>