RE: [PHP] Re: Problem decrypting data stored in MySQL text field using mcrypt?
Hi Jas, Not really -- I can't help thinking there's something about retrieving the encrypted value from the MySQL text field that makes the end result un-decryptable... I was hoping that someone could provide me with a working example that encrypts a value, stores in a MySQL table, then retrieves the value from the table, decrypts it and returns the same text as the original plaintext... But, thank you very, very much for going out of your way to help, all the same... Much warmth, Murray http://www.planetthoughtful.org Building a thoughtful planet, One quirky comment at a time. -Original Message- From: Jas [mailto:[EMAIL PROTECTED] Sent: Monday, 26 January 2004 4:48 PM To: [EMAIL PROTECTED] Subject: [PHP] Re: Problem decrypting data stored in MySQL text field using mcrypt? Jas wrote: [ Here there be snippage ] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Problem decrypting data stored in MySQL text field using mcrypt?
Why not use the AES_ENCRYPT and AES_DECRYPT functions? from the MySQL manual: AES_ENCRYPT() and AES_DECRYPT() were added in version 4.0.2, and can be considered the most cryptographically secure encryption functions currently available in MySQL. SELECT AES_ENCRTPY('my_password_is_easy', 'secretword') -?¤»5Ë#TÓAò=WN¤/¿wøÓpñe¬1d\m, SELECT AES_ENCRTPY(my_column, 'secretword') -my_password_is_easy - Original Message - From: Murray @ PlanetThoughtful.org [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Thursday, January 29, 2004 2:31 PM Subject: RE: [PHP] Re: Problem decrypting data stored in MySQL text field using mcrypt? Hi Jas, Not really -- I can't help thinking there's something about retrieving the encrypted value from the MySQL text field that makes the end result un-decryptable... I was hoping that someone could provide me with a working example that encrypts a value, stores in a MySQL table, then retrieves the value from the table, decrypts it and returns the same text as the original plaintext... But, thank you very, very much for going out of your way to help, all the same... Much warmth, Murray http://www.planetthoughtful.org Building a thoughtful planet, One quirky comment at a time. -Original Message- From: Jas [mailto:[EMAIL PROTECTED] Sent: Monday, 26 January 2004 4:48 PM To: [EMAIL PROTECTED] Subject: [PHP] Re: Problem decrypting data stored in MySQL text field using mcrypt? Jas wrote: [ Here there be snippage ] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Problem decrypting data stored in MySQL text field using mcrypt?
Murray @ Planetthoughtful.Org wrote: Hi All, I'm trying to implement encryption on certain data fields in my MySQL database and I'm experiencing ongoing problems. I seem to be able to encrypt the data without issues, but can't figure out how to decrypt the data. My field in my test table, in which I'm storing the encrypted data, is a TEXT type field. The code I'm using, from the PHP help file, is: [ begin code ] $key = mysecretkey; $input = This is some plain text.; $td = mcrypt_module_open ('tripledes', '', 'ecb', ''); $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND); mcrypt_generic_init ($td, $key, $iv); $encrypted_data = mcrypt_generic ($td, $input); $insq = INSERT into test (entry) VALUES ('$encrypted_data'); mysql_query($insq); mcrypt_generic_deinit ($td); mcrypt_module_close ($td); $query = SELECT entry from test; $res = mysql_query($query); $row = mysql_fetch_array($res); This is your problem... You are assigning a new variable $res to the database contents mysql_free_result($res); $et = $row[entry]; $td = mcrypt_module_open ('tripledes', '', 'ecb', ''); $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND); mcrypt_generic_init ($td, $key, $iv); $plain = mdecrypt_generic ($td, $et); $contents = count($res); for ($z = 0; $z $contents; $z++) { $decrypted[] = mdecrypt_generic($cipher, $res[$z]); } Now just print it out... $count = count($decrypted); for ($m = 0; $m $count; $m++) { print $decrypted[$m]br /\n; } mcrypt_generic_deinit ($td); mcrypt_module_close ($td); echo $inputp$etp$plainp; [ end code ] The output from the echo statement, which I was expecting to be plaintext encrypted text decrypted plaintext, is: [ begin echo output] This is some plain text. #fadKt #fadKt [ end echo output ] I assume some of the characters won't display properly in this post, however I can verify that the encrypted text (line 2) and decrypted plaintext (line 3) appear to be identical, where I was expecting the plaintext (line 1) and decrypted plaintext (line 3) to be identical. Can anyone give me an insight into what I'm doing wrong? I'm using PHP version 4.3.4 and Apache 1.3.28 on WindowsXP, if these are meaningful. Any help will be immensely appreciated! Much warmth, Murray http://www.planetthoughtful.org Building a thoughtful planet, One quirky comment at a time. Hope this helps... If you want to see my example just let me know. On another note if you were using mcrypt on linux vs. windows you would have to do something like this to get rid of a small bug in the decoding process... ? /* Array of data to use in encryption */ $data = array(Who is the man?, Jason, Jason, Who has the plan?, Jason, Jason); /* Define cipher, stream and key */ $key = Some kinda secret; $cipher = mcrypt_module_open('rijndael-256', '', 'ecb', ''); $inv = mcrypt_create_iv(mcrypt_enc_get_iv_size ($cipher), MCRYPT_RAND); mcrypt_generic_init($cipher, $key, $inv); /* Count and begin loop for encryption on every element in array */ $cnt = count($data); $encrypted = $data; $anum = count($encrypted); for ($u = 0; $u $anum; $u++) { array_shift($encrypted); } for ($b = 0; $b $cnt; $b++) { $encrypted[] = mcrypt_generic($cipher, $data[$b]); } /* Now loop over array and decrypt the cipher text */ $decrypted = $encrypted; for ($w = 0; $w $anum; $w++) { array_shift($decrypted); } $wu = count($encrypted); // The chop() function removes some extra padding in the decryption process on linux machines, I didn't experience this problem on windows machines for ($z = 0; $z $wu; $z++) { $decrypted[] = chop(mdecrypt_generic($cipher, $encrypted[$z])); } $inte = count($decrypted); for ($m = 0; $m $inte; $m++) { print $decrypted[$m]br /\n; } ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Problem decrypting data stored in MySQL text field using mcrypt?
Jas wrote: Murray @ Planetthoughtful.Org wrote: Hi All, I'm trying to implement encryption on certain data fields in my MySQL database and I'm experiencing ongoing problems. I seem to be able to encrypt the data without issues, but can't figure out how to decrypt the data. My field in my test table, in which I'm storing the encrypted data, is a TEXT type field. The code I'm using, from the PHP help file, is: [ begin code ] $key = mysecretkey; $input = This is some plain text.; $td = mcrypt_module_open ('tripledes', '', 'ecb', ''); $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND); mcrypt_generic_init ($td, $key, $iv); $encrypted_data = mcrypt_generic ($td, $input); $insq = INSERT into test (entry) VALUES ('$encrypted_data'); mysql_query($insq); mcrypt_generic_deinit ($td); mcrypt_module_close ($td); $query = SELECT entry from test; $res = mysql_query($query); $row = mysql_fetch_array($res); This is your problem... You are assigning a new variable $res to the database contents mysql_free_result($res); $et = $row[entry]; $td = mcrypt_module_open ('tripledes', '', 'ecb', ''); $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND); mcrypt_generic_init ($td, $key, $iv); $plain = mdecrypt_generic ($td, $et); $contents = count($res); for ($z = 0; $z $contents; $z++) { $decrypted[] = mdecrypt_generic($cipher, $res[$z]); } Now just print it out... $count = count($decrypted); for ($m = 0; $m $count; $m++) { print $decrypted[$m]br /\n; } mcrypt_generic_deinit ($td); mcrypt_module_close ($td); echo $inputp$etp$plainp; [ end code ] The output from the echo statement, which I was expecting to be plaintext encrypted text decrypted plaintext, is: [ begin echo output] This is some plain text. #fadKt #fadKt [ end echo output ] I assume some of the characters won't display properly in this post, however I can verify that the encrypted text (line 2) and decrypted plaintext (line 3) appear to be identical, where I was expecting the plaintext (line 1) and decrypted plaintext (line 3) to be identical. Can anyone give me an insight into what I'm doing wrong? I'm using PHP version 4.3.4 and Apache 1.3.28 on WindowsXP, if these are meaningful. Any help will be immensely appreciated! Much warmth, Murray http://www.planetthoughtful.org Building a thoughtful planet, One quirky comment at a time. Hope this helps... If you want to see my example just let me know. On another note if you were using mcrypt on linux vs. windows you would have to do something like this to get rid of a small bug in the decoding process... ? /* Array of data to use in encryption */ $data = array(Who is the man?, Jason, Jason, Who has the plan?, Jason, Jason); /* Define cipher, stream and key */ $key = Some kinda secret; $cipher = mcrypt_module_open('rijndael-256', '', 'ecb', ''); $inv = mcrypt_create_iv(mcrypt_enc_get_iv_size ($cipher), MCRYPT_RAND); mcrypt_generic_init($cipher, $key, $inv); /* Count and begin loop for encryption on every element in array */ $cnt = count($data); $encrypted = $data; $anum = count($encrypted); for ($u = 0; $u $anum; $u++) { array_shift($encrypted); } for ($b = 0; $b $cnt; $b++) { $encrypted[] = mcrypt_generic($cipher, $data[$b]); } /* Now loop over array and decrypt the cipher text */ $decrypted = $encrypted; for ($w = 0; $w $anum; $w++) { array_shift($decrypted); } $wu = count($encrypted); // The chop() function removes some extra padding in the decryption process on linux machines, I didn't experience this problem on windows machines for ($z = 0; $z $wu; $z++) { $decrypted[] = chop(mdecrypt_generic($cipher, $encrypted[$z])); } $inte = count($decrypted); for ($m = 0; $m $inte; $m++) { print $decrypted[$m]br /\n; } ? Sorry but I was just wondering if this solved your problem or not? Jas -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php