Created
May 30, 2013 13:55
-
-
Save whitekid/5678001 to your computer and use it in GitHub Desktop.
MySQL aes_encrypt compatible encrypt/decrypt function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static public function aes_encrypt($data, $key){ | |
$expected_length = 16 * (floor(strlen($data) / 16) +1); | |
$padding_length = $expected_length - strlen($data); | |
$data = $data . str_repeat(chr($padding_length), $padding_length); | |
$enc = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_ECB); | |
return strtoupper(bin2hex($enc)); | |
} | |
static public function aes_decrypt($data, $key){ | |
// bin2hex의 역 함수가 php는 없어서 새로 만듦 | |
if(!function_exists('hex2bin')){ | |
function hex2bin($h) | |
{ | |
if (!is_string($h)) return null; | |
$r=''; | |
for ($a=0; $a<strlen($h); $a+=2) { $r.=chr(hexdec($h{$a}.$h{($a+1)})); } | |
return $r; | |
} | |
} | |
$data = hex2bin($data); | |
$dec = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_ECB); | |
$last = $dec[strlen($dec) - 1]; | |
$dec = substr($dec, 0, strlen($dec) - ord($last)); | |
return $dec; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment