I’m learning how to use openssl and learn more about it.
I wrote a very short line of codes which AES encrypts plaintext and display the output as ciphered text. I had no idea how to display its encrypted plaintext.
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <openssl/err.h>
#include <stdint.h>
int main()
{
unsigned char *buf;
int num;
int rn = RAND_bytes(buf, num);
//if(rn != 1)
//{
// ERR_print_errors(bp);
//}
if(rn == 1)
{
EVP_CIPHER_CTX *ctx;
const EVP_CIPHER *type = EVP_aes_128_cbc();
const unsigned char *iv;
unsigned char *out;
int *outl;
unsigned char *in;
int inl;
iv = buf; // Random numbers from RAND_bytes
const unsigned char *key = (const unsigned char *)
"x00x01x02x03x04x05x06x07"
"x08x09x0ax0bx0cx0dx0ex0f"
"x10x11x12x13x14x15x16x17"
"x18x19x1ax1bx1cx1dx1ex1f";
in = "password"; // Plaintext to be decrypted
ctx = EVP_CIPHER_CTX_new(); // Alocate ctx variable
EVP_EncryptInit(ctx, type, key, iv);
EVP_EncryptUpdate(ctx, out, outl, in, inl);
EVP_EncryptFinal(ctx, out, outl);
const char *ciphertext = out;
uintptr_t ciphertext_len = (uintptr_t)outl; // Convert pointer type variable to non-pointer type variable
// Bio dump
BIO_dump_fp(stdout, out, ciphertext_len);
// Clean up
EVP_CIPHER_CTX_free(ctx);
}
}
I read openssl documentation and from what I’ve understand ‘BIO_dump_fp’ function were used to print out hexadecimal values.
New contributor
user23917954 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.