473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem decrypting data stored in MySQL using mcrypt

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);
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);
mcrypt_generic_deinit ($td);
mcrypt_module_close ($td);
echo "$input<p>$et<p>$plain<p>";

[ 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.

#faÖŸdšK𳵚"‡Á¸•Š‹ßÐt£

#faÖŸdšK𳵚"‡Á¸•Š‹ßÐt£

[ 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.

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.
Jul 17 '05 #1
1 6853

"M Wells" <pl**********@planetthoughtful.org> wrote in message
news:bi********************************@4ax.com...
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);
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);
mcrypt_generic_deinit ($td);
mcrypt_module_close ($td);
echo "$input<p>$et<p>$plain<p>";

[ 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.

#faÖYdsKð³µs"?Á¸.S<ßÐt£

#faÖYdsKð³µs"?Á¸.S<ßÐt£

[ 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.

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.


I can't see where you're going wrong - but if I were you, I'd concentrate on
getting the encryption/decryption working first, before doing anything with
the result to/from MySQL - I am a newbie at MySQL and I'm wondering if the
data being stored is different than that being recovered)... You could try
and do an md5 hash on the encrypted data send to and from the database and
compare them both - If the hashes are different, then the data you are
reading back might look the same ,but are different...

Thats about as much as I can suggest...
Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Madhusudan Singh | last post by:
Hi After months of waiting for Redhat to come out with up to date rpms, I decided to compile a working OpenSSL/ MySQL / PHP / httpd installation for myself. Installed the latest versions of...
12
by: Jerry Weinstein | last post by:
Hi, I know about stored procedures and that they can speed up data entry via the SQL 'insert ' statement. However, one drawback to this method is that using the stored procedure still requires...
5
by: sylvian stone | last post by:
Hi, I'm getting some strange errors that I cannot pin down: Warning: mcrypt_generic_init(): Iv size incorrect; supplied length: 7, needed: 8 in...... This is strange because the data is...
0
by: cmrchs | last post by:
Hi, Using the RSA-crypto algorithm in .NET , when actually encrypting/decrypting, all the code samples I ran into always used something like, to encrypt : // create keypair and store in...
0
by: cmrchs | last post by:
Hi, Using the RSA-crypto algorithm in .NET , when actually encrypting/decrypting, all the code samples I ran into always used something like, to encrypt : ' create keypair and store in...
3
by: neil | last post by:
Hi, I'm not sure whether this is a known or documented issue (I could'nt turn anything up, but maybe I'm not looking hard enough), but I seem to be have a problem encrypting and decrypting...
3
by: Sophisticado | last post by:
Greetings I have a script in which I am collecting sensitive information via a form (METHOD=POST) and encrypting the posted variable (format = BLOB) using mcrypt, then saving it in a MySql...
2
by: Amar | last post by:
Hi All, I want to insert my password into the mysql database by encrypting it so that I can also retrieve the password. Before I was using sha1() for encrypting password,but it is an one way...
5
by: Harb247 | last post by:
Hi Guys, First Post. Need some help decrypting a String / DB The company i manage recently had an argument with their lead programmer for their data base. However he has decided to have nothing...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.