【发布时间】:2014-05-30 13:27:26
【问题描述】:
我在这里没有看到这个问题(mysql 是关键 - 不仅仅是一个加密/解密问题)。
当它不是来自 mysql 数据库时,相同的 php 代码成功解密了 md5 加密字符串。不使用 mysql 字符串加密和解密都成功。我在两种情况下都使用相同的解密代码。
1 )字符串被加密保存到mysql中
2) 加密后的字符串被提取出来并尝试解密并显示。
3) mysql 版本 - 解密失败(输出 -> ef32b9252e40bc9e228744923e33393b)。不使用 mysql –(输出“团队”)
<?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
?>
【问题讨论】:
标签: php mysql encryption