Encrypt/Decrypt Functions in PHP

This sections will contains code regarding different PHP & MySQL features and contains help about php/mysql issues.

Encrypt/Decrypt Functions in PHP

Postby Web Guru on April 8th, 2008, 11:54 am

A neat, simple and secure encrypt decrypt function that is URL safe. (meaning you can transport the encrypted string through a http get or post). Since this is based on the base64_encode() function, you should expect the encoded data to take about 33% more space than the original data.

Usage:
Code: Select all
encrypt(’Welcome to GeoSourceCode.com’,’secretkey’);


This will show:
0NjRxuHS2YvZ6JPVy-LJ3czX8qHU1dk=


Now, let’s reverse it.
Code: Select all
decrypt(’0NjRxuHS2YvZ6JPVy-LJ3czX8qHU1dk=’,’secretkey’);


This will show:
Welcome to GeoSourceCode.com


See, it works! And now the code..

Code: Select all
<?
   function encrypt($sData, $sKey)
   {
      $sResult = '';

      for($i = 0; $i < strlen($sData); $i ++)
      {
         $sChar    = substr($sData, $i, 1);
         $sKeyChar = substr($sKey, ($i % strlen($sKey)) - 1, 1);
         $sChar    = chr(ord($sChar) + ord($sKeyChar));
         $sResult .= $sChar;
      }

      return encode_base64($sResult);
   }

   function decrypt($sData, $sKey)
   {
      $sResult = '';
      $sData   = decode_base64($sData);

      for($i = 0; $i < strlen($sData); $i ++)
      {
         $sChar    = substr($sData, $i, 1);
         $sKeyChar = substr($sKey, ($i % strlen($sKey)) - 1, 1);
         $sChar    = chr(ord($sChar) - ord($sKeyChar));
         $sResult .= $sChar;
      }

      return $sResult;
   }

   function encode_base64($sData)
   {
      $sBase64 = base64_encode($sData);

      return strtr($sBase64, '+/', '-_');
   }

   function decode_base64($sData)
   {
      $sBase64 = strtr($sData, '-_', '+/');

      return base64_decode($sBase64);
   }
?>


And if you may be curious to know what are the different characters that the encrypt() function may return, here is the list:
A-Z
a-z
0-9
-
_
=

Therefore your encrypted data will be URL and filename safe.
User avatar
Web Guru
 
Posts: 75
Joined: March 24th, 2008, 7:59 am
Location: Lahore, Pakistan

Re: Encrypt/Decrypt Functions in PHP

Postby lia on June 19th, 2008, 12:58 pm

source code a algorithm AES or RSA with PHP??
lia
 
Posts: 1
Joined: June 19th, 2008, 12:44 pm

Re: Encrypt/Decrypt Functions in PHP

Postby MT Shahzad on June 25th, 2008, 5:12 am

RSA is a very important encryption algorithm. It allows securely transmitting data encrypted with a private key that can only be decrypted with a public key.

There is class provides a pure PHP implementation of the RSA encryption algorithm, thus without relying on any PHP cryptography extensions.

Note:
php-rsa.zip is structured based implementation of RSA Algorithm

rsa-class.zip is class based implementation of RSA Algorithm
You do not have the required permissions to view the files attached to this post.
MT Shahzad
Web/Software Developer
http://mts.sw3solutions.com
User avatar
MT Shahzad
Site Admin
 
Posts: 300
Joined: February 29th, 2008, 8:11 am
Location: Muridke, Pakistan


Return to PHP / MySQL / XML

Who is online

Users browsing this forum: No registered users and 0 guests

cron