Tuesday, March 6, 2012

Caesar Cipher in PHP


As more of your life is spent online, the more data comes online and the need for data encryption grows. Cryptography is the art of "secret writing," as defined by the word's roots.  One of the earliest known forms is the Caesar Cipher from around 50bce.  This is intended to be the first installment of the series in DIY cryptography.  

The Caesar Cipher is one of the earliest and simplest ciphers to exist.  It is easy to understand and easy to break, which we will get into soon.  It belongs to a cipher class known as shift ciphers and is the basis for many modern applications such as the Vigenere cipher and ROT13.

But how does it work?

Well, it takes any letters and shifts them by a defined integer.  If you decide that your integer/key is 5, you increase the letter value of every character by 5.  For example, A would become F, B would become G, etc.  In a traditional version, you would wrap around at Z back to A and the casing would all be upper case.  But, with PHP and the power of the processor, we are able to make it support any character in any language.

I am going to do the worst American thing possible and presume that you speak English in this demonstration.  Since the American English language has 26 characters in it, these are using a modulus to limit it at 26.  That way, your cipher code can be some really large number and still work.  Later, we will be switching this to a much higher number, which will allow us to include all 256 characters in the ASCII chart. 

If you assign all numbers an integer (A=1, B=2, etc) then these two formulas will do all of our math.

Encryption would be as follows.  With x being the integer value of our character in the string and add it to n, the shift value.  We then use modulus (% in PHP) to loop those characters that go past Z towards A.  This ends up leaving us with our new characters integer.  If we started with the letter C(3) and had a shift of +3, we would end up with F(6).  
Decryption is exactly the same thing, except we are reversing n.  So to get back to our original C(3) from F(6), our key is still 3, but we subtract it.


Got it? Great!  But you came here to do this in PHP, right?  We are going to write this as one simple function, with 3 inputs.  They will be your string, the shift value and if it is encryption of decryption.  We are expanding our original idea here and not limiting it to the 26 characters in the English language.  Since it is an example, we are leaving all error checking out.  In a production environment, you should always verify all input. 

Next post will be about adapting this to a rotating cipher based off of a key.  I always look forward to questions, comments and suggestions.

// Usage:
// Encryption - caesar('Input String', 1, true);
// Decryption - caesar('Joqvu!Tusjoh', 1, false);
function caesar($sInput = '', $iShift = 0, $bEncrypt = true) {
// Inline if statement to set our shift value if encrypting or decrypting
$iShift = ($bEncrypt) ? $iShift : 0 - $iShift;
// We declare the length here instead of in the for statement, otherwise it reevaluates this every time, which is bad for speed.
$iLength = strlen($sInput);
// Declare our empty string
$sReturn = '';
// We are extracting individual characters and getting the ascii code with ord, then adding the shift value and
// converting it back to a char with chr and adding it to our return string. 
for ($i = 0; $i < $iLength; $i++) {
$sReturn .= chr((ord($sInput[$i])+$iShift) % 256);
}
// Now click your heels together three times and say "There’s no place like home"
return $sReturn;
}





No comments:

Post a Comment