PHP Encrypt Decrypt using Base64
Posted by tech on
March 24, 2008
Here are two methods to encrypt and decrypt using Base64. I forgot where I got this from but these 2 methods are pretty handy. Make sure you remember your key as your string will be encrypted and decrypted according to what you specify as key. Key is a string here.
Usage is as follows:
$encrypted = encrypt("to encrypt string", "chitgoks");
$decrypted = decrypt($encrypted, "chitgoks");
$decrypted will return to encrypt string.
function encrypt($string, $key) {
$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) {
$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;
}









September 5th, 2008 at 3:56 am
This is useful. Thank you very much!
September 5th, 2008 at 4:03 am
glad this post helped you. i actually got this code in the net, i just forgot where as it’s been a long time since i got this.
March 6th, 2009 at 5:03 am
Thanks i found this script again, i once use this script in one of the project i made.
March 6th, 2009 at 7:26 am
you are welcome
keep visiting and supporting this blog. thanks
March 18th, 2009 at 2:02 pm
wah tengkyu banget euy …
sip lanjut terus gan …