I have a PHP function I have written that uses a 64 bit binary string and a random initialization vector to encode data using the following basic principal:

$encrypted = openssl_encrypt($data_to_encrypt, AES_256_CTR, $64bit_binary_string, 0, $iv);

Then in order for me to decrypt the message I have to know the IV so I tie this to the new hash with a colon separator:

$encrypted = $encrypted . ':' . $iv;

Finally, I base64 encode the entire thing for another purpose

I have read that the IV does not need to be secret as long as it is random. If this is true then am I correct in assuming that it is safe to be included at the end of the hash like this? Obviously, base64 is easy to reverse so once that has been done, I assume it would be easy for anyone to guess what the IV is. The problem would be decoding the 64bit string.

  • 1
    Encryption is not hashing and a ciphertext is not a hash. Yes, simply adding the IV to the ciphertext is safe. Note however the ciphertext is and the IV should be random binary data that can easily contain the byte value for ':' so you must not parse the result using the colon, you must parse it by knowing the correct length of the IV and taking that length then removing the colon. If by '64-bit string' you mean 8 bytes, that is not a very good key for AES-128 and using AES-256 is totally wasted, while if you mean 64 characters each '0' or '1' that is super weak. Commented Mar 19, 2018 at 7:54
  • Alternatively and more flexibly you can base64 each and concat those with colon which allows parsing because a base64 encoding never includes a colon. Commented Mar 19, 2018 at 8:00
  • 5
    Could you describe your use case a bit here? What are you trying to accomplish? There seems to be some confusion regardin encoding/encrypting/hashing.
    – Anders
    Commented Mar 19, 2018 at 8:29
  • @dave_thompson_085 thank you for the clarification on the terminology the IV length check makes sense. My IV is randomly generated and my key looks like the following (obviously not the real key) $key = hex2bin('cj90d85t6c1234895a3369be123p061b32q653ad6af381f0d36abde3a3215987'); Commented Mar 20, 2018 at 15:35
  • 1
    You store the hash that is generated by password_hash() in your database and in your login function you validate the entered password against your stored hashes for the user with the function password_verify(). php.net/manual/de/function.password-verify.php
    – VaaChar
    Commented Mar 24, 2018 at 16:27

1 Answer 1


The IV is not secret so it doesn't matter how you store it. It's perfectly find to append it to a hash.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .