I am generating a key & iv with Ruby's OpenSSL wrapper for an AES CBC 256 setup:
cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
key = cipher.random_key
iv = cipher.random_iv
I am then storing the generated key / iv in blob columns in the database. When trying to display the key or iv it looks something similar to this:
×>×=ñ±8i·ÊahJ[ }öúÇ{xÎÝ¿
My question is:
should the key & iv which seem to be random bytes be converted to hex with an SHA function before using and storing into the DB?
Is it safer this way for storage / use or can they be used just as they are generated in byte form?
UPDATE:
Looks like I was mistaken on my terminology and use of SHA.
Revised question: Is a random byte key from OpenSSL just as random and secure if I convert it to hex via unpack('H*')
Any help is appreciated.
Thanks!
unpack('H*')
converts hex to bytes, not the other way around as you're describing… you needpack
to to that, (2) depending on how you implement/use it, the hex version will not be as secure as the byte version, and (3) you should not be storing keys and encrypted data next to each other… in fact, if you do that, you could as well simply store it plaintext to spare an attacker the effort of copy-and-pasting the keys. $\endgroup$ – e-sushi Mar 19 '14 at 21:18pack()
andunpack()
. ;) $\endgroup$ – e-sushi Mar 30 '14 at 18:27