0

I am using aes256 with php to encrypt data.
In the various documents I see various ways to generate a key, Like:

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

Or

$Key = "what ever, plain string";

Or

$Key = "123456789abcdef";//128bit

What is the point of the first example, as opposed to the others? Why not simply use a random string, 128 or 256 long?

I am using the example here http://php.net/manual/en/function.mcrypt-encrypt.php with each of the different key generating methods above.

thor
  • 21,418
  • 31
  • 87
  • 173
Itay Moav -Malimovka
  • 52,579
  • 61
  • 190
  • 278
  • Well, that's similar to another question asked earlier today: [Creating My Symmetric Key in C#](http://stackoverflow.com/questions/30657323/creating-my-symmetric-key-in-c-sharp) – Artjom B. Jun 05 '15 at 17:01
  • My system is not that high level of security, I thought about creating a phrase and using this as a key. Should I pack the phrase? – Itay Moav -Malimovka Jun 05 '15 at 17:10
  • If your question is specific to mcrypt (mcrypt is a little broken) and you're only asking about those three methods, please show how that key is applied. Also, which PHP version are you using? – Artjom B. Jun 05 '15 at 17:14
  • I updated the question with the info u asked about – Itay Moav -Malimovka Jun 05 '15 at 17:19
  • 1
    http://crypto.stackexchange.com/questions/3615/what-is-the-effect-of-the-different-aes-key-lengths – bishop Jun 05 '15 at 17:28
  • I understand the effect of length, What I do not understand is why I would use `pack` vs just a string. – Itay Moav -Malimovka Jun 05 '15 at 18:27
  • One good thing wIth pack (or hex2bin) is you can see the raw hex data. You can safely represent all 8 bits of each byte with 2 hex characters so it is copy and paste friendly. – Phil Jun 05 '15 at 23:19
  • I would like to [caution you not to use mcrypt](https://paragonie.com/blog/2015/05/if-you-re-typing-word-mcrypt-into-your-code-you-re-doing-it-wrong). Even moreso I would like to encourage [learning the ins and outs of cryptography engineering](https://stackoverflow.com/a/30189841/2224584) before implementing homemade cryptography in a production environment. – Scott Arciszewski Jun 08 '15 at 15:58

1 Answers1

0

You have three different key lengths. AES is specified for the following three key lengths: 128-bit (16 byte), 192-bit (24 byte) and 256-bit (32 byte). I'm not going to go into detail about the strength of different key sizes.

Let's take them apart:

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

This is a hex encoded which is 64 characters long in encoded form. The key itself will be 32 bytes long which means that when the key is passed to mcrypt_encrypt() AES-256 is used automatically.


$Key = "what ever, plain string";

This is a 23 character string which can be used as a key for PHP versions before 5.6.0. This is not a valid length for a key in AES. MCrypt will pad the key with \0 up to the next valid key size which is 24 byte for AES-192. So this key is actually a valid key for PHP 5.6 in this form:

$Key = "what ever, plain string\0";

$Key = "123456789abcdef"; //128bit

This is a 15 character "key". As with the previous example, it will be padded to reach 16 bytes so that AES-128 is used.

Generating a key

Since you're asking about key generation, this question contains some approaches. Keys should be random and consist of all possible bytes. Using keys that are only alphanumeric or only contain printable characters is not good if you want to be safe against brute-force attacks on your key.

Since it's not possible to directly hard-code arbitrary bytes as a key in a code file, you should use the first approach of hard-coding an encoded version of the key and decode it programmatically.

Using hard-coded keys

There are only a handful of scenarios where hard-coding a symmetric key in the code can be used:

  • testing cryptographic implemetations (during development)
  • encryption data at rest where the data is not on the same machine as the encryption key (otherwise, it's just data obfuscation)

If your scenario doesn't match to the above, you're either happy with obfuscation or you should think about how you can employ public-key-encryption with a hybrid encryption approach.

Community
  • 1
  • 1
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • 1
    You can also, if you need to hard-code one in a script, store it like this instead of passing it to `pack()` or `hex2bin()`: `$key = "\xbc\xb0\x4b\x7e\x10\x3a\x0c\xd8\xb5\x47\x63\x05\x1c\xef\x08\xbc\x55\xab\xe0\x29\xfd\xeb\xae\x5e\x1d\x41\x7e\x2f\xfb\x2av00\xa3";` – Scott Arciszewski Jun 08 '15 at 15:55