使用 mysql aes_encryption 函数解密加密的字符串
Decrypt string Encrypted using mysql aes_encryption function
为了项目要求,我需要在 mysql 中以加密格式存储值。
PHP 和 Mysql 双方应该都可以解密和加密。
我尝试使用 openssl 加密解密。在 mysql 中,对于低于 5.7 的版本,我们无法设置加密模式。默认模式是 aes-128-ecb。当我尝试在 openssl 中使用相同模式解密时,我得到空值。
查询:SELECT AES_ENCRYPT('ABCD', '69f0ff56314d4e2d02bc89f6f4b5292c') FROM tbl_name;
结果:0x907fb5ce60223d205e7c0f0bc338d563
当我将此结果传递给 PHP 时:
<?php
$plaintext = "0x907fb5ce60223d205e7c0f0bc338d563";
$cipher = "aes-128-ecb";
$key = "69f0ff56314d4e2d02bc89f6f4b5292c";
if(in_array($cipher, openssl_get_cipher_methods())) {
$ciphertext = openssl_decrypt($plaintext, $cipher, $key);
echo $ciphertext;
}
没用。知道我缺少什么或任何其他实现方式吗?
Mysql 版本:5.0
PHP : 7
您的 PHP-code 中存在一些问题,但最重要的是通过 MySQL 文档中未提及的函数从给定密钥“导出”解密密钥。
我从这个站点获得了信息和功能,所有功劳归于他们:https://www.smashingmagazine.com/2012/05/replicating-mysql-aes-encryption-methods-with-php/。
其他问题是 a) 密文是二进制表示的十六进制编码字符串和 b) openssl_decrypt 函数末尾的“true”参数[将数据作为二进制而不是 Base64编码].
安全警告:此代码使用的是不安全的 AES 模式'EBC',静态密钥并且没有异常处理。该代码仅用于教育目的。
这是简单的预期输出 - 您可以通过我的在线编译器 运行 代码 (https://repl.it/@javacrypto/PhpAesDecryptFromMysql):
ABCD
完整 运行ning 源代码:
<?php
function mysql_aes_key($key)
// source: https://www.smashingmagazine.com/2012/05/replicating-mysql-aes-encryption-methods-with-php/
{
$new_key = str_repeat(chr(0), 16);
for ($i = 0, $len = strlen($key); $i < $len; $i++) {
$new_key[$i % 16] = $new_key[$i % 16] ^ $key[$i];
}
return $new_key;
}
$ciphertextFromMysql = "907fb5ce60223d205e7c0f0bc338d563";
$key = "69f0ff56314d4e2d02bc89f6f4b5292c";
$ciphertext = hex2bin($ciphertextFromMysql);
$cipher = "aes-128-ecb";
if (in_array($cipher, openssl_get_cipher_methods())) {
//$ciphertext = openssl_decrypt($ciphertext, $cipher, $key, true);
$decrypttext = openssl_decrypt($ciphertext, $cipher, mysql_aes_key($key), true);
echo $decrypttext;
}
?>
为了项目要求,我需要在 mysql 中以加密格式存储值。 PHP 和 Mysql 双方应该都可以解密和加密。 我尝试使用 openssl 加密解密。在 mysql 中,对于低于 5.7 的版本,我们无法设置加密模式。默认模式是 aes-128-ecb。当我尝试在 openssl 中使用相同模式解密时,我得到空值。
查询:SELECT AES_ENCRYPT('ABCD', '69f0ff56314d4e2d02bc89f6f4b5292c') FROM tbl_name;
结果:0x907fb5ce60223d205e7c0f0bc338d563
当我将此结果传递给 PHP 时:
<?php
$plaintext = "0x907fb5ce60223d205e7c0f0bc338d563";
$cipher = "aes-128-ecb";
$key = "69f0ff56314d4e2d02bc89f6f4b5292c";
if(in_array($cipher, openssl_get_cipher_methods())) {
$ciphertext = openssl_decrypt($plaintext, $cipher, $key);
echo $ciphertext;
}
没用。知道我缺少什么或任何其他实现方式吗? Mysql 版本:5.0 PHP : 7
您的 PHP-code 中存在一些问题,但最重要的是通过 MySQL 文档中未提及的函数从给定密钥“导出”解密密钥。
我从这个站点获得了信息和功能,所有功劳归于他们:https://www.smashingmagazine.com/2012/05/replicating-mysql-aes-encryption-methods-with-php/。
其他问题是 a) 密文是二进制表示的十六进制编码字符串和 b) openssl_decrypt 函数末尾的“true”参数[将数据作为二进制而不是 Base64编码].
安全警告:此代码使用的是不安全的 AES 模式'EBC',静态密钥并且没有异常处理。该代码仅用于教育目的。
这是简单的预期输出 - 您可以通过我的在线编译器 运行 代码 (https://repl.it/@javacrypto/PhpAesDecryptFromMysql):
ABCD
完整 运行ning 源代码:
<?php
function mysql_aes_key($key)
// source: https://www.smashingmagazine.com/2012/05/replicating-mysql-aes-encryption-methods-with-php/
{
$new_key = str_repeat(chr(0), 16);
for ($i = 0, $len = strlen($key); $i < $len; $i++) {
$new_key[$i % 16] = $new_key[$i % 16] ^ $key[$i];
}
return $new_key;
}
$ciphertextFromMysql = "907fb5ce60223d205e7c0f0bc338d563";
$key = "69f0ff56314d4e2d02bc89f6f4b5292c";
$ciphertext = hex2bin($ciphertextFromMysql);
$cipher = "aes-128-ecb";
if (in_array($cipher, openssl_get_cipher_methods())) {
//$ciphertext = openssl_decrypt($ciphertext, $cipher, $key, true);
$decrypttext = openssl_decrypt($ciphertext, $cipher, mysql_aes_key($key), true);
echo $decrypttext;
}
?>