PHP与mysql无法解密字符串(字符串来自数据库)

标签 php mysql encryption

我在这里没有看到这个问题(mysql 是关键 - 不仅仅是加密/解密问题)。

当 md5 加密字符串不是来自 mysql 数据库时,相同的 php 代码可以成功解密该字符串。不使用 mysql,字符串加密和解密均成功。我在这两种情况下使用相同的解密代码。

1)字符串加密保存到mysql

2) 加密的字符串随后被拉出并尝试解密并显示。

3) mysql 版本 - 解密失败(输出 -> ef32b9252e40bc9e228744923e33393b)。不使用 mysql –(输出“team”)

<?php
// version #1 – mysql version
$encrypted = "team";

// encrypt the string $encrypted
$key = md5(date('l jS of F Y h i s A'));
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $encrypted, MCRYPT_MODE_CBC, md5(md5($key))));**

// Of course here would be the INSERT…..
// So now “team” is in the mysql database encrypted.

$userid = "bob";
$query = "select team from league where username = '".$userid."'";
$result = mysql_query($query);

while ($row = mysql_fetch_array($result)) {
    $encrypted = trim($row['team']);
}

// decrypt the string $encrypted
$key = md5(date('l jS of F Y h i s A'));
$decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($encrypted), MCRYPT_MODE_CBC, md5(md5($key))), "\0");

print  $decrypted;   //outputs -> ef32b9252e40bc9e228744923e33393b

// Version #2 - non mysql

$encrypted  = "team"    // Not from mysql DB.

// encrypt the string $encrypted
$key = md5(date('l jS of F Y h i s A'));
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $encrypted, MCRYPT_MODE_CBC, md5(md5($key))));

// decrypt the string $encrypted
$key = md5(date('l jS of F Y h i s A'));
$decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($encrypted), MCRYPT_MODE_CBC, md5(md5($key))), "\0");

print  $decrypted;   //outputs -> team

?>

最佳答案

如果我正确理解了该问题,则问题在于您对 MCRYPT_RIJNDAEL_256 的使用。

来自AES-256 encryption in PHP

AES-256 is different from RIJNDAEL-256. The 256 in AES refers to the key size, where the 256 in RIJNDAEL refers to block size. AES-256 is RIJNDAEL-128 when used with a 256 bit key.

关于PHP与mysql无法解密字符串(字符串来自数据库),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23096478/

相关文章:

php - php xpath 中的句子中间通配符

php - 从 WAMP 服务器更改为 LAMP 服务器后,文件下载脚本不起作用。

mysql - 大型不常访问数据集的数据库解决方案

用于文件加密和解密的Python脚本

php - 获取序列化的 POST 数据

php mysql,更新查询不起作用

PHP for 循环在 2 次循环后停止获取 MYSQL 表行

web-services - 为什么要使用 x.509 证书来加密 xml?为什么不直接通过 https 传输?

sql-server - SQL EncryptByKey 不适用于插入,适用于更新

php - Ubuntu 12.04 和 PHP 5.4 上缺少软件包