Click to See Complete Forum and Search --> : Ceaser Cipher


drawmack
10-19-2003, 01:03 PM
I'm still bored so I made up a ceaser cipher in php.

Here is the function

function ceaser($clicks,$input) {
//set output to stop errors from showing
$output = "";

//get the length of the string once to aid in efficency
$str_len = strlen($input);

//encryption loop
for($i=0;$i<$str_len;$i++) {
//get the ascii value of the current character
$cur_char = ord(substr($input,$i,1));

//if this is an upper case letter
if($cur_char > 64 && $cur_char < 91) {
//turn the letter into its position in the alphebet
$cur_char = $cur_char - 65;
//move the position according to clicks and turn the position
//back into the value of the ascii letter
$cur_char = ($cur_char + $clicks)%26 + 65;

//if this is a lower case letter
} elseif ($cur_char > 96 && $cur_char < 123) {
//turn the letter into its position in the alphebet
$cur_char = $cur_char - 97;
//move the position according to clicks and turn the position
//back into the value of the ascii letter
$cur_char = ($cur_char + $clicks)%26 + 97;
} //end if

$output .= chr($cur_char);
} //end for
return $output;
} //end rot13


the attachment is a complete working example

marque
10-20-2003, 07:06 AM
yhub fxwh gudzpdfn!

Moonglobe
10-20-2003, 10:45 AM
so is this just a rot13 implentation? 'cuz there is str_rot13()....

laserlight
10-20-2003, 11:09 AM
Not quite.

str_rot13() is a special case of a Caesar cipher.

drawmack
10-20-2003, 05:13 PM
rot13 is a ceaser cipher with a key of 13, this is a generic implementation of the cesear cipher where you can use any number of clicks.

Moonglobe
10-20-2003, 10:06 PM
o of course lol, ya wasn't thinkin straight. thanks for clearing that up :)

drawmack
11-07-2003, 01:49 AM
Originally posted by marque
yhub fxwh gudzpdfn!

I finally got around to programming the chi square attack on the ceaser cipher.

Tanslation: very cute drawmack!

with a key of 3, which is the classic ceaser cipher, very cute marque

procoder
11-09-2003, 09:29 PM
Well done drawmack

One suggestion

change


function ceaser($clicks,$input) {


to


function ceaser($input, $clicks = 13) {


Just incase no clicks specified, it uses rot13 actully.

so people can just do ceaser('bla') for defautt cypher

drawmack
11-09-2003, 11:13 PM
Originally posted by procoder

function ceaser($input, $clicks = 13) {

Just incase no clicks specified, it uses rot13 actully.
so people can just do ceaser('bla') for defautt cypher [/B]

Actually since the original Ceaser cipher used 3 clicks then shouldn't I set the clicks to 3 if I'm going to default it?

The reason I didn't default it is because it's more flexible this way. I can define the default clicks in the calling code without changing the function at all.