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

Related Posts Plugin for WordPress, Blogger...

27 comments

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

  2. 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?

  3. 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.

  4. 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

  5. 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) ;
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *