Created
April 16, 2013 21:36
-
-
Save Opus1no2/5399860 to your computer and use it in GitHub Desktop.
Caesar Cipher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Caesar Cipher | |
*/ | |
$msg = 'GUVFVFZLFRPERGZRFFNTR'; | |
$key = 13; | |
$mode = 'decrypt'; | |
$letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
$trans = ''; | |
$msg = strtoupper($msg); | |
for ($i = 0; $i < strlen($msg); $i++) { | |
if (strpos($letters, $msg[$i]) !== false) { | |
$num = strpos($letters, $msg[$i]); | |
if ($mode == 'encrypt') { | |
$num += $key; | |
} else { | |
$num -= $key; | |
} | |
$cnt = strlen($letters); | |
if ($num >= $cnt) { | |
$num -= $cnt; | |
} else { | |
$num += $cnt; | |
} | |
if ($num >= $cnt) { | |
$num = $num - $cnt; | |
} | |
$trans .= $letters[$num]; | |
} | |
} | |
echo $trans . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment