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;
}
This is useful. Thank you very much!
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.
Thanks i found this script again, i once use this script in one of the project i made.
you are welcome 😉 keep visiting and supporting this blog. thanks
wah tengkyu banget euy …
sip lanjut terus gan …
This is a great piece of work. Got a PayPal donation area?
Never mind - found that donation box and sent some. Thanks, I hope many people send you donations for your sharing.
thanks ubernerd!
you are the second one to give a donation in this blog. thank you!
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?
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
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.
Nice piece of code have resolve my problem. I just looking for this. Thumbs up.
thanks! do subscribe to my rss feeds
You’ve got great insights about PHP, keep up the good work!
Thank you, thank you! Just what was needed! +1 for you.
Nice code thanks for help.
@jombs: you’re welcome. do +1 this post
Nice Job…
Continue……………
Nice job…….
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
Very old piece of code, but I’m happy I found it. It does the job very well! Thanks.
this crypt is so usefull for me thank you so much
It’s working fine. Thanks a lot
Why not just use mcrypt?
How’s this?
function encrypt($plaintext) { // Returns encrypted version of plaintext, prefixed by the initialisation vector.
if( ($plaintext != “”) && ($plaintext != NULL) ) {
global $sysconf ;
$key = $sysconf[‘cfg_cryptKey’] ;
$iv = mcrypt_create_iv(8, MCRYPT_RAND) ;
$plaintext = base64_encode($plaintext);
$enc = mcrypt_encrypt(MCRYPT_BLOWFISH, $key, $plaintext, MCRYPT_MODE_CBC, $iv) ;
$encrypted = base64_encode($iv . $enc) ;
return($encrypted) ;
}
}
/****************************************************************************************/
function decrypt($cipher) { // Cipher is encrypted text, prefixed by initialisation vector.
if( ($cipher != “”) && ($cipher != NULL) ) {
global $sysconf ;
$key = $sysconf[‘cfg_cryptKey’] ;
$cipher=base64_decode($cipher);
$cipherText = substr($cipher, 8) ;
$iv = substr($cipher, 0, 8) ;
$plainText = mcrypt_decrypt(MCRYPT_BLOWFISH, $key, $cipherText, MCRYPT_MODE_CBC, $iv) ;
$plainText = base64_decode($plainText) ;
return($plainText) ;
}
}
@w3code: my post is an old code. if your mcode is much more effective, then the better. thanks for sharing this.
great post, keep going
that’s so much simple syntax. even nubie can easily understand it. wait for another tips and tricks.