10/07/2007

PHP Code: Simple Encryption Algorithm

This is a very simple Encryption Algorithm that uses the Caesar Shift Decoder (also called the Caesar Cipher).

The encryption is not limited to 25 characters though but is limited to the ASCII table moving each character a random amount of numbers forward.

Here is how it works:

1) Generate a Random Number


PHP Code:
// Create a Random Generator
srand((double)microtime()*1000000); // Seed the Random Generator
$strCharNumber = rand(102,106); // Pics a number between 102 and 106

This code will be the variable for how many characters the encryption scheme will be moved forward. As you can see, it is between 102 and 106.

2) Add this character to our final string output


PHP Code:
$strcode = chr($strCharNumber); // Add char to ending String

This allows us to grab that character and decrypt the entire string. This makes the code very unsecure as anyone that knows that is the decoding character can decode this message.

3) Encode the rest of the string. Lets say you received a [b]g[/g] which is 103 ascii value. For each character you would get the ascii value and move it up 103 values in the ascii table.


PHP Code:
// For Loop to convert each char into ascii then increase number
for ($i = 0; $i < strlen($name); $i++) {
$strChar = ord($name[$i]) + $strCharNumber;

4) In my code I also converted to hex but you can leave this step out if you like.


PHP Code:
$strChar = bin2hex(chr($strChar));

5) Add the string to the final string and close the for loop.


PHP Code:
$strcode = $strcode & $strChar;
}
?>

I've also attached a working copy of the PHP script. Let me know if you have any questions!
Attached FilesTo view attachments your post count must be 1 or greater. Your post count is 0 momentarily.

No comments: