phpseclib: Symmetric Key Encryption Examples and Notes

Cipher:

Encryption Mode:

Key Length:

Key Derivation:

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

$cipher = new Crypt_AES(
CRYPT_AES_MODE_
ECB);

// keys are null-padded to the closest valid size
// longer than the longest key and it's truncated
//$cipher->setKeyLength(128);
$cipher->setKey('abcdefghijklmnopijklmnop');

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

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

Speed Comparisons

The following table compares the speed of five different pure-PHP implementations of AES (one of which is Crypt_Rijndael and one of which is Crypt_AES) when ran on 150KB of text on a 1.8GHz Pentium 4-M. The numbers listed are averaged from five different trials and are measured in seconds. phpseclib's two implementations are highlighted. All implementations can be viewed by clicking on their names.

Implementation Speed
movable-type.phps 15.6844158172
phpaes.phps 39.9537248135
phpclasses1.phps 15.0100150108
phpclasses2.phps 62.591713190079
phpseclib-aes.phps 2.03581542968752
phpseclib-rijndael.phps 2.62501101493836

As can be seen, phpseclib's implementations are the fastest. phpseclib-aes.phps is faster than phpseclib-rijndael.phps because phpseclib-rijndael.phps has to contend with multiple block sizes whereas phpseclib-aes.phps does not. Note that if mcrypt weren't explicitily disabled phpseclib would have been even faster.