mysql aes256_mysql和php中的AES加密

您使用的代码有三个问题:

>正如其他人所提到的,您的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" ));

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值