A client of mine wants clients names to be encrypted in the database, don't ask why they are just intent of it so I have been trying to do MySQL AES_Encrypt and AES_Decrypt using PHP so I would REALLY appreciate some help...
Encrypt Code
function addname ($name, $refid) {
include("../config.php");
// Open up a new MySQLi connection to the MySQL database
mysql_connect($dbHost, $dbUsername, $dbPassword);
mysql_select_db($dbTable);
$code = substr($output, 0, 8);
if (!isset($refid)) {
$refid = "ERROR";
}
$query = "INSERT INTO `clients` (fname, code, refid, active) VALUES (AES_ENCRYPT('$fname', UNHEX('F3229A0B371ED2D9441B830D21A390C3')), '$code', '$refid', 0)";
$runQuery = mysql_query($query);
if ($runQuery != true) {
return mysql_error();
} else {
return $code;
}
}
Decrypt Code
function decryptname() {
$input=947270;
include("config.php");
// Open up a new MySQLi connection to the MySQL database
mysql_connect($dbHost, $dbUsername, $dbPassword);
mysql_select_db($dbTable);
// Build the query
$sqlToRun = "SELECT * FROM `clients` WHERE code='$input' AND active=0";
// Run it
$check = mysql_query($sqlToRun);
while($row = mysql_fetch_array($check)) {
$encryptedname = $row['fname'];
$decryptedname = mysql_query("AES_DECRYPT('$encryptedname', UNHEX('F3229A0B371ED2D9441B830D21A390C3'))");
$check2 = $row['fname'];
}
mysql_close();
if (!isset($check2)) {
$check2 = "wow there is no check2";
}
exit($check2);
}
decryptname();
The Problem
MySQL Database shows the following value, which to be looks normal
e309367d1867c3273a8f8b298ed8beb3
Basically when ever I don't include the $decryptedname I get the following as a output
ã6}gÃ':‹)ŽØ¾³
If I do include it, I get a blank screen and no PHP or MySQL Errors?
Some More Information
The database column structure for names is
varbinary(9999)
If anyone can help me I would really appreciate it, if you need more info please ask!
UPDATE
I ran the following command in SQL and it returned NULL
SELECT AES_DECRYPT('password', "UNHEX('F3229A0B371ED2D9441B830D21A390C3')") FROM passwords WHERE code=947270