duanguanye4124 2012-07-10 16:51
浏览 247
已采纳

MySQL AES Decrypt返回值

Trying to develop an AES-encrypted database for a client using PHP/MySQL. Inserting encrypted values is working, but having trouble returning usable decrypted values.

public static function auth($username="", $password="") {
    global $database;
    $fields = self::$db_fields;
    array_shift($fields);
    $username = $database->escape_value($username);
    $password = $database->escape_value($password);
    $hashed_pwd = sha1($password);
    $join_str = ", '" . AUTH_KEY . "'), AES_DECRYPT(";

    $sql  = "SELECT id, AES_DECRYPT(";
    $sql .=  join($join_str, $fields) . ", '" . AUTH_KEY . "') FROM " . self::$table_name. " ";
    $sql .= "WHERE u_name = AES_ENCRYPT('{$username}', '" . AUTH_KEY . "') ";
    $sql .= "AND u_pwd = AES_ENCRYPT('{$hashed_pwd}', '" . AUTH_KEY . "') ";
    $sql .= "LIMIT 1";

With the following query:

    $query_result = $database->query($sql);
    $result_array = $database->fetch_array($query_result);

}

Returns:

    Array ( 
        [0] => 5 
        [id] => 5 
        [1] => a_user_name 
        [AES_DECRYPT(user_name, '[PRINTS FULL AUTH KEY]')] => a_user_name 
        [2] => 0000hashedpasswordstring00000 
        [AES_DECRYPT(user_pwd, '[PRINTS FULL AUTH KEY]')] => 0000hashedpasswordstring00000  
        [3] => sample@email.com 
        [AES_DECRYPT(user_email, '[PRINTS FULL AUTH KEY]')] => sample@email.com 
    )

This was a secondary solution. Ideally the query would be object-oriented, but that approach was returning an array with the fields but no values.

Clearly, having the full auth key transferred in the results completely defeats the purpose of the encryption. Confused as to why it's returning a combined associate/indexed array, rather than just an indexed array, or just an associative array. Is there a change in SQL syntax that would return [user_email] => sample@email.com instead of [AES_DECRYPT(user_email, '[PRINTS FULL AUTH KEY]')] => sample@email.com?

  • 写回答

1条回答 默认 最新

  • doushi3715 2012-07-10 17:58
    关注

    You need to use an alias for the column that includes the function:

    $sql  = "SELECT id, AES_DECRYPT(";
    $sql .=  join($join_str, $fields) . ", '" . AUTH_KEY . "') AS user_email FROM " . self::$table_name. " ";
    $sql .= "WHERE u_name = AES_ENCRYPT('{$username}', '" . AUTH_KEY . "') ";
    $sql .= "AND u_pwd = AES_ENCRYPT('{$hashed_pwd}', '" . AUTH_KEY . "') ";
    $sql .= "LIMIT 1";
    

    From MySQL documentation on SELECT:

    A select_expr can be given an alias using AS alias_name. The alias is used as the expression's column name and can be used in GROUP BY, ORDER BY, or HAVING clauses. For example:

    SELECT CONCAT(last_name,', ',first_name) AS full_name FROM mytable ORDER BY full_name;

    Encryption like this is usually done in the application instead of in MySQL. When done in MySQL, the secret key ends up in the MySQL logs.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算