1-
2- , base64 MIME.
3- , (: , , GET ..)
4- base64 var.
5-
I implemented a class that does this work. (security and data hiding) Use the openssl method with aes-256 cbc mode to protect the crypt (do not forget the 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);
// Later, let's move on to $ _GET for example
<a href="https://toto.com?v=<?php echo $base64_crypt;?>" >Other page</a>
// On another page, restore 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.
source
share