AES Encryption -Key Generation with OpenSSL

后端 未结 2 1537
遇见更好的自我
遇见更好的自我 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 
    
    /* ... */
    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 */
    }
    

提交回复
热议问题