A Reversible Encryption Routine for PHP

by Tony Marston
TonyMarston.net
Friday, 2nd June 2006

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 .


Options:
Printer Friendly
Email Friend

About The Author:

I have been a software engineer, both designing and developing, since 1977. I have worked with a variety of 2nd, 3rd and 4th generation languages on a mixture of mainframes, mini- and micro-computers. I have worked with flat files, indexed files, hierarchical databases, network databases and relational databases. The user interfaces have included punched card, paper tape, teletype, block mode, CHUI, GUI and web. I have written code which has been procedural, model-driven, event-driven, component-based and object oriented. I have built software using the 1-tier, 2-tier, 3-tier and Model-View-Controller (MVC) architectures. After working with COBOL for 16 years I switched to UNIFACE in 1993, starting with version 5, then progressing through version 6 to version 7. In the middle of 2002 I decided to teach myself to develop web applications using PHP and MySQL.

Developer Categories



Developer Tutorials
ASP
CGI & Perl
CSS
Flash
HTML
Java
JavaScript
MySQL
PHP
Python
XML

Developer Documentation

Developer Tools



Search our Developer Tutorials
  The DevSyndicate Network