Usage:
- Code: Select all
encrypt(’Welcome to GeoSourceCode.com’,’secretkey’);
This will show:
0NjRxuHS2YvZ6JPVy-LJ3czX8qHU1dk=
Now, let’s reverse it.
- Code: Select all
decrypt(’0NjRxuHS2YvZ6JPVy-LJ3czX8qHU1dk=’,’secretkey’);
This will show:
Welcome to GeoSourceCode.com
See, it works! And now the code..
- Code: Select all
<?
function encrypt($sData, $sKey)
{
$sResult = '';
for($i = 0; $i < strlen($sData); $i ++)
{
$sChar = substr($sData, $i, 1);
$sKeyChar = substr($sKey, ($i % strlen($sKey)) - 1, 1);
$sChar = chr(ord($sChar) + ord($sKeyChar));
$sResult .= $sChar;
}
return encode_base64($sResult);
}
function decrypt($sData, $sKey)
{
$sResult = '';
$sData = decode_base64($sData);
for($i = 0; $i < strlen($sData); $i ++)
{
$sChar = substr($sData, $i, 1);
$sKeyChar = substr($sKey, ($i % strlen($sKey)) - 1, 1);
$sChar = chr(ord($sChar) - ord($sKeyChar));
$sResult .= $sChar;
}
return $sResult;
}
function encode_base64($sData)
{
$sBase64 = base64_encode($sData);
return strtr($sBase64, '+/', '-_');
}
function decode_base64($sData)
{
$sBase64 = strtr($sData, '-_', '+/');
return base64_decode($sBase64);
}
?>
And if you may be curious to know what are the different characters that the encrypt() function may return, here is the list:
A-Z
a-z
0-9
-
_
=
Therefore your encrypted data will be URL and filename safe.