AES Encryption -Key Generation with OpenSSL

As a reference and as a continuation of the publication: how to use OpenSSL to decrypt AES data with Java encryption?

I have the following questions.

I use OpenSSL libraries and C programming to encrypt data in aes-cbc-128. I have been given any input binary data, and I have to encrypt it.

I find out that Java has a CipherParameters interface for setting IV and KeyParameters.

Is there a way to generate IV and key using openSSL? In short, how can you use C to call a random openSSL generator for these purposes. Can any of you provide some documents / examples / links to this?

thanks

+6
source share
2 answers

The AES and IV key for symmetric encryption is just bundles of random bytes. Therefore, any cryptographically strong random number generator will do the trick. OpenSSL provides such a random number generator (which itself is powered by what the operating system provides, such as CryptGenRandom() on Windows or /dev/random and /dev/urandom on Linux). Function RAND_bytes() . Thus, the code will look like this:

 #include <openssl/rand.h> /* ... */ unsigned char key[16], iv[16]; if (!RAND_bytes(key, sizeof key)) { /* OpenSSL reports a failure, act accordingly */ } if (!RAND_bytes(iv, sizeof iv)) { /* OpenSSL reports a failure, act accordingly */ } 
+12
source

Assuming AES-128:

 unsigned char key[16]; RAND_bytes(key, sizeof(key)); unsigned char iv[16]; RAND_bytes(iv, sizeof(iv)); 

A random generator must be seeded before using one of those .

+2
source

Source: https://habr.com/ru/post/885406/


All Articles