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 9192255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:52:41+00:00 2026-06-17T20:52:41+00:00

In PHP, i have a action.php page, i have a string which is stored

  • 0

In PHP, i have a action.php page, i have a string which is stored into a variable

$myString = "Foo Bar";

I’m encrypting this string using AES256 with the following function :

function aes256Encrypt($key, $data) {

if(32 !== strlen($key)) $key = hash('SHA256', $key, true);

$padding = 16 - (strlen($data) % 16);

$data .= str_repeat(chr($padding), $padding);

return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC,    str_repeat("\0", 16));

}

And before inserting data to MySQL database, I’m using mysql_real_escape_string() to the encrypted string before inserting it to DB

In my knowledge, the mysql_real_escape_string() function adds \ before the charachters that need to be escaped !

In the other hand, i need to get the data back from MySQL and decrypt it using this function :

  function aes256Decrypt($key, $data) {

  if(32 !== strlen($key)) $key = hash('SHA256', $key, true);

  $data = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC,   str_repeat("\0", 16));

  $padding = ord($data[strlen($data) - 1]);

  return substr($data, 0, -$padding);

  }

Everything is OK, when i decrypt the string that i get from db, i get the correct string back, but I get really confused on how could this work correctly without un-escaping the string back after getting it from DB ?

  • 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-06-17T20:52:43+00:00Added an answer on June 17, 2026 at 8:52 pm

    First of all, you should really switch from using mysql_* to either PDO or mysqli and use properly parameterized statements. Then, you avoid calls to mysql_real_escape_string altogether.

    The escaping concept is very broad in programming, and it applies here. Say you want to store someone’s name to the DB: Coryn O'Driscoll. This is not even malicious, but this query will fail:

    INSERT INTO Names VALUES ('Coryn O'Driscoll')
    

    You have an unclosed string.

    mysql_real_escape_string converts this to ('Coryn O\'Driscoll'). The \' signals to mysql that the apostrophe is not a part of the syntax of the query but actuall part of the scalar value to insert. It is actually inserted as Coryn O'Driscoll. This is why you don’t have to remove any backslashes from it when you select it again.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a php-script i have a form, method is post, action-attribute is empty, which
I have this code: function submitTwice(f){ f.action = 'http://mydomain.com/threevideopage.php'; f.target='name'; f.submit(); } I left
I have a post html form, and the action is index.php, which is the
I have this HTML: <form action='uploadhandle.php' method='POST' enctype=multipart/form-data> <input type='file' class='fileinput' id='photo1' name='photo1'> <input
I have form like this: <form method=POST action=<?php echo base_url() ?>admin/admin_search> <fieldset> <label for=nalozi>Nalozi</label><input
I have a php-ajax page that where user writes search string in search.php ,
I have form in my html page <form id=login_form method=POST action=index.php> <table> <tr> <td><label
Very basic seeming PHP question here... I have a page to which I'm already
I have an action from a controller that provides a result variable via $this->set('found_products',
I have a form named reg.php and its action is reg.php , i want

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.