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”

Pages: [1] 2 3 4 » Show All

  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 …

Pages: [1] 2 3 4 » Show All

Leave a Reply

Spam protection by WP Captcha-Free