Miscellaneous
|
|
|
|
<?php
/*=============================================================================
*
* caeser_shift.php
*
* Robert Fisher 11/2000
*
* Just got sent a stupid little message which looked like it was ROT13-ed,
* but it wasn't. It looked like a Caesar shift cypher, so I hacked
* together a little script to decode it. It's really rubbish - it mangles
* case, removes spaces completely, and it simply /has/ to be the
* most useless script on this site. Hurrah!
*
*==========================================================================*/
$msg = "insert your message here";
/* this loop prints all 25 caesar shifts of the message */
for ($offset = 0; $offset < 26; $offset++) {
echo "<P> shift of $offset : ", rot($msg, $offset);
}
function get_no($letter, $alphabet) {
// crudely return the position in the alphabet of the letter we're given
if (!ereg("^[a-zA-Z]$", $letter))
return false;
for ($i = 0; $i < sizeof($alphabet); $i++) {
if (strtolower($letter) == $alphabet[$i])
break;
}
return $i;
}
function rot($msg, $offset) {
$from = $to = array();
$msg = explode(" ", trim(chunk_split($msg, 1, " ")));
/* generate an alphabet */
for ($i = ord("a"); $i <= ord("z"); $i++) {
$from[] = chr($i);
}
/* join two alphabets together so we can do the Caesar shift */
$to = array_merge($from, $from);
/* go through each letter, building up an array containing the shifted
* message */
for ($i = 0; $i < sizeof($msg); $i++) {
if ($shifted = get_no($msg[$i], $from))
$out[] = $to[$shifted + $offset];
}
/* squash the shifted message into a string and return it */
return implode("", $out);
}
?>
|
|
|
Usage Example
|
|
|
Rate This Script
|
|
|
|