【问题标题】:PHP with mysql fails to decrypt a string (string comes from the database)使用mysql的PHP无法解密字符串(字符串来自数据库)
【发布时间】: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


    【解决方案1】:

    如果我正确理解了这个问题,那么问题在于您使用了MCRYPT_RIJNDAEL_256

    来自AES-256 encryption in PHP

    AES-256 与 RIJNDAEL-256 不同。 AES 中的 256 是指密钥大小,而 RIJNDAEL 中的 256 是指块大小。 AES-256 与 256 位密钥一起使用时是 RIJNDAEL-128。

    【讨论】:

      猜你喜欢
      • 2018-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 2016-08-12
      • 1970-01-01
      相关资源
      最近更新 更多