1- Crypt your var
2- Make sure to encode correctly with base64 MIME.
3- Do what you want (example : store in your database in order to decrypt later, pass into GET etc ...)
4- Decode base64 safely your var.
5- Decrypt your var
I implemented a class which does the job. (security and data hiding)
Use openssl method with aes-256 mode cbc to secure crypt (don't forget initialization vector)
class Encryption{
public static function safe_b64encode($string='') {
$data = base64_encode($string);
$data = str_replace(['+','/','='],['-','_',''],$data);
return $data;
}
public static function safe_b64decode($string='') {
$data = str_replace(['-','_'],['+','/'],$string);
$mod4 = strlen($data) % 4;
if ($mod4) {
$data .= substr('====', $mod4);
}
return base64_decode($data);
}
public static function encode($value=false){
if(!$value) return false;
$iv_size = openssl_cipher_iv_length('aes-256-cbc');
$iv = openssl_random_pseudo_bytes($iv_size);
$crypttext = openssl_encrypt($value, 'aes-256-cbc', 'your security cipherSeed', OPENSSL_RAW_DATA, $iv);
return self::safe_b64encode($iv.$crypttext);
}
public static function decode($value=false){
if(!$value) return false;
$crypttext = self::safe_b64decode($value);
$iv_size = openssl_cipher_iv_length('aes-256-cbc');
$iv = substr($crypttext, 0, $iv_size);
$crypttext = substr($crypttext, $iv_size);
if(!$crypttext) return false;
$decrypttext = openssl_decrypt($crypttext, 'aes-256-cbc', 'your security cipherSeed', OPENSSL_RAW_DATA, $iv);
return rtrim($decrypttext);
}
}
Example :
$pass_get = 'hello';
$base64_crypt = Encryption::encode($pass_get); // get base64 of crypt data
// Later pass into $_GET for example
<a href="https://toto.com?v=<?php echo $base64_crypt;?>" >Other page</a>
// In your other page, recover your var
$my_get_crypt_var = $_GET['v'];
Encryption::decode($my_get_crypt_var); // return 'hello' or false in case the string to be decrypted is invalid.
!!! This solution is not hashing, but CRYPTING ! So, it means that you can recover the content of your var. Can be used for no sensitive data, but not for password for example. !!!