1 Star2 Stars3 Stars4 Stars5 Stars (17 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 be sure to use over and over again the future. This takes all the data that 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 your 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);
}

Example of Php Encryption and Decryption

Here is a full working example of the above method in action. Feel free to use this but make sure that you use a different key.

$key = 'qkwjdiw239&&jdafweihbrhnan&^%$ggdnawhd4njshjwuuO';

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

if(isset($_POST['submit'])){
$data=$_POST['foo'];
$encrypted=encryptthis($data, $key);
$decrypted=decryptthis($encrypted, $key);
echo '<h2>Original Data</h2>';
echo '<p>'.$data.'</p>';
echo '<h2>Encrypted Data</h2>';
echo '<pre>'.$encrypted.'</pre>';
echo '<h2>Decrypted Data</h2>';
echo '<p>'.$decrypted.'</p>';
}

echo '<form method="post">
<input type="text" name="foo">
<input type="submit" name="submit" value="submit">
</form>';

 

Subscribe To My YouTube Channel:
Data Encryption Php MySql Methods Implementation Open SSL Encrypt was last modified: February 23rd, 2019 by Maximus Mccullough

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

Tips

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

Pages:Previous 1 2

2 Comments

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.