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.

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: April 29th, 2023 by Maximus Mccullough
Data Encryption Php MySql Methods Implementation Open SSL Encrypt

Pages:Previous 1 2 3 Next

4 Comments

Leave a Reply

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