FlaskDev
FlaskDev

Reputation: 65

Encrypt with AES-128-CBC and Open SSL EVP (Language C)

I'm doing the following procedure to encrypt:

EVP_CIPHER_CTX * ctx;
if (! (ctx = EVP_CIPHER_CTX_new ()))
    handleErrors ();

if (1! = EVP_EncryptInit_ex (ctx, EVP_aes_128_cbc (), NULL, key, InitializationVector))
    handleErrors ();

if (1! = EVP_EncryptUpdate (ctx, new Encryption, & len, unencryptedtext, strlen (unencryptedtext)))
    handleErrors ();
ciphertext_len = len;

if (1! = EVP_EncryptFinal_ex (ctx, newCryption + len, & len))
    handleErrors ();
ciphertext_len + = len;

/ * Clean up * /
EVP_CIPHER_CTX_free (ctx);

Once executed, I get two things, the encrypted string (newCipher) and the length of that string (ciphertext_len).

It is a preview version with merge from various sources, because of ugly names and they follow different formats... 😂😂

Well, the question is that when I want to obtain the encrypted hexadecimal string I do a:

BIO_dump_fp (stdout, newCipher, ciphertext_len);

Obtaining as output: ** 0000 - 12 b0 76 00 74 78 fc 61-09 70 c4 9e 8d f8 d8 47 ..v.tx.a.p ..... G **

And if I do a:

int i;
for (i = 0; i <ciphertext_len; i ++) {
    printf ("% x", newCipher [i]);
}

This appears: ** 12 ffffffb0 76 0 74 78 fffffffc 61 9 70 ffffffc4 ffffff9e ffffff8d fffffff8 ffffffd8 47 **

Suddenly 6 'f' appear in before some octets, but not in others.

QUESTION How can I remove those 'f' and output the hex string how I really want: ** 12b076007478fc610970c49e8df8d847 ** or what other methods can I follow to get it?

Upvotes: 1

Views: 558

Answers (0)

Related Questions