Introduction:
This is an implementation of AES (Advanced Encryption Standard) algoritm that can easyly encrypt/decrypt data without external liblary.
Install:
To install this class just copy the file AES.class.php to your php liblary or scripts execution directory.
Configuration:
There are no configuration options. However during the object creation You can choose the length of key using: AES::AES128, AES::AES192, or AES::AES256 values.
Cipher functions:
This functions are used to crypt data:
- AES(key_strength): Class constructor. The parameter key_strength takes values: AES::AES128, AES::AES192, AES::AES256.
- encrypt(content, key):Encrypts 16 bytes of data. The parameter content is 16 bytes of data to encrypt in hexadecimal string representation, key Hexadecimal key with lenght specified in class constructor
- decrypt(content, key):Decrypts 16 bytes of data. The parameter content is 16 bytes of data to decrypt in hexadecimal string representation, key Hexadecimal key with lenght specified in class constructor
- stringToHex(str):Converts string content into hexadecimal representation. The parameter str is string that is converting.
- hexToString(hex):Converts hexadecimal string into charactes string. The parameter hex reprsets the hexadecimal input data
- selfTest():Testing how algoritm works showing time of execution each routine.
Examples:
Testing:
require_once('./AES.class.php');
$Cipher = new AES(AES::AES128);
$Cipher->selfTest();
Basic use (128bit):
$Cipher = new AES(AES::AES128);
$Cipher->selfTest();
require_once('./AES.class.php');
$Cipher = new AES();
$key_128bit = '2b7e151628aed2a6abf7158809cf4f3c';
// Encryption
$cryptext = $Cipher->encrypt($Cipher->stringToHex('Alice has a cat'), $key_128bit);
print $cryptext;
// Decryption
$result = $Cipher->encrypt($cryptext, $key_128bit);
print $Cipher->hexToString($result);
Custom key strength:
$Cipher = new AES();
$key_128bit = '2b7e151628aed2a6abf7158809cf4f3c';
// Encryption
$cryptext = $Cipher->encrypt($Cipher->stringToHex('Alice has a cat'), $key_128bit);
print $cryptext;
// Decryption
$result = $Cipher->encrypt($cryptext, $key_128bit);
print $Cipher->hexToString($result);
require_once('./AES.class.php');
$Cipher = new AES(AES::AES256);
$key_256bit = '603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4';
// Encryption
$cryptext = $Cipher->encrypt($Cipher->stringToHex('Alice has a cat'), $key_256bit);
print $cryptext;
// Decryption
$result = $Cipher->encrypt($cryptext, $key_256bit);
print $Cipher->hexToString($result);
$Cipher = new AES(AES::AES256);
$key_256bit = '603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4';
// Encryption
$cryptext = $Cipher->encrypt($Cipher->stringToHex('Alice has a cat'), $key_256bit);
print $cryptext;
// Decryption
$result = $Cipher->encrypt($cryptext, $key_256bit);
print $Cipher->hexToString($result);