Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5840361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:43:59+00:00 2026-05-22T11:43:59+00:00

I’m storing a sensitive, 16 character user string in a MySQL table via the

  • 0

I’m storing a sensitive, 16 character user string in a MySQL table via the PDO functions within PHP5. I have a pair of native encryption/decryption functions as follows:

function encrypt($in)
{

    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $enc = mcrypt_encrypt(MCRYPT_RIJNDAEL_256,ENCRYPT_KEY, $in, MCRYPT_MODE_ECB, $iv);

    return $enc;
}

function decrypt($in)
{

    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $dec = mcrypt_decrypt(MCRYPT_RIJNDAEL_256,ENCRYPT_KEY, $in, MCRYPT_MODE_ECB, $iv);

    return $dec;
}

Where ENCRYPT_KEY is a previously defined global constant.

I firstly encrypt() the string, then INSERT the record into the nominated table. Every so often, I need to cycle through this information via a SELECT statement, then decrypt() the results for processing.

Now, this happens perfectly for every case except one.

On the select/decryption cycle, one (and only) record from about 50 decrypts as gobbledy-goop. I cannot see anything wrong with the encrypt() and decrypt() functions, and have repeatedly INSERTed the record in question with the same results. A call like below:

echo decrypt(encrypt($string));

Works fine. Therefore, the only thing that I have come up with is that MySQL is failing to correctly store the encrypted version of this particular string, but I’m at a loss as to why. The storage function is as follows:

function update_sensitive_details($sensitive)
{
    $this->store_sensitive = encrypt($sensitive);

    try
    {
        $sql = "UPDATE table SET store_sensitive = ? WHERE (id = ?);";
        $sth = $this->registry->db->prepare($sql);
        $sth->execute(array($this->store_sensitive,$this->id));
    }
    catch (PDOException $p)
    {
        log_error($p);
        return false;
    }

    return true;
}

This does not result in any errors, and I can confirm that this does in fact update the table with data (although as it is encrypted it is unintelligable when viewed in phpMyAdmin.

Does anyone have any ideas about what might be going on here? I’m stumped. The only thing I can think of is that the particular encrypted string isn’t being properly stored by MySQL, as even changing one of the sixteen characters fixes the problem. I would have thought this would have been prevented by PDO, but perhaps not.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:44:00+00:00Added an answer on May 22, 2026 at 11:44 am

    Try wrapping your encrypted data with base64_encode() and then base64_decode() before decrypting it. I’ve had data corrupt in MySQL in some instances and this has always fixed it.

    function encrypt($in)
    {
    
        $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
        $enc = mcrypt_encrypt(MCRYPT_RIJNDAEL_256,ENCRYPT_KEY, $in, MCRYPT_MODE_ECB, $iv);
        $enc = base64_encode($enc);
        return $enc;
    }
    
    function decrypt($in)
    {
        $in = base64_decode($in);
        $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
        $dec = mcrypt_decrypt(MCRYPT_RIJNDAEL_256,ENCRYPT_KEY, $in, MCRYPT_MODE_ECB, $iv);
    
        return $dec;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I want to count how many characters a certain string has in PHP, but
Does anyone know how can I replace this 2 symbol below from the string
For some reason, after submitting a string like this Jack’s Spindle from a text
i got an object with contents of html markup in it, for example: string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.