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); }
4 Comments
Want more info? Contact me.
Impressive sharing. For the base64 decode, here is the tool, I want to suggest
https://url-decode.com/tool/base64-decode
that tool not only provides base64 decode/encode, but you also get access to 100+ web utilities. That will definitely help, and the reader out, in their future projects.
Shame i cant find the code you show on your video =(
Sorry about that Brad, let me see if I can find it for you!