Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

A Reversible Encryption Routine for PHP

By Tony Marston
2005-04-15


Customising the Encryption Algorithm

By this I mean taking a routine that is freely available, such as this one, and customising it so that it produces totally different results from any other implementation. This customisation may be done at two levels:

• By changing lines of code within the routine.
• By supplying additional values that the routine may include in its algorithm.

The first method is something that should be left to someone who knows what he's doing as making a mistake can result in a broken algorithm. The second method is as simple as supplying the routine with the values that you want it to use.

Test Harness

In order to demonstrate that this routine does in fact encrypt a text string and decrypt it back into the original text I have created a test harness which looks like the following:


The fields labelled 'key' and 'password' should be self-explanatory. The 'password length' field is there to ensure that a shorter password is padded out with spaces so that the length of the encrypted string does not give away the length of the original string. Notice how each of the trailing spaces in the above example has been converted into a totally different character.

'Adjustment' is a value that is added to the value obtained from the 'key' array in each processing cycle. By allowing decimal places it means that the resulting number may be rounded either up or down, which causes the number to be incremented again on occasions.

'Modulus' is another value that is used to adjust the index number between STRING1 and STRING2. If the value can be divided by 'modulus' with no remainder then it is multiplied by -1 which reverses the sign. This effectively means that when picking out characters from the string you may sometimes start from the beginning and search forwards, while at other times you start from the end and search backwards.

You can run the test harness by clicking here . You can view the source for the test harness by clicking here .

The Encryption Class

This routine is provided in the form of a class with properties (variables) and methods (functions). Below are commented highlights, but the entire class file can be viewed here .

Tutorial Pages:
» A Reversible Encryption Routine for PHP
» Rotating each character a fixed number of positions
» Swapping between two different character strings
» Manipulating the index number between the two strings
» Customising the Encryption Algorithm
» Class Variables
» Class Constructor
» The ConvertKey function
» The ApplyFudgeFactor function
» The CheckRange Function
» The Decrypt function
» Using the Encryption Class
» Summary


 | Bookmark
Related Tutorials:
» Zend Framework Tutorial
» Port Scanning and Service Status Checking in PHP
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1