您使用的代码有三个问题:
>正如其他人所提到的,您的PHP代码目前正在使用MCRYPT_RIJNDAEL_256,而正如AES_ENCRYPT()所述:
Encoding with a 128-bit key length is used, but you can extend it up to 256 bits by modifying the source. We chose 128 bits because it is much faster and it is secure enough for most purposes.
>正如其他人所提到的,您正在应用base64_encode()将PHP的二进制结果转换为文本,而MySQL结果似乎只是其二进制结果的十六进制表示.你可以在v5.6.1中使用MySQL中的TO_BASE64(),或者在PHP中使用bin2hex().
>如mcrypt_encrypt()所述:
If the size of the data is not n * blocksize, the data will be padded with ‘\0‘.
因此,要在PHP中获得与当前为MySQL显示的相同的结果:
class MySQL_Function {
const PKCS7 = 1;
private static function pad($string, $mode, $blocksize = 16) {
$len = $blocksize - (strlen($string) % $blocksize);
switch ($mode) {
case self::PKCS7:
$padding = str_repeat(chr($len), $len); break;
default:
throw new Exception();
}
return $string.$padding;
}
public static function AES_ENCRYPT($str, $key_str) {
return mcrypt_encrypt(
MCRYPT_RIJNDAEL_128,
$key_str, self::pad($str, self::PKCS7),
MCRYPT_MODE_ECB
);
}
}
echo bin2hex(MySQL_Function::AES_encrypt( "Hello World", "password" ));
?>