CyberArmy University | Open Source Institute | CyberArmy Intelligence & Security | CyberArmy Services & Projects

[Programming] Basic PHP Tutorial #1 - Ceasar's Cypher


[Reply] [View by Thread] [Help]
[Back To Article Discussion Forum]

Posted by Author alfer On 2007-04-29 10:01:43




View and vote on the article here: Basic PHP Tutorial #1 - Ceasar's Cypher


Basic PHP Tutorial #1 - Ceasar's Cypher

Category
Programming
Summary
A beginner's PHP tutorial that deals with string manipulation. Pretty useful for those new to PHP. What this tutorial will teach you: Using basic control structures, using the PHP functions substr, strlen, ord, chr, and print.
Body
Welcome to my first and hopefully one of many PHP tutorials. In this and the future tutorials I will format my information in a particular manner. I will first display the complete source code of the project















(unless of course it is very long), than I will explain almost line by line the purpose of each function, statement and variable. Please send all comments to alienine@gmx.net.
































In this tutorial I will tech you how to write a simple encoder - if you can even call it that. When I said simple I meant the great grand daddy















of all encryption (I mean the frail, slow and old one) - Ceasar's Cypher. No one in their sane mind would use this for anything important, but it's nice if you want to add encryption to your hax0r skills ;).
































What this tutorial will teach you: Using basic control structures, using the PHP functions substr, strlen, ord, chr, and print.
































What Ceasar's Cypher does is simply move each letter of a string up or down a place in the alphabet by a preset shift value. If a letter of a word that was to be encrypted contained A and the shift value would be 3, than the cyphered letter would be D, F would be I and so on.















Anyway here is the code.















Remember in PHP you don't have to inmediately specify your variables, or even their type!
















<tt>
































$string = 'Some sample text to be encoded. Boo! 124 I can't count!';
















$shift = 12; //value between -254 and 254
































for ($i=0; $i strlen
($string); $i++) {
















$char = substr($string, $i, 1);
















$decchar = ord($char);
















if (($decchar+$shift) > 255) {
















$decchar = $decchar - 255 + 32;
















} else if ((($decchar+$shift)















$decchar = ($decchar-31) + 255;
















}
















$c_decchar = $decchar + $shift;
















$c_char = chr($c_decchar);
















print($c_char);
















}















































?>















</tt>















<tt>$string = 'Some sample text to be encoded. Boo! 124 I can't count!';</tt>
















The string text to be encoded, virtually any ASCII character will do. Remember to unescape quote signs that are fo the same kind as those that hold them.
































<tt>$shift = 12; //value between -254 and 254















</tt>
















As the comment suggests, enter a a shift value between a set range. It becomes obvious why that is in the next few lines of code.
































<tt>for ($i=0; $i </tt>
















Begins a loop that will go through the entire length of the string.
















MAN Description:
















strlen (string str) - Returns the length of string.

































<tt>$char = substr($string, $i, 1);
















$decchar = ord($char);</tt>





































[/b]









Makes $char equal to one of characters of the string. The for loop will do this to the whole string, selecting each character one by one.
















The next line sets $decchar to be the ASCII decimal equalivent of the selected character. This is case sensitive, A (upper case) for example would become 65. This is where the $shift value limitation comes in, if it is too large it will simply go our of the ASCII range. But as long as you go by the comments suggestion you will be file thanks to the next few lines of code.
















MAN Description:
















substr (string string, int start [, int length])















- Substr returns the portion of string specified by the start and length parameters.
















ord (string string) - Returns the ASCII value of the first character of string. This function complements chr().

































<tt>if (($decchar+$shift) > 255) {
















$decchar = $decchar - 255 + 32;
</tt>
[/b]















The if statement mechs is the ASCII value of the cypher character is out of the range 255, that is if it is larger than 255. Here we already have calculated the cypher character by adding $decchar and $shift.
















If it is out of range than we simply take 255 away from it - so if we get 257, the result will be 2, but now the problem is that 2 isn't a visible character, so we add 32 to push it back to the right range making it a 34. (Any char below 32 is useless to us).
































<tt>} else if ((($decchar+$shift)















$decchar = ($decchar-31) + 255;
















}
</tt>
















This time is the value is something like -34 or 31 it will by simply pushed back up. Remember to close all open brackets and use them wisely, it is important that the stuff you want to be calculated seperately is separated using brackets.
































<tt>$c_decchar = $decchar + $shift;
















$c_char = chr($c_decchar);
</tt>
















Now the fun part. $c_decchar is our cyphered ASCII decimal value, calculated by adding the $decchar value that has now been adjusted to suit $shift, which we add.
















Next the $c_char variable is the final result, where the cyphered ASCII decimal value is converted to its character form, eg our 65 is now an A!
















MAN Description:
















chr (int ascii) - Returns a one-character string containing the character specified by ascii.
















































<tt>print($c_char);
















}















</tt>
















All that's left to do now is close the for loop bracket and inside it print our cyphered character.
















MAN Description:
















print (string arg) - Outputs arg as a string.

































Now to decypher simply set $string to be the cyphered text and instead of a $shift value of 12 use -12. :)
































That's it for now folks! Hope it was usefull to you! Please comment so I have the will to write another tutorial (I'm serious), any suggestions?
































DISCLAIMER: THIS TUTORIAL CAN NOT BE USED, EDITED, DISPLAYED, TRANSMITTED AND/OR CONSUMED IN ANY FORM BY ANYONE EXCEPT WHERE I HAVE EXPRESSED PERMISSION AND CONSENT. IF ANY OF THIS CODE HURTS YOU THAN GO CRY AND WALLOW IN YOUR SELF PITY FOR ALL ETERNITY IN A HOLE I CALL THE BILE OF SELF LOVE YOU PATHETIC FOOL. HAVE A NICE DAY. WHATEVER TURNS YOU ON.


This article was imported from zZine. (original author: alfer)


There are no replies to this post yet.



Guest:
Subject:
Message:
Signature:
Optional Image Link:
http://

CyberArmy::Forum v0.6
Generated In 0.02043 seconds


About Us | Privacy Policy | Mission Statement | Help