Sometime we need to convert PHP strings to secret code which cannot be decoded easily by user. This method will help us to pass coded strings with URL GET arguments and also inside Hidden Form fields ..
Eg : http://myserver.com/index.php?username=j345kjh656bthbf6
PHP has inbuilt base64 encocde and base64 decode functions . but anyone can easily decrypt the string by passing it to base64 decoded functions .. So we need our own encode and decode function so that user cant identify the original content .
Simple encryption and decryption function is given below
Assign $key with your own secretpasskey
The string encrypted using function encrypt can be decoded only using decrypt function .
For extra security we have included base64 encryption also.
function encrypt($string, $key='mysecretcode123')
{
$result = '';
for($i=0; $i<strlen($string); $i++)
{
$char = substr($string, $i, 1);
$keychar = substr($key, ($i % strlen($key))-1, 1);
$char = chr(ord($char)+ord($keychar));
$result.=$char;
}
return base64_encode($result);
}
function decrypt($string, $key=' mysecretcode123 ')
{
$result = '';
$string = base64_decode($string);
for($i=0; $i<strlen($string); $i++)
{
$char = substr($string, $i, 1);
$keychar = substr($key, ($i % strlen($key))-1, 1);
$char = chr(ord($char)-ord($keychar));
$result.=$char;
}
return $result;
}
PHP Encryption and Decryption Function
