AES Encryption -Key Generation with OpenSSL

后端 未结 2 1536
遇见更好的自我
遇见更好的自我 2020-12-16 04:30

As a reference and as continuation to the post: how to use OpenSSL to decrypt Java AES-encrypted data?

I have the following questions.

I am using OpenSSL lib

相关标签:
2条回答
  • 2020-12-16 05:09

    An AES key, and an IV for symmetric encryption, are just bunchs of random bytes. So any cryptographically strong random number generator will do the trick. OpenSSL provides such a random number generator (which itself feeds on whatever the operating system provides, e.g. CryptGenRandom() on Windows or /dev/random and /dev/urandom on Linux). The function is RAND_bytes(). So the code would 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 */
    }
    
    0 讨论(0)
  • 2020-12-16 05:13

    Assuming AES-128:

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

    The random generator needs to be seeded before using one of those.

    0 讨论(0)
提交回复
热议问题