1

I'm trying to make a C program and openssl CLI generate consistent output for AES encryption.

For encryption, I'm using this C and OpenSSL EVP example: https://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption#Encrypting_the_message

And it is invoked as follows:

....
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
....

int main()
{
    char ciphertext[1024];
    static char *enckey = (char *) "7c07f68ea8494b2f8b9fea297119350d78708afa69c1c7600000000000000000";
    static char *iv = (char *)"FEDCBA09876543210000000000000000";
    size_t* output_length;
    
    ssize_t crypto_length = encrypt("test", 4, enckey, iv, ciphertext);
    
    printf("%s", base64_encode(ciphertext, crypto_length, output_length) );
}

For OpenSSL CLI I'm using:

openssl enc -aes-256-cbc -e -a -A -in input.dat \
  -K 7c07f68ea8494b2f8b9fea297119350d78708afa69c1c7600000000000000000 -iv FEDCBA09876543210000000000000000

Identical key, IV, plaintext (the input.dat has just "test" inside (without quotes)), identical mode - AES-256-CBC.

And yet, I get two different outputs:

  • EVP+C: HBy5KT15kp+dLFuBNU15rw==
  • CLI: zcTjiVTkZI8XSpDbc0HvRA== (I get the same with a Java program with the same parameters)

Any idea why these would generate different outputs, and therefore the EVP output can't be decrypted in CLI.

4
  • 1
    I don't see any EVP code, it must be in the encrypt() method. Oct 12, 2020 at 13:15
  • However, you will have to explicitly hex-decode that key and IV in your C code. The CLI does that for you. Oct 12, 2020 at 13:17
  • Thanks, that fixes it.
    – Bozho
    Oct 12, 2020 at 13:25
  • 1
    static char *iv = (char *)"\xFE\xDC\xBA\x09\x87\x65\x43\x21\x00\x00\x00\x00\x00\x00\x00\x00";
    – Bozho
    Oct 12, 2020 at 13:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.