How to securely generate an IV for AES CBC Encryption?

后端 未结 3 504
自闭症患者
自闭症患者 2021-01-11 16:39

I work on some crypto stuff.

  • I use AES 256 with CBC mode
  • I use OPENSSL

I am aware of the following things (source = wikipedia):

3条回答
  •  日久生厌
    2021-01-11 17:00

    Use openssl_random_pseudo_bytes (most preferably with the second parameter set to an existing variable, which you should then test that it was set to TRUE). This will generate IVs with appropriate randomness characteristics.

    $wasItSecure = false;
    $iv = openssl_random_pseudo_bytes(16, $wasItSecure);
    if ($wasItSecure) {
        // We're good to go!
    } else {
        // Insecure result. Fail closed, do not proceed.
    }
    

    Alternatively, PHP 7 offers random_bytes() which is much simpler.

提交回复
热议问题