在mysql和php中加密AES [英] AES encryption in mysql and php
问题描述
SELECT AES_encrypt(Hello World,password)AS encrypted_value
这给出了结果: 9438eb79863e7009722fc3f0ad4b7198
但是当我使用php中的代码执行AES_encrypt时,它会给我一个不同的值。
我得到的PHP代码来自stackoverflow - PHP AES加密/解密
$ b $ (
mcrypt_encrypt(
MCRYPT_RIJNDAEL_256
$ sSecretKey,$ sValue,
MCRYPT_MODE_ECB(
$ ,
mcrypt_create_iv(
mcrypt_get_iv_size(
MCRYPT_RIJNDAEL_256,
MCRYPT_MODE_ECB
),
MCRYPT_RAND)
)
),\ 0
?>
PHP代码的结果是 ytip2sEkD87gmRk3IVI09qE7T + RoLr20YK4rJp16NkY =
php或codeigniter中是否有方法返回相同的值。
- 谢谢。
您正在使用的代码有三个问题:
-
正如其他人所说,您的PHP代码目前正在使用
MCRYPT_RIJNDAEL_256
,而如下所述AES_ENCRYPT()
:
使用128位密钥长度的编码,但您可以通过修改扩展到256位来源。我们选择了128位,因为它的速度要快得多,而且对于大多数用途来说都是安全的。
-
正在应用
base64_encode()
将PHP的二进制结果转换为文本,而MySQL结果似乎只是其二进制结果的十六进制表示。您可以使用TO_BASE64()
在v5.6.1之后,或者bin2hex()
在PHP中。 -
如
mcrypt_encrypt()
:
如果数据的大小不是n * blocksize,数据将用 \0 '。
而MySQL使用 PKCS7 padding 。
因此,要获得相同的结果在PHP中您正在为MySQL显示:
<?php
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);打破;
默认值:
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));
?>
There is a function in Mysql AES_encrypt.
SELECT AES_encrypt( "Hello World", "password" ) AS encrypted_value
This gives the result: 9438eb79863e7009722fc3f0ad4b7198
But when I use the code in php to do AES_encrypt it gives me a different value.
The PHP code I got from stackoverflow -- PHP AES encrypt / decrypt
<?php
base64_encode(
mcrypt_encrypt(
MCRYPT_RIJNDAEL_256,
$sSecretKey, $sValue,
MCRYPT_MODE_ECB,
mcrypt_create_iv(
mcrypt_get_iv_size(
MCRYPT_RIJNDAEL_256,
MCRYPT_MODE_ECB
),
MCRYPT_RAND)
)
), "\0"
?>
The result from PHP code is ytip2sEkD87gmRk3IVI09qE7T+RoLr20YK4rJp16NkY=
Is there a method in php or codeigniter so that it returns the same value.? --Thank you.
There are three problems with the code you are using:
As others have mentioned, your PHP code is currently using
MCRYPT_RIJNDAEL_256
whereas, as documented underAES_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.
As others have mentioned, you are applying
base64_encode()
to convert PHP's binary result to text, whereas the MySQL result appears merely to be a hexadecimal representation of its binary result. You can either useTO_BASE64()
in MySQL since v5.6.1 or elsebin2hex()
in PHP.As documented under
mcrypt_encrypt()
:If the size of the data is not n * blocksize, the data will be padded with '\0'.
Whereas MySQL uses PKCS7 padding.
Therefore, to obtain the same results in PHP as you currently show for MySQL:
<?php
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" ));
?>
这篇关于在mysql和php中加密AES的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!