title: C - OpenSSL AES Encryption key issue
body: <p>I'm porting an AES encryption procedure from Javascript to C.</p>
  <p>I'm using <strong>OpenSSL</strong>.</p>
  <p>For the encryption I'm using <strong>ECB 256</strong> mode.</p>
  <p>The encryption goes successfully but the final Base64 <strong>does not correspond</strong> to the result I get in JS.</p>
  <p>By testing with several online tools, the output I was getting from the C version was actually correct but different from the one from JS.</p>
  <p>Then I realized the problem thanks to this tool <a href="https://gchq.github.io/CyberChef" rel="nofollow noreferrer">https://gchq.github.io/CyberChef</a>.</p>
  <p>The key I'm providing is a <strong>MD5 hash</strong> like this:
  aa8744256a89cba0d77f0686916f8b2e</p>
  <p>If I pass the key as an <strong>HEX string</strong> in the CyberChef tool, the output corresponds to the JS version.</p>
  <p>The C version is reading the key as an <strong>UTF string</strong>.</p>
  <p>How can I force it to read the key as an HEX string?</p>
  <p>Here's the code:</p>
  <pre><code>//generating MD5 of key
  char keyHash[33];
  unsigned char digest[MD5_DIGEST_LENGTH];
  MD5((unsigned char*)keyString, strlen(keyString), (unsigned char*)&amp;digest);
  for (int i = 0; i &lt; 16; i++)
      sprintf(&amp;keyHash[i * 2], &quot;%02x&quot;, (unsigned int)digest[i]);
  
  char out[256];
  int outlen = 0;
  //initializing context
  EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  //inizializing encryption with 256 ECB Cipher and MD5 Hash as Key
  EVP_EncryptInit(ctx, EVP_aes_256_ecb(), keyHash, 0);
  //setting padding
  EVP_CIPHER_CTX_set_padding(ctx, EVP_PADDING_PKCS7);
  //encrypting
  EVP_EncryptUpdate(ctx, out, &amp;outlen, user, strlen(user));
  EVP_EncryptFinal(ctx, out, &amp;outlen);
  EVP_CIPHER_CTX_free(ctx);
  
  //parsing the result to Base64
  BIO *mbio = BIO_new(BIO_s_mem());
  BIO *b64bio = BIO_new(BIO_f_base64());
  BIO_set_flags(b64bio, BIO_FLAGS_BASE64_NO_NL);
  BIO *bio = BIO_push(b64bio, mbio);
  
  BIO_write(bio, out, outlen);
  BIO_flush(bio);
  
  char* data = NULL;
  size_t datalen = 0;
  datalen = BIO_get_mem_data(mbio, &amp;data);
  data[datalen] = '\0';
  
  //printing the result
  printf(&quot;encrypted data: [%s]\n&quot;, data);
  </code></pre>