sholsinger.com

Categories Photography Résumé About Me

Basic Cryptography With PHP - shift and unshift

10 Apr 2009

I just started reading Dan Brown's, "Digital Fortress" which is an excellent read so far. Early on in the novel, we are introduced to some very basic cryptography. Nothing like the FBI Cryptography Challenge</a>, mind you. This is much simpler. So, what is the cypher? It's simple. Just shift each letter of the "clear text" message by minus one. So, 'a' becomes 'z', 'b' becomes 'a', 'c' becomes 'b', and so on. First, we'll need a script to encrypt the message. We'll also need a script to decrypt the message just for good measure. So I give you, shift.php</code> and unshift.php</code>.

shift.php contains:

<?php
function readline ( $length = 255 )
{
    $line = fgets ( STDIN, $length );
    return trim ( $line );
}

function shift ( $str ) { $shifted = ''; $chars = str_split( $str ); foreach ( $chars as $char ) { $ord = ord( $char ); if ( ( ( $ord >= 64 ) && ( $ord <= 90 ) ) || ( ( $ord >= 97 ) && ( $ord <= 122 ) ) ) { if ( ($ord === 97) || ($ord === 65) ) { $shifted .= chr( $ord + 25 ); } else { $shifted .= chr( $ord - 1 ); } } else { $shifted .= chr ($ord); } } return $shifted; }

function writeline( $line ) { $bytes = fwrite ( STDOUT, $line, strlen( $line ) ); return $bytes; }

/* main */

while ( $line = readline () ) { writeline( shift( $line ) . "\r\n" ); } ?></code></pre> Notice the use of the STDIN and STDOUT magic constants. PHP Is nice enough to give us pre-opened file handles with read and write (respectively) access to each STDIN and STDOUT whenever a script is run in CLI SAPI mode. For instance if you ran your script using: $ php -f script_name.php</code> it would be running in CLI mode. We also are provided $argc</code> and $argv</code> in CLI mode. By using the POSIX standard input and standard output we can use these small scripts in more powerful ways with other small scripts. It is a very UNIX way of doing this, but it is handy to know.

unshift contains:

<?php
function readline ( $length = 255 )
{
    $line = fgets ( STDIN, $length );
    return trim ( $line );
}

function unshift ( $str ) { $shifted = ''; $chars = str_split( $str ); foreach ( $chars as $char ) { $ord = ord( $char ); if ( ( ( $ord >= 64 ) && ( $ord <= 90 ) ) || ( ( $ord >= 97 ) && ( $ord <= 122 ) ) ) { if ( ($ord === 90) || ($ord === 122) ) { $shifted .= chr( $ord - 25 ); } else { $shifted .= chr( $ord + 1 ); } } else { $shifted .= chr ($ord); } } return $shifted; }

function writeline( $line ) { $bytes = fwrite ( STDOUT, $line, strlen( $line ) ); return $bytes; }

/* main */

while ( $line = readline () ) { writeline( unshift( $line ) . "\r\n" ); } ?></code></pre> That is it. Now we can encrypt and decrypt simple messages with the following commands: php -f shift.php</code> and php -f unshift.php</code>. I hereby release the included code to the community free of any restrictions. That is, gratis</em> for any use, commercial or private. Enjoy! You can download a zip archive of the two files from the following url: http://sholsinger.com/files/shift_unshift.zip</a>

Dppm, ivi? Ibwf gvo boe vtf zpvs qpxfst pomz gps hppe.</encrypted>

Filed under

Comments
  • Mohamed Jafer Ali 2012-04-18 05:16:56 -0400

    Nice dude. After reading your article i have a clear idea about my process...Thank you

comments powered by Disqus