0

I was looking at mcrypt documentation and one of the examples was the following:

$key = pack('H*', "bcb04b7e103a0cd8b54763051cef08bc55abe029fdebae5e1d417e2ffb2a00a3");

$key_size =  strlen($key);
echo "Key size: " . $key_size . "\n";

Given I have to use either 16, 24 or 32 byte keys for AES-128, 192 and 256 respectively, is it a good practice using a 32-byte key for 128-bit encryption? If not, why?


Second question is related to that of using a random key each time. Would it be possible for something like that to actually work out since afterwards I wouln't know which key was used to encrypt the string hence making it impossible to decrypt?

I just want to make it secure and am not quite sure whether key randomness is a possible approach.

dwvaxaz
  • 49
  • 1
  • 9

1 Answers1

0

Usually when people say 128 bit encryption then the 128 bit is the key size. Since a 32 byte key is a 256 bits key, it is not possible to use this for 128 bit encryption. So it's not good/bad practice, it's impossible. Note that the 128 in MCRYPT_RIJNDAEL_128 designates the block size and not the key size. Specifying MCRYPT_RIJNDAEL_128 is identical to just specifying AES (with any key size).

Keys should be indistinguishable from random, so it is certainly a good idea to generate keys randomly. Of course, if you do this you should store the key. Keys can be stored in smart cards, on USB keys or by encryption with a password or other key. It is also possible that the key is derived from another key, password or key agreement algorithm (such as Diffie-Hellman). The creation, storage and destruction of keys is calling key management and is the topic of whole books - it cannot be described in just one paragraph or answer.

If you don't store a newly generated random key, then yes, you lose the data you encrypted with it.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263