How to display decrypted AES in php form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anhtu
    New Member
    • Oct 2017
    • 1

    How to display decrypted AES in php form

    I have a form that I use php to retrieve data from MySQL and display that data in an input box. One of the fields is encrypted when the data is submitted into MySQL using AES_ENCRYPTED and that part works fine, I can see in the DB that that data is encrypted.
    My issue is how to use AES_DECRYPT to display the decrypted data in my input box with my php code. For example my form has a field like this <input type="text" name="data" value="<?php echo $data2['data']?>"/> <--- This field being the encrypted data that I need to decrypted and display as it's original info.
    I know that I can enter this at a MySQL command line SELECT aes_decrypt(dat a, '123456790abcde fghijklmnopqrst uvwxyz') FROM applications WHERE ID = '52' and it will display the data for record 52 decrypted I just don't know how to implement it in my php form.
    I've tried this:
    $data = "SELECT * FROM $tbl_name WHERE ID = $id";
    $query = mysqli_query($c onn, $data);
    $data2 = mysqli_fetch_ar ray($query);
    and obviously it doesn't decrypt but it does show the encrypted data in the form.
    I tried different variations of SELECT AES_DECRYPT('da ta', '$aeskey') FROM $tbl_name WHERE ID = $id"; and so on with no success.
    I've tried to change the input value to value="<?php echo $data2[AES_DECRYPT('da ta', '$aeskey')]?>"/> and I get an empty input box.

    Can someone help me get this solved PLEASE?
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    My experience with PHP is limited, but this example shows a working INSERT and SELECT

    Code:
    <?php
    $server = "localhost";
    $user = "root";
    $pass = "secret";
    $db = "test";
    $mysqli = mysqli_connect($server, $user, $pass, $db);
    
    $aeskey = "12345abcde";
    $testvalue = "sometestvalue";
    
    $stmt =  $mysqli->stmt_init();
    $stmt->prepare("drop table if exists aes");
    $stmt->execute();
    $stmt->fetch();
    $stmt->close();
    
    $stmt =  $mysqli->stmt_init();
    $stmt->prepare("create table aes(value blob)");
    $stmt->execute();
    $stmt->fetch();
    $stmt->close();
    echo "Table 'aes' created\n";
    
    $stmt =  $mysqli->stmt_init();
    if ($stmt->prepare("INSERT INTO aes VALUES(AES_ENCRYPT(?,?))")) {
        $stmt->bind_param("ss", $testvalue, $aeskey);
        $stmt->execute();
        $stmt->close();
    }
    echo "Record inserted\n";
    
    $stmt =  $mysqli->stmt_init();
    if ($stmt->prepare("SELECT AES_DECRYPT(value, ?) FROM aes")) {
        $stmt->bind_param("s", $aeskey);
        $stmt->execute();
        $stmt->bind_result($value);
        $stmt->fetch();
        $stmt->close();
    }
    echo "Your testvalue is: $value\n";
    
    $mysqli->close();
    The output is:
    Table 'aes' created
    Record inserted
    Your testvalue is: sometestvalue

    Comment

    Working...