BEST PHP ENCRYPTION DECRYPTION MYSQL TUTORIAL | PROACTIVE METHODS

Inserting The Encrypted Data

This is one way to insert encrypted data into your MySQL database.

//GET POST VARIABLES
$firstName=$_POST['firstName'];
$email=$_POST['email'];

//THE ENCRYPTION PROCESS
$nameencrypted=encryptthis($firstName, $key);
$emailencrypted=encryptthis($email, $key);

//INSERT INTO DATABASE
mysqli_query($con,"INSERT INTO people(`name`, `email`)
VALUES ('$nameencrypted','$emailencrypted')");

Retrieving and Decrypting Data From MySQL

Now that you have all the data encrypted and stored in MySQL you will want to retrieve it and display in human-readable format. Here is an example on how we do this.

$result = $con->query("SELECT * FROM people") ;
while ($row = $result->fetch_assoc()) {
echo '<p>'.decryptthis($row['name'], $key).'</p>';
echo '<p>'.decryptthis($row['email'], $key).'</p>';
}

Download Encryption Decryption Scripts With MySQL

Lucky for you, I’m a nice guy and am going to give you the complete scripts for free. Imagine what you would have to pay if you had to pay for such a script? Please consider a donation to my PayPal below. It helps me keep this website free. If I get more donations, I will not have to charge for my next lesson, which will show you how to search an encrypted database for information.

BEST PHP ENCRYPTION DECRYPTION MYSQL TUTORIALDONWLOAD COMPLETE FILES HERE

Image by Pete Linforth on Pixabay

BEST PHP ENCRYPTION DECRYPTION MYSQL TUTORIAL | PROACTIVE METHODS was last modified: October 29th, 2021 by Maximus Mccullough

Summary
BEST PHP ENCRYPTION DECRYPTION MYSQL TUTORIAL | PROACTIVE METHODS
Article Name
BEST PHP ENCRYPTION DECRYPTION MYSQL TUTORIAL | PROACTIVE METHODS
Description
Encryption, Decryption and MySQL in PHP tutorial with codes and video included
Author
Publisher
A1WEBSITEPRO LLC
Logo
BEST PHP ENCRYPTION DECRYPTION MYSQL TUTORIAL

Pages:Previous 1 2

14 Comments

  • Jess Larsen says:

    I get the following error when trying to decrypt: Warning: openssl_decrypt(): IV passed is 17 bytes long which is longer than the 16 expected by selected cipher, truncating in /home/skyttesystem/amager.yyyyyyy.dk/Admin/UserAdm/test4.php on line 24

    The code is as following:

    function decryptthis($data, $key) {
    $encryption_key = base64_decode($key);
    list($encrypted_data, $iv) = array_pad(explode(‘::’, base64_decode($data), 2),2,null);
    XXXX return openssl_decrypt($encrypted_data, ‘aes-256-cbc’, $encryption_key, 0, $iv); XXXX
    }

    The line with XXXX is line 24.

    Hope you can see what I am doing wrong.

    • This should be your decrypt function.

      //DECRYPT FUNCTION
      function decryptthis($data, $key) {
      $encryption_key = base64_decode($key);
      list($encrypted_data, $iv) = array_pad(explode('::', base64_decode($data), 2),2,null);
      return openssl_decrypt($encrypted_data, 'aes-256-cbc', $encryption_key, 0, $iv);
      }

      Also are you calling your key in properly?

  • ZTech MOV says:

    Have you had any troubles with decrypting dates? I had a date field that I encrypted and when trying to decrypt it just returns today’s date/day the I inserted the data. It works fine for DateTime fields.

    foreach($stmt as $row){
    $e = date_create(decryptthis($row[‘*******’],$key));
    echo date_format($e, “Y-m-d”) . ”;
    }
    echo “”;

    returns:

    2021-12-30
    2021-12-30
    2021-12-30
    …..

    • Sorry for the delay. Interesting find that you have but I have not tested it yet. If that is inside a function, you will have to use the Global variable like global $key; in order for it to work. I will test this out when I get some time. ๐Ÿ™‚

  • Panos says:

    Hello sir, i really liked your videos and they seem very helpful. I also subscribed to your YouTube channel plus i followed you on Instagram ! But i have a problem here. I have a simple project and i cannot figure out how to add this functionality to my project. Could you please add it for me if i send you the code ? It’s something very easy and very small ! Thank you very much for your time reading my email.

    • HI Panos, can you post the code here so I can see it? Make sure you let me know what kind of server you are using. You can surround your code in [code]code here[/code] tags. Thanks Maximus

  • HARRY says:

    Hi Maximus – thanks for the tutorial its working great, however can you explain how to Select all records from a database where the email is equal to an encrypted value.
    e.g
    instead of
    $result = $con->query(“SELECT * FROM people”) ;
    I would like to SELECT * FROM people WHERE email = $emailencrypted

  • David says:

    Hi Maximus.

    Thanks for making great tutorials and free scripts!

    I’ve copy-pasted your encryption and decryption scripts and provided my own key, which I get from an included PHP-file. At first it works perfectly, but after a while, when I encrypt some new data, it somehow gets currupted: All other encrypted entries are 56 characters long, but the new one are 60 characters long. Then the decrypt script can’t decrypt the data.
    The key hasn’t changed, the script hasn’t changed, the connection to MySQL hasn’t changed, so… Do you know what is going on?

    • Did you try just putting the key directly back into the script? I’ve been having funny things going on today in my programming. I could not even get an image to show up for the longest time. What happened I think is I copy and pasted my code from the website. It brought in invisible characters. However, I brought up notepad and copy and pasted into that then copied it again and pasted into my notepad plus plus then woo hoo there was an image! lol Funny things do happen. Do you have the script online or are you working locally? Let me know or just shoot it to me in an email. [email protected] and I can try it out here. ๐Ÿ™‚

  • David says:

    Thanks for the fast reply!
    Yeah, I tried putting it in directly into the script.
    I’m still working locally. If I mail it to you, how much du you need? Just the key and the script? Or should I just post it here, and create a new key when we find a sollution? ๐Ÿ˜€

  • sam says:

    please sir decryption doesn’t work on textarea. it works with input fields well. i want to know why?

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.