Source for file mysqlAES.php
Documentation is available at mysqlAES.php
* Project: mysqlAES :: MYSQL 호환 AES ENCRYPT/DECRYPT Class<br>
* mysqlAES 패키지는 MySQL의 AES_EMCRYPT, AES_DECRYPT, HEX, UNHEX 함수를
* php에서 호환되게 사용할 수 있도록 하는 기능을 제공한다.
* encrypt method와 decrypt method의 경우, key 길이가 128bit(16byte)이면
* MySQL과 MariaDB의 AES_ENCRYPT/AES_DECRYPT 함수와 완벽하게 호환이 된다.
* key 길이가 192또는 256bit일 경우에는 oops에서 제공하는 lib_mysqludf_aes256
* UDF에서 제공하는 AES256_ENCRYPT, AES256_DECRYPT와 완변하게 호환이 된다.
* {@example mysqlAES/test.php}
* @author JoungKyun.Kim <http://oops.org>
* @copyright (c) 2018, OOPS.org
* @link http://pear.oops.org/package/mysqlAES
* @since File available since release 0.0.1
* @example mysqlAES/test.php mysqlAES 예제
* mysqlAES 패키지는 MySQL의 AES_EMCRYPT, AES_DECRYPT, HEX, UNHEX 함수를
* php에서 호환되게 사용할 수 있도록 하는 기능을 제공한다.
* encrypt method와 decrypt method의 경우, key 길이가 128bit(16byte)이면
* MySQL과 MariaDB의 AES_ENCRYPT/AES_DECRYPT 함수와 완벽하게 호환이 된다.
* key 길이가 192또는 256bit일 경우에는 oops에서 제공하는 lib_mysqludf_aes256
* UDF에서 제공하는 AES256_ENCRYPT, AES256_DECRYPT와 완변하게 호환이 된다.
* {@example mysqlAES/test.php}
const AES_BLOCK_SIZE = 16;
* Variables for separating mcrypt or openssl extensions.
* mcrypt takes precedence over openssl.
// {{{ +-- public __construct (void)
// {{{ +-- static public (string) hex ($v)
* Return a hexadecimal representation of a decimal or string value
* This method is compatible HEX function of mysql
* {@example mysqlAES/test.php 16 1}
* @return string hexadecimal data. If given parameter $v is empty, return null.
* @param string original data
static public function hex ($v) {
// {{{ +-- static public (string) unhex ($v)
* Return a string containing hex representation of a number
* This method is compatible UNHEX function of mysql
* {@example mysqlAES/test.php 19 1}
* @return string Returns an ASCII string containing the hexadecimal representation.
* If given parameter $v is empty, return null.
* @param string hexadecimal data
static public function unhex ($v) {
return self::hex2bin ($v);
// {{{ +-- static private (string) _encrypt ($cipher, $key)
* skeleton encrypt function
* @return string encrypted data by AES
* @param string The plaintext message data to be encrypted.
* @param string The key for encryption
static private function _encrypt ($cipher, $key) {
if ( self::$extname == 'mcrypt' )
return mcrypt_encrypt (MCRYPT_RIJNDAEL_128, $key, $cipher, MCRYPT_MODE_ECB);
else if ( $keylen <= 24 )
return openssl_encrypt ($cipher, $method, $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);
// {{{ +-- static public (string) encrypt ($cipher, $key)
* This method is compatible AES_ENCRYPT function of mysql, if key is 128 bit.
* And then, If key is 192 or 256 bit, this method is compatible follow APIS:
* - {@link http://mirror.oops.org/pub/oops/mysql/lib_mysqludf_aes256/ MySQL UDF lib_mysqludf_aes256}
* - {@link http://mirror.oops.org/pub/oops/javascript/mysqlAES/ Javascript mysqlAES class}
* {@example mysqlAES/test.php}
* @return string encrypted data by AES. If $cipyer or $key has empty value, return null
* @param string The plaintext message data to be encrypted.
* @param string encryption key
* - 128bit : 16 byte string
* - 192bit : 24 byte string
* - 256bit : 32 byte string
static public function encrypt ($cipher, $key) {
if ( ! $cipher || ! $key )
$blocks = self::AES_BLOCK_SIZE * (floor (strlen ($cipher) / self::AES_BLOCK_SIZE) + 1);
$padlen = (int) $blocks - strlen ($cipher);
$r = self::_encrypt ($cipher, $key);
// {{{ +-- static private (string) _decrypt ($cipher, $key)
* skeleton encrypt function
* @return string encrypted data by AES
static private function _decrypt ($cipher, $key) {
if ( self::$extname == 'mcrypt' ) {
return mcrypt_decrypt (MCRYPT_RIJNDAEL_128, $key, $cipher, MCRYPT_MODE_ECB);
else if ( $keylen <= 24 )
return openssl_decrypt ($cipher, $method, $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);
// {{{ +-- static public (string) decrypt ($cipher, $key)
* This method is compatible AES_DECRYPT function of mysql, if key is 128 bit
* And then, If key is 192 or 256 bit, this method is compatible follow APIS:
* - {@link http://mirror.oops.org/pub/oops/mysql/lib_mysqludf_aes256/ MySQL UDF lib_mysqludf_aes256}
* - {@link http://mirror.oops.org/pub/oops/javascript/mysqlAES/ Javascript mysqlAES class}
* {@example mysqlAES/test.php}
* @return string decrypted data by AES. If $cipyer or $key has empty value, return null.
* @param string cipher data for being decryption
* @param string decryption key
* - 128bit : 16 byte string
* - 192bit : 24 byte string
* - 256bit : 32 byte string
static public function decrypt ($cipher, $key) {
if ( ! $cipher || ! $key )
if ( ! ($r = self::_decrypt ($cipher, $key)) )
// {{{ +-- private (string) hex2bin ($v)
* Decodes a hexadecimally encoded binary string
* Support hex2bin function if php version is less than 5.4.0.
* @return string Returns the binary representation of the given data or null on failure.
* @param string Hexadecimal representation of data.
private function hex2bin ($v) {
for ( $i= 0; $i< $len; $i+= 2 ) {
// {{{ +-- public mysqlAES_REQUIRES (void)
* mysqlAES 패키지에서 필요한 의존성을 검사한다.
else if ( extension_loaded ('openssl') )
throw new Exception ('mysqlAES class must need mcrypt or openssl extension', E_USER_ERROR);
|