Source for file mysqlAES.php
Documentation is available at mysqlAES.php
declare (strict_types= 1);
declare (encoding= 'UTF-8');
* Project: oops\Encrypt\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와 완변하게 호환이 된다.
* <ul style="margin-left: 30px;">
* PHP 7.1 이전 버전에서는 1.0 branch 를 사용하십시오.</li>
* <li>openssl extension</li>
* {@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;
// {{{ +-- public __construct (void)
* oops\Encrypt\mysqlAES 초기화
// {{{ +-- static public (string) hex (string $v = null)
* Return a hexadecimal representation of a decimal or string value
* This method is compatible HEX function of mysql
* {@example mysqlAES/test.php 22 1}
* @return string hexadecimal data. If given parameter $v is empty, return null.
* @param string original data
static public function hex (? string $v): ? string {
// {{{ +-- static public (string) unhex (string $v = null)
* Return a string containing hex representation of a number
* This method is compatible UNHEX function of mysql
* {@example mysqlAES/test.php 26 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 (? string $v): ? string {
// {{{ +-- static private (string) _encrypt (string $cipher, string $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 (string $cipher, string $key): ? string {
else if ( $keylen <= 24 )
return openssl_encrypt ($cipher, $method, $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);
// {{{ +-- static public (string) encrypt (string $cipher = null, string $key = null)
* 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 (? string $cipher, ? string $key): ? string {
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 (string $cipher, string $key)
* skeleton encrypt function
* @return string encrypted data by AES
static private function _decrypt (string $cipher, string $key): ? string {
else if ( $keylen <= 24 )
return openssl_decrypt ($cipher, $method, $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);
// {{{ +-- static public (string) decrypt (string $cipher = null, string $key = null)
* 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 (? string $cipher, ? string $key): ? string {
if ( ! $cipher || ! $key )
if ( ! ($r = self::_decrypt ($cipher, $key)) )
// {{{ +-- public mysqlAES_REQUIRES (void)
* mysqlAES 패키지에서 필요한 의존성을 검사한다.
__NAMESPACE__ . '\mysqlAES class must need openssl extension',
|