1 Star2 Stars3 Stars4 Stars5 Stars (19 votes, average: 5.00 out of 5)

Data Encryption Php MySql Methods Implementation Open SSL Encrypt

Encrypt Function in PHP

The magic happens with the encrypt function in Php. Here is a little gem that you will use repeatedly in the future. This takes all the data you entered and encrypts it. You will want to use this to encrypt all your private data before inserting it into a database.  We use base 64 decode to begin the operation. I know what you’re thinking, that you can just go to some decode website like this and decode the string.  Well, think again my friend because we are going to use open random ssl pseudo bytes to make this string as complicated as possible. Then we are going to use openssl encrypt. Next we user aes-256-cbc which is NOT CRACKABLE without the key. Finally, we return the base 64 encode encrypted string and surprise. We have an encrypted string.

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);

Decrypt Php Function

All of this means nothing if you cannot decrypt your string, right? Here is how you decrypt your string with a Php function. You will want to save this code snippet in your security arsenal as well. Once again we decode using base 64. Notice we have our key in there as well. Then we use the list function in PHP to interpret our array. Then we use openssl decrypt to decrypt our string with our key.

Tip: Click on images to make them larger. If you find anything wrong on this page or need further assistance please comment below or contact me. 🙂
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);

Data Encryption Php MySql Methods Implementation Open SSL Encrypt was last modified: January 11th, 2021 by Maximus Mccullough

Subscribe To My YouTube Channel:

Please like, share and subscribe.

Advanced Programming Made Easy

I hope you enjoyed the article. If I was able to help you please consider a tip for the content.

One Time Tip

more tips

Reoccuring Tips


Cool People Share:
Data Encryption Php MySql Methods Implementation Open SSL Encrypt

Pages:Previous 1 2 3 Next


Leave a Reply

Your email address will not be published. Required fields are marked *

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