PHP Data Encryption is important to safety and privacy. In this lesson, I am going to show you how to insert and retrieve encrypted data in PHP using a MySQL database. Using these methods will make your websites and applications more secure. After completing this tutorial then you will want to go over How To Search PHP Encrypted Database.
PHP DATA ENCRYPTION INSERT RETRIEVE MYSQL DATABASE
In an earlier PHP Data Encryption lesson, I showed you guys how to use OpenSSL to encrypt and decrypt your data. In this lesson, I will show you how to store it and retrieve it from a MySQL Database.
Code Example
function encryptthis($data, $key) { $encryption_key = base64_decode($key); $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc')); $encrypted = openssl_encrypt($data, 'aes-256-cbc', $encryption_key, 0, $iv); return base64_encode($encrypted . '::' . $iv); } 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); }
Create Database For Encryption
Create a database to insert your encrypted data. Name the database “users” in your phpmyadmin dashboard. Then create a table called people. Next, make 4 columns in the table id, name, email, reg_date. Set the name and email fields to TEXT. Do not use varchar because varchar will only give you 255 characters and you may need more than that for your encrypted code. We will use this database to insert and retrieve the data.
CREATE TABLE `people` ( `id` int(11) NOT NULL, `name` text NOT NULL, `email` text NOT NULL, `reg_date` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Create A Config File
You need to create a config file to connect to your database. This will let us insert our encrypted data and retrieve our encrypted data.
Inserting PHP Encrypted Data
To insert your PHP Encrypted Data into the database you created from the information above, use this code.
mysqli_query($con,"INSERT INTO people(`name`, `email`)VALUES ('$name','$email')"); echo '<div class="alert alert-success"> You entered successfully</div>';
Header.php File
<?php date_default_timezone_set('America/New_York'); include_once('functions.php'); ?> <html> <head> <title>PHP ENCRYPTION DECRYPTION MYSQL</title> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" type="text/css" > <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css"> </head> <body> <div class="container">
Footer.php File
</div> </body> </html> <?php $con->close(); ?>
Copy all the files here and set them up on your server. Follow the video instructions and if you have any questions, ask them below.
14 Comments
thanks for doing this
You are welcome 🙂
Doesn’t seem like zip files exist. Do you have them on github?
I put the codes back in the post so you can copy them. Let me know if you have any more questions.
decryption function doesn’t work, I got this error
Warning: base64_decode() expects parameter 1 to be string, array given in C:\xampp\htdocs\X\test.php on line 29
bool(false)
I have it working on several applications currently so it may be in your setup.
header and footer file
are you asking for the header and footer file?
Hello Maximus, can you show us the header and footer file please ? Thank you.
The header and footer just have the basic html and body open and close tags. Nothing special
Hey Max, can i know how did you get/generated that key ?
I just typed in random numbers and charectors
Is this working in log in system?
No it is an encryption system. I have a log in tutorial here and here