Skip to content

Instantly share code, notes, and snippets.

@Opus1no2
Created April 16, 2013 21:36
Show Gist options
  • Save Opus1no2/5399860 to your computer and use it in GitHub Desktop.
Save Opus1no2/5399860 to your computer and use it in GitHub Desktop.
Caesar Cipher
<?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