简体   繁体   中英

Using MYSQL AES_ENCRYPT function with PDO but wrong and different string format

here is my QUERY

SELECT client_id,client_name,client_db_ip,client_db_username,AES_ENCRYPT(client_db_pass, '1234') as client_db_pass,client_db_name FROM client_list WHERE client_id =:id

//pls focus on AES_ENCRYPT(client_db_pass, '1234') as client_db_pass

now if i query directly using phpmyadmin, i will get the result eg for client_db_pass after AES_ENCRYPT:

501defc4013f3f21529c123f33c065ad

But the problem come when iam using PDO to fetch the data,

        $Q = "SELECT client_id,client_name,client_db_ip,client_db_username,AES_ENCRYPT(client_db_pass, '1234') as client_db_pass,client_db_name FROM client_list WHERE client_id =:id";
    $R = $this->pdo->prepare($Q);
    $R->bindParam(':id', $id);
    $R->execute();
    $result = $R->fetch(PDO::FETCH_ASSOC);

if i var_dump($result)

i will get this result:

array (size=6)
'client_id' => string '1' (length=1)
'client_name' => string 'tester' (length=4)
'client_db_ip' => string '127.0.0.2' (length=9)
'client_db_username' => string 'root' (length=4)
'client_db_pass' => string 'PïÄ??!Rœ?3Àe­' (length=16)
'client_db_name' => string 'test_user' (length=9)

can you see the array for 'client_db_pass' string is

result 2 = PïÄ??!Rœ?3Àe­

not

result 1 = 501defc4013f3f21529c123f33c065ad

may i know how to get result 1 "501defc4013f3f21529c123f33c065ad" if using pdo?

thanks

AES_ENCRYPT() , according to docs , returns binary data:

returns a binary string containing the encrypted output.

If you see anything else in your MySQL client it's because the client encodes the raw output automatically.

In PHP you can get an hexadecimal dump with eg bin2hex() and in MySQL you can do it with HEX() . I don't think you need it though, both MySQL and PHP handle binary just fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM