Problem decrypting data stored in MySQL using mcrypt

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • M Wells

    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 = "mysecretke y";
    $input = "This is some plain text.";
    $td = mcrypt_module_o pen ('tripledes', '', 'ecb', '');
    $iv = mcrypt_create_i v (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_da ta')";
    mysql_query($in sq);
    mcrypt_generic_ deinit ($td);
    mcrypt_module_c lose ($td);
    $query = "SELECT entry from test";
    $res = mysql_query($qu ery);
    $row = mysql_fetch_arr ay($res);
    mysql_free_resu lt($res);
    $et = $row["entry"];
    $td = mcrypt_module_o pen ('tripledes', '', 'ecb', '');
    $iv = mcrypt_create_i v (mcrypt_enc_get _iv_size ($td), MCRYPT_RAND);
    mcrypt_generic_ init ($td, $key, $iv);
    $plain = mdecrypt_generi c ($td, $et);
    mcrypt_generic_ deinit ($td);
    mcrypt_module_c lose ($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

    Building a thoughtful planet,
    One quirky comment at a time.
  • Randell D.

    #2
    Re: Problem decrypting data stored in MySQL using mcrypt


    "M Wells" <planetquirky@p lanetthoughtful .org> wrote in message
    news:bit210liid vt9vvcio7lrt1o9 37prf4vdb@4ax.c om...[color=blue]
    > 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 = "mysecretke y";
    > $input = "This is some plain text.";
    > $td = mcrypt_module_o pen ('tripledes', '', 'ecb', '');
    > $iv = mcrypt_create_i v (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_da ta')";
    > mysql_query($in sq);
    > mcrypt_generic_ deinit ($td);
    > mcrypt_module_c lose ($td);
    > $query = "SELECT entry from test";
    > $res = mysql_query($qu ery);
    > $row = mysql_fetch_arr ay($res);
    > mysql_free_resu lt($res);
    > $et = $row["entry"];
    > $td = mcrypt_module_o pen ('tripledes', '', 'ecb', '');
    > $iv = mcrypt_create_i v (mcrypt_enc_get _iv_size ($td), MCRYPT_RAND);
    > mcrypt_generic_ init ($td, $key, $iv);
    > $plain = mdecrypt_generi c ($td, $et);
    > mcrypt_generic_ deinit ($td);
    > mcrypt_module_c lose ($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.[/color]

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


    Comment

    Working...