2

Ok so here's the sample code from the page http://phpseclib.sourceforge.net/crypt/examples.html

<?php
include('Crypt/AES.php');
include('Crypt/Random.php');

$cipher = new Crypt_AES(); // could use CRYPT_AES_MODE_CBC
// keys are null-padded to the closest valid size
// longer than the longest key and it's truncated
//$cipher->setKeyLength(128);
$cipher->setKey('abcdefghijklmnop');
// the IV defaults to all-NULLs if not explicitly defined
$cipher->setIV(crypt_random_string($cipher->getBlockLength() >> 3));

$size = 10 * 1024;
$plaintext = str_repeat('a', $size);

echo $cipher->decrypt($cipher->encrypt($plaintext));
?>

Before anything, why is the line //$cipher->setKeyLength(128); is being commented out?

And if I want to set my key to '1234568790', is there anything I should do? Because it's much shorter than they length of the key in the example above (abcdefghijklmnop).

And finally if the plaintext that I want to encrypt is short, something like "my name is james bond", is there anything extra that I should do? Because from the code above, it seems the plaintext's length should be 10 x 1024. (Why is that?)

Community
  • 1
  • 1
imin
  • 4,504
  • 13
  • 56
  • 103
  • Plaintext shouldn't be 10x1024, they just created a string that's 10KB in size (hence 10x1024), for example purposes. The 128bit key means 128 / 8 bytes = 16 bytes, at 2 bytes per character that amounts to 8 chars. 256 bit key = 32 bytes = 16 characters (hence abcdefghijklmnop which is 16 characters). I'm not overly familiar with that particular library that you found, personally - I prefer `openssl` extension over pure php libraries. – Mjh May 09 '16 at 13:35
  • @Mjh thanks for your comment. But the example in the page would display the key as abcdefghijklmnop when I set it to 128bit... that's 16 characters for 128 bit key – imin May 09 '16 at 13:54
  • 1
    Those characters don't have to be used. The key can also be transformed into hex-format (via hashing) and then corresponding amount of bytes can be used. Personally, the keys I use are always randomly created and then hashed - the hashing function used is selected according to the block size of the cipher. Encryption is a complicated beast and it's often not implemented correctly (trained crypto guys will find mistakes in the comment I posted). I'd suggest [this blog](http://www.cryptofails.com/) before you get further into encryption. It's the best to do it right from the start. – Mjh May 09 '16 at 13:58
  • Wow, good read in the link that you gave. Thanks! – imin May 09 '16 at 14:08

1 Answers1

4

Before anything, why is the line //$cipher->setKeyLength(128); is being commented out?

Say you pass a 17 byte key to phpseclib via setKey. What do you suppose ought to happen? In the 1.0 and 2.0 versions if setKeyLength isn't called then the key is null-padded to 24 bytes (eg. 192-bits). But if setKeyLength is called it'll be truncated to 16 bytes. In the master branch (which, as I understand it, will eventually become the 3.0 branch), an exception is thrown if the key length isn't valid. In that version it may still be desirable to call setKeyLength if, for example, if you want to set the key length as being 128-bits in one part of the code and then actually set the key in another part of the code. ie. you could do the length checking or phpseclib could do the length checking.

And if I want to set my key to '1234568790', is there anything I should do? Because it's much shorter than they length of the key in the example above (abcdefghijklmnop).

1234568790 isn't technically long enough to be a valid key. It's 80 bytes long. The smallest key AES supports is 128-bits. Maybe you should consider doing something like setPassword('1234568790') instead of setKey('1234568790'). setPassword will use a key-derivation function to generate a key from the password.

In phpseclib 1.0 and 2.0 setKey will, none-the-less, accept that as a key. It'll just null pad the key. The master branch otoh will throw an exception since the key isn't actually long enough.

And finally if the plaintext that I want to encrypt is short, something like "my name is james bond", is there anything extra that I should do? Because from the code above, it seems the plaintext's length should be 10 x 1024. (Why is that?)

10 * 1024 is just an example. phpseclib, by default, will use PKCS7 padding to make strings long enough. eg. your string is 21 bytes long (if I counted correctly). So if you call $cipher->encrypt('my name is james bond') then chr(11) will be appending to the plaintext 11 times, by phpseclib and then the string will be encrypted. This is because block algorithms need to have the the plaintext be a multiple of the block length. So the ciphertext will be 32 bytes long. When you then call $cipher->decrypt('...') you'll get the orig 21-byte string back. I could elaborate further on PKCS7 padding, but I think I've answered your immediate question.

neubert
  • 15,947
  • 24
  • 120
  • 212