Code

Coding, Programming & Algorithms, Tips, Tweaks & Hacks
Search

Encrypt and Decrypt strings in URL in PHP 7 using openssl

Here's a dead simple solution to pass through encrypted data as a value in your URL. Say you wanted to load something like https://my-product.com/loadProduct.php?id=1234 but don't want to show the real id as it may be the primary value in your database's table.

Instead, you can load it like this : https://my-product.com/loadProduct.php?id=33797030395539366e55673d and decrypt the value in your script.
PHP

<?php
define("ENCRYPTION_KEY", "123456*");
function encrypt($string) { return bin2hex(openssl_encrypt($string, 'BF-ECB', ENCRYPTION_KEY)); }
function decrypt($string) { return openssl_decrypt(hex2bin($string),'BF-ECB',ENCRYPTION_KEY); }
?>
PHP 7.2
More details on security : https://stackoverflow.com/a/50373095/126833
Vanakkam !

0 comments: