Hi I need to encrypt the data sent by URL, using base64 but had trouble decrypting. I want to use encodeURL And DECODEURL properly considering special characters and spaces
|
Jun '16 |
4 |
1287 |
0 |
What you need first is to provide more details. You use GET http method for data exchange, where data is encrypted in a binary form and encoded with base64, and what exact issue do you have?
$encoded = urlencode( base64_encode( $str ) );
$decoded = base64_decode( urldecode( $encoded ) );
Here are simple functions that i use to transfer encoded information. It is url safe.
private static $cryptKey = 'i$a^&/:%2_z10ROQ<@{(e=*!<7u|rI~0';
public static function encodeString($string)
{
$crypt = new \Phalcon\Crypt();
return $crypt->encryptBase64($string, self::$cryptKey, true);
}
public static function decodeString($string)
{
$crypt = new \Phalcon\Crypt();
return $crypt->decryptBase64($string, self::$cryptKey, true);
}
And example of course:
$string = 'Hello it is me Nikolay';
$encoded = \Helpers\Tools::encodeString($string);
echo $encoded; // EfLYhEcnsGUiXZ8LZ30uH2DuZMfVmcmuDuZm-tG8w1QO2zDPhkiDf315J11v001OeEJAjS6CwiDB7Ve8B_zp7g==
$decoded = \Helpers\Tools::decodeString($encoded);
echo $decoded; // Hello it is me Nikolay
You can also avoid base64, and use hexadecimal notation/convert both binary and text data and send it via HTTP GET.
//Before send
$str = 'This is binary data or \0 terminated array of bytes.';
//convert it to hexadecimal
$hex = bin2hex($str); //send $hex via HTTP GET method
// ###########################################
//After fetch
$decodeHex = pack('H*', $hex); //or simply $_GET['data'] instead of my test $hex variable
I use this approach while connecting to mainframe upstream services which does not support base64 or json :)
Hexadecimal notation is supported on any platform out there.
In the same php file it has always worked well. The problem is when e-url special characters
Here are simple functions that i use to transfer encoded information. It is url safe.
private static $cryptKey = 'i$a^&/:%2_z10ROQ<@{(e=*!<7u|rI~0'; public static function encodeString($string) { $crypt = new \Phalcon\Crypt(); return $crypt->encryptBase64($string, self::$cryptKey, true); } public static function decodeString($string) { $crypt = new \Phalcon\Crypt(); return $crypt->decryptBase64($string, self::$cryptKey, true); }
And example of course:
$string = 'Hello it is me Nikolay'; $encoded = \Helpers\Tools::encodeString($string); echo $encoded; // EfLYhEcnsGUiXZ8LZ30uH2DuZMfVmcmuDuZm-tG8w1QO2zDPhkiDf315J11v001OeEJAjS6CwiDB7Ve8B_zp7g== $decoded = \Helpers\Tools::decodeString($encoded); echo $decoded; // Hello it is me Nikolay