Skip to content

Instantly share code, notes, and snippets.

@Halama
Created July 15, 2013 09:14
Show Gist options
  • Save Halama/5998576 to your computer and use it in GitHub Desktop.
Save Halama/5998576 to your computer and use it in GitHub Desktop.
AES ecryption using mcrypt simple API
<?php
/**
* AES Encryption in CBC mode with PKCS7 padding
*
* User: martinhalamicek
* Date: 7/8/13
* Time: 3:06 PM
*/
namespace Keboola\Encryption;
class AESEncryptor implements EncryptorInterface
{
private $_cipher = MCRYPT_RIJNDAEL_128;
private $_mode = MCRYPT_MODE_CBC;
private $_key;
private $_initializationVectorSize;
public function __construct($key)
{
$this->_key = $key;
$this->_initializationVectorSize = mcrypt_get_iv_size($this->_cipher, $this->_mode);
if (strlen($key) > ($keyMaxLength = mcrypt_get_key_size($this->_cipher, $this->_mode))) {
throw new \InvalidArgumentException("The key length must be less or equal than $keyMaxLength.");
}
}
public function encrypt($data)
{
$blockSize = mcrypt_get_block_size($this->_cipher, $this->_mode);
$pad = $blockSize - (strlen($data) % $blockSize);
$iv = mcrypt_create_iv($this->_initializationVectorSize, MCRYPT_DEV_URANDOM);
return $iv . mcrypt_encrypt(
$this->_cipher,
$this->_key,
$data . str_repeat(chr($pad), $pad),
$this->_mode,
$iv
);
}
public function decrypt($encryptedData)
{
$initializationVector = substr($encryptedData, 0, $this->_initializationVectorSize);
$data = mcrypt_decrypt(
$this->_cipher,
$this->_key,
substr($encryptedData, $this->_initializationVectorSize),
$this->_mode,
$initializationVector
);
$pad = ord($data[strlen($data) - 1]);
return substr($data, 0, -$pad);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment