Source for file aes.php
Documentation is available at aes.php
* @package FrameworkOnFramework
* @copyright Copyright (C) 2010 - 2012 Akeeba Ltd. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
// Protect from unauthorized access
* A simple implementation of AES-128, AES-192 and AES-256 encryption using the
* high performance mcrypt library.
* @package FrameworkOnFramework
/** @var string The AES cipher to use (this is an mcrypt identifier, not the bit strength) */
private $_cipherType =
0;
/** @var string Cipher mode. Can be CBC or ECB. We recommend using CBC */
private $_cipherMode =
0;
/** @var string The cipher key (password) */
private $_keyString =
'';
* Initialise the AES encryption object
* @param string $key The encryption key (password). It can be a raw key (32 bytes) or a passphrase.
* @param int $strength Bit strength (128, 192 or 256)
* @param string $mode Ecnryption mode. Can be ebc or cbc. We recommend using cbc.
public function __construct($key, $strength =
256, $mode =
'cbc')
$this->_keyString =
$key;
$this->_cipherType =
MCRYPT_RIJNDAEL_256;
$this->_cipherType =
MCRYPT_RIJNDAEL_192;
$this->_cipherType =
MCRYPT_RIJNDAEL_128;
$this->_cipherMode =
MCRYPT_MODE_ECB;
$this->_cipherMode =
MCRYPT_MODE_CBC;
* Encrypts a string using AES
* @param string $stringToEncrypt The plaintext to encrypt
* @param bool $base64encoded Should I Base64-encode the result?
* @return string The cryptotext. Please note that the first 16 bytes of
* the raw string is the IV (initialisation vector) which
* is necessary for decoding the string.
public function encryptString($stringToEncrypt, $base64encoded =
true)
if (strlen($this->_keyString) !=
32)
$key =
hash('sha256', $this->_keyString, true);
$key =
$this->_keyString;
// Set up the IV (Initialization Vector)
$cipherText =
mcrypt_encrypt($this->_cipherType, $key, $stringToEncrypt, $this->_cipherMode, $iv);
// Prepend the IV to the ciphertext
$cipherText =
$iv .
$cipherText;
// Optionally pass the result through Base64 encoding
* Decrypts a ciphertext into a plaintext string using AES
* @param string $stringToDecrypt The ciphertext to decrypt. The first 16 bytes of the raw string must contain the IV (initialisation vector).
* @param bool $base64encoded Should I Base64-decode the data before decryption?
* @return string The plain text string
public function decryptString($stringToDecrypt, $base64encoded =
true)
if (strlen($this->_keyString) !=
32)
$key =
hash('sha256', $this->_keyString, true);
$key =
$this->_keyString;
$iv =
substr($stringToDecrypt, 0, $iv_size);
$stringToDecrypt =
substr($stringToDecrypt, $iv_size);
$plainText =
mcrypt_decrypt($this->_cipherType, $key, $stringToDecrypt, $this->_cipherMode, $iv);
* Is AES encryption supported by this PHP installation?
if (!in_array('rijndael-128', $algorightms))
if (!in_array('rijndael-192', $algorightms))
if (!in_array('rijndael-256', $algorightms))
Documentation generated on Tue, 19 Nov 2013 14:53:38 +0100 by phpDocumentor 1.4.3