3 * The mcrypt block cipher implementation
5 * This class is used above all other implementations since it uses a core PHP
6 * library if it is available.
10 * @category PHPCryptLib
13 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
14 * @copyright 2011 The Authors
15 * @license http://www.opensource.org/licenses/mit-license.html MIT License
16 * @version Build @@version@@
19 namespace CryptLib\Cipher\Block\Cipher
;
22 * The mcrypt block cipher implementation
24 * This class is used above all other implementations since it uses a core PHP
25 * library if it is available.
27 * @category PHPCryptLib
30 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
32 class MCrypt
extends \CryptLib\Cipher\Block\AbstractCipher
{
35 * @var resource The mcrypt resource for cipher operations
37 protected $mcrypt = null;
40 * Get a list of supported ciphers for this class implementation
42 * @return array A list of supported ciphers
44 public static function getSupportedCiphers() {
45 // @codeCoverageIgnoreStart
46 if (!function_exists('mcrypt_list_algorithms')) {
49 // @codeCoverageIgnoreEnd
50 return mcrypt_list_algorithms();
54 * Construct the instance for the supplied cipher name
56 * @param string $cipher The cipher to implement
59 * @throws InvalidArgumentException if the cipher is not supported
61 public function __construct($cipher) {
62 parent
::__construct($cipher);
63 $this->keySize
= mcrypt_get_key_size($cipher, MCRYPT_MODE_ECB
);
64 $this->blockSize
= mcrypt_get_block_size($cipher, MCRYPT_MODE_ECB
);
68 * Destroy the mcrypt module if it's open
72 public function __destruct() {
74 mcrypt_module_close($this->mcrypt
);
79 * Set the key to use for the cipher
81 * @param string $key The key to use
83 * @throws InvalidArgumentException If the key is not the correct size
86 public function setKey($key) {
87 switch ($this->cipher
) {
91 if (!in_array(strlen($key), array(16, 20, 24, 28, 32))) {
92 throw new \
InvalidArgumentException(
94 'The supplied key block is in the valid sizes [%d:%s]',
100 $this->keySize
= strlen($key);
102 parent
::setKey($key);
107 * Decrypt a block of data using the supplied string key
109 * Note that the supplied data should be the same size as the block size of
110 * the cipher being used.
112 * @param string $data The data to decrypt
114 * @return string The result decrypted data
116 protected function decryptBlockData($data) {
117 return mdecrypt_generic($this->mcrypt
, $data);
121 * Encrypt a block of data using the supplied string key
123 * Note that the supplied data should be the same size as the block size of
124 * the cipher being used.
126 * @param string $data The data to encrypt
128 * @return string The result encrypted data
130 protected function encryptBlockData($data) {
131 return mcrypt_generic($this->mcrypt
, $data);
135 * Initialize the cipher by preparing the key
137 * @return boolean The status of the initialization
138 * @codeCoverageIgnore
140 protected function initialize() {
142 mcrypt_module_close($this->mcrypt
);
144 $this->mcrypt
= mcrypt_module_open($this->cipher
, '', MCRYPT_MODE_ECB
, '');
146 $initv = str_repeat(chr(0), $this->getBlockSize());
147 return false !== mcrypt_generic_init($this->mcrypt
, $this->key
, $initv);