PHP Encrypt Decrypt using Base64

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;
}

Donations appreciated. Every little $ helps. Or click Google +1

Related Posts with Thumbnails

20 Responses to “PHP Encrypt Decrypt using Base64”

  1. 1
    Bighand Says:

    This is useful. Thank you very much!

  2. 2
    tech Says:

    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.

  3. 3
    tagapandan Says:

    Thanks i found this script again, i once use this script in one of the project i made.

  4. 4
    tech Says:

    you are welcome ;) keep visiting and supporting this blog. thanks

  5. 5
    aagun2006 Says:

    wah tengkyu banget euy …
    sip lanjut terus gan …

  6. 6
    Ubernerd Says:

    This is a great piece of work. Got a PayPal donation area?

  7. 7
    Ubernerd Says:

    Never mind - found that donation box and sent some. Thanks, I hope many people send you donations for your sharing.

  8. 8
    tech Says:

    thanks ubernerd!

    you are the second one to give a donation in this blog. thank you!

  9. 9
    Ems Says:

    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?

  10. 10
    tech Says:

    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

  11. 11
    Xavier Petzoldt Says:

    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.

  12. 12
    Nadeem Says:

    Nice piece of code have resolve my problem. I just looking for this. Thumbs up.

  13. 13
    tech Says:

    thanks! do subscribe to my rss feeds

  14. 14
    Simple PHP Says:

    You’ve got great insights about PHP, keep up the good work!

  15. 15
    Jombs Says:

    Thank you, thank you! Just what was needed! +1 for you.

  16. 16
    Lalit Says:

    Nice code thanks for help.

  17. 17
    tech Says:

    @jombs: you’re welcome. do +1 this post

  18. 18
    Ragu Says:

    Nice Job…

    Continue……………

  19. 19
    mangal Says:

    Nice job…….

  20. 20
    sam Says:

    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

Leave a Reply

Spam protection by WP Captcha-Free