Thursday, May 25, 2006

Simple PHP encryption/decryption

Today Im going to show you some functions for simple encryption/decryption in PHP. Feel free to use any of this code. As always I am not responsible for any damage caused by the use or misuse of any of this code. And if you like my tutorial please visit and consider joining my website [Relaywrite.com].

See the functions in action here

The functions



  • Our first function is our encryption function called createsecretmessage. It takes two arguments, the message we want to encrypt and the key.

  • The second function is the decryption function. It also takes two arguements, the encrypted message and the key.


How does it work?


I'm glad you asked otherwise i would have nothing to say. The encryption function takes the ascii value of each character in the string and adds it an associated ascii value in the key. If the result is greater than 255(ascii maximum) it loops around by subtracting 256 from the combined value. The key is looped around as well so if we have a 5 character key then on character 6 of the message the function goes back to the first character of the key. The decryption function works pretty much the same but in reverse. It takes the encrypted message and subtracts from each character's ascii value the associated key ascii value. If the result is less than zero it adds 256 to compensate for the looping from the encryption function. Here is a picture of the encryption process.


encryption function



  • find length of string

  • find length of key

  • initiate a for loop to cylce through each character in the string

  • for each character get the ascii value of the message and the associated character from the key and add them together(use the modulous operater to get the remainder of the current character divided by the key length)
  • if the sum of the two values is less than or equal to 255, add that char value to our encrypted string

  • if the sum of the two values is greater than 255, subtract 256 then add that char value to our encrypted string

  • return our encrypted string



function createsecretmessage($message,$key)
{
$keylength = strlen($key);
$messagelength = strlen($message);
for($i=0;$i<=$messagelength - 1;$i++)
{
$msgord = ord(substr($message,$i,1));
$keyord = ord(substr($key,$i % $keylength,1));

if ($msgord + $keyord <= 255){$encstring .= chr($msgord + $keyord);}
if ($msgord + $keyord > 255){$encstring .= chr(($msgord + $keyord)-256);}
}
return $encstring;
}


decryption function



  • find length of string

  • find length of key

  • initiate a for loop to cylce through each character in the encrypted string

  • for each character get the ascii value of the message and the associated character from the key and subtract the key ascii value from the encrypted message ascii value(use the modulous operater to get the remainder of the current character divided by the key length)
  • if the difference of the two is greater than or equal to 0 then add that charachter value to our decrypted string

  • if the sum of the two values is less than 0, add 256 then add that char value to our decrypted string

  • return our decrypted string



function readsecretmessage($message,$key)
{
$keylength = strlen($key);
$messagelength = strlen($message);
for($i=0;$i<=$messagelength - 1;$i++)
{
$msgord = ord(substr($message,$i,1));
$keyord = ord(substr($key,$i % $keylength,1));

if ($msgord - $keyord >= 0){$decstring .= chr($msgord - $keyord);}
if ($msgord + $keyord < 0){$decstring .= chr(($msgord - $keyord)+256);}
}
return $decstring;
}


Copy and paste section...



function createsecretmessage($message,$key)
{
$keylength = strlen($key);
$messagelength = strlen($message);
for($i=0;$i<=$messagelength - 1;$i++)
{
$msgord = ord(substr($message,$i,1));
$keyord = ord(substr($key,$i % $keylength,1));

if ($msgord + $keyord <= 255){$encstring .= chr($msgord + $keyord);}
if ($msgord + $keyord > 255){$encstring .= chr(($msgord + $keyord)-256);}
}
return $encstring;
}

function readsecretmessage($message,$key)
{
$keylength = strlen($key);
$messagelength = strlen($message);
for($i=0;$i<=$messagelength - 1;$i++)
{
$msgord = ord(substr($message,$i,1));
$keyord = ord(substr($key,$i % $keylength,1));

if ($msgord - $keyord >= 0){$decstring .= chr($msgord - $keyord);}
if ($msgord + $keyord < 0){$decstring .= chr(($msgord - $keyord)+256);}
}
return $decstring;
}

echo "String to encrypt - ".$_POST[toenc]."

";
$encstring = createsecretmessage($_POST[toenc],$_POST[key]);
echo "encrypted string - " . $encstring ."

";
$decstring = readsecretmessage($encstring,$_POST[key]);
echo "decrypted string - " . $decstring ."

";



dont forget about my website


Relaywrite.com The collaborative writing project Relay Write

3 comments:

Brandon said...

alex eats too much spam!

soul krasty said...

can you help me encrypt using php?

Howard said...

Didn't you read the post soul krasty? lol