我在这里没有看到这个问题(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 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/