AES_DECRYPT返回“Array”而不是解密数据

时间:2012-12-04 19:38:32

标签: php mysqli aes encryption

我正在尝试在MySQL中使用AES_DECRYPT来解密成功加密的SSN。在输出中,我得到单词“Array”而不是该字段的实际数据。我的PHP和MySQL知识有点生疏,所以我确定这是我忽略的傻事。任何帮助将不胜感激。

输出:
verify_name other_names ssn dob
:test:test:Array:test

CODE:
$key="88b871WZ3SntWK67rN3l2J1SvMqsOjyk";
$SQLstring = "SELECT * FROM applications";
$QueryResult = @mysql_query($SQLstring, $conn) or die("Query Problem - "
. mysql_error($conn) . " - Error Number - "
. mysql_errno($conn));
echo "verify_name other_names ssn dob";

$num_result = mysql_num_rows($QueryResult);
for ($i = 0; $i < $num_result; $i++)

{
$row = mysql_fetch_array($QueryResult);
$SQLstring2 = "SELECT AES_DECRYPT(ssn,'$key') FROM applications WHERE name='" . $row["name"] . "'";
$QueryResult2 = @mysql_query($SQLstring2, $conn) or die("Query Problem - "
. mysql_error($conn) . " - Error Number - " . mysql_errno($conn));
$num_result2 = mysql_num_rows($QueryResult2);
for ($j = 0; $j < $num_result; $j++){
$ssndecrypt = mysql_fetch_array($QueryResult2);

echo $ssndecrypt[0];
} echo $row["verify_name"];
echo $row["other_names"];
echo $ssndecrypt;
echo $row["dob"];

1 个答案:

答案 0 :(得分:0)

这是因为您将结果作为数组提取。

$ssndecrypt = mysql_fetch_array($QueryResult2);
...
echo $ssndecrypt;

它回应Array,因为您永远不会重新分配$ssndecrypt变量。

然而,问题的核心似乎是您不必要地使查询复杂化。你可以做的就是没有理由两次查询表格:

SELECT verify_name,
       other_names,
       dob,
       AES_DECRYPT(ssn,'$key') AS ssn
  FROM applications

这简化了代码:

$stmt = "SELECT verify_name, other_names, dob, AES_DECRYPT(ssn,'$key') AS ssn FROM applications";
$result = @mysql_query($stmt, $conn) or die("Query Problem - " . mysql_error($conn) . " - Error Number - " . mysql_errno($conn));
echo "verify_name other_names ssn dob";
while ($row = mysql_fetch_assoc($result))
{
    echo $row["verify_name"];
    echo $row["other_names"];
    echo $row["ssn"];
    echo $row["dob"];
}

但理想情况下,您应该使用PDO而不是mysql_*函数:

try {
    $dbh = new PDO('mysql:host=localhost;dbname=mydb', $user, $pass);
    foreach($dbh->query("SELECT verify_name, other_names, dob, AES_DECRYPT(ssn,'$key') AS ssn FROM applications") as $row) {
        echo $row["verify_name"];
        echo $row["other_names"];
        echo $row["ssn"];
        echo $row["dob"];
    }
    $dbh = null;
} catch (PDOException $e) { die("ERROR: " . $e->getMessage()); }