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 …
October 5th, 2009 at 9:30 am
This is a great piece of work. Got a PayPal donation area?
October 5th, 2009 at 9:33 am
Never mind - found that donation box and sent some. Thanks, I hope many people send you donations for your sharing.
October 5th, 2009 at 9:37 am
thanks ubernerd!
you are the second one to give a donation in this blog. thank you!
January 24th, 2010 at 1:27 am
This does not seem to work when the string being encoded has a line feed.
Line feeds were converted to space after decoding. Any fix for that?
January 24th, 2010 at 2:09 am
ems. not as of the moment but perhaps you can convert your line feed to some other characters and convert it back once you have it decrypted
April 11th, 2010 at 4:33 am
Wonderful piece of content, this is very similar to a site that I have. Please check it out sometime and feel free to leave me a comenet on it and tell me what you think. I’m always looking for feedback.
September 4th, 2010 at 6:34 pm
Nice piece of code have resolve my problem. I just looking for this. Thumbs up.
September 4th, 2010 at 6:36 pm
thanks! do subscribe to my rss feeds
June 21st, 2011 at 12:34 pm
You’ve got great insights about PHP, keep up the good work!
July 13th, 2011 at 8:15 am
Thank you, thank you! Just what was needed! +1 for you.
July 20th, 2011 at 8:20 am
Nice code thanks for help.
July 20th, 2011 at 8:25 am
@jombs: you’re welcome. do +1 this post
September 16th, 2011 at 5:08 am
Nice Job…
Continue……………
November 25th, 2011 at 12:24 am
Nice job…….
December 29th, 2011 at 3:12 pm
Hi, this works great but i am using the encrypted string to form part of a URL address and for some encryption a ‘+’ symbol is included which causes an error is there a way to stop ‘+’ symbols being used in the encryption?
Thanks in advance