Created
March 27, 2011 01:54
-
-
Save ramimassoud/888838 to your computer and use it in GitHub Desktop.
Example of using PHP's mcrypt to decrypt data that was encrypted using MySQL's AES_ENCRYPT()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$con = mysql_connect('localhost', 'dbUser', 'dbPassword'); | |
$key = '1234567890123456'; | |
$table = "CREATE TABLE people ( | |
id INT NOT NULL AUTO_INCREMENT, | |
full_name VARCHAR(255), | |
ssn VARCHAR(255), | |
PRIMARY KEY (id) | |
)"; | |
$sampleData1 = "INSERT INTO people (full_name, ssn) VALUES ('User One', '111-11-1111')"; | |
$sampleData2 = "INSERT INTO people (full_name, ssn) VALUES ('User Two', '222-22-2222')"; | |
$sampleData3 = "INSERT INTO people (full_name, ssn) VALUES ('User Three', '333-33-3333')"; | |
$sampleData4 = "INSERT INTO people (full_name, ssn) VALUES ('User Four', '444-44-4444')"; | |
$sampleData5 = "INSERT INTO people (full_name, ssn) VALUES ('User Five', '555-55-5555')"; | |
$encryptSql = "UPDATE people SET ssn = AES_ENCRYPT(ssn, '$key')"; | |
mysql_select_db('testing', $con); | |
mysql_query($table, $con); | |
mysql_query($sampleData1, $con); | |
mysql_query($sampleData2, $con); | |
mysql_query($sampleData3, $con); | |
mysql_query($sampleData4, $con); | |
mysql_query($sampleData5, $con); | |
mysql_query($encryptSql, $con); | |
$query = 'SELECT id, full_name, ssn FROM people'; | |
$result = mysql_query($query, $con); | |
for ($i=0;$i<4;$i++) { | |
$id = mysql_result($result, $i, 'id'); | |
$name = mysql_result($result, $i, 'full_name'); | |
$ssn = mysql_result($result, $i, 'ssn'); | |
echo "First we have person $id named $name with encrypted SSN $ssn\n"; | |
echo "Now the decrypted version ", mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $ssn, MCRYPT_MODE_ECB), "\n"; | |
} | |
mysql_query('DROP TABLE people', $con); | |
mysql_close(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment