Created
October 16, 2016 18:15
-
-
Save AndreiTelteu/476e95ef84ae99269bac6a3eb5cc94c0 to your computer and use it in GitHub Desktop.
Caesar cipher encryption technique https://en.wikipedia.org/wiki/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 | |
function caesar_cipher_smart($shift, $input) { | |
$a = str_split('ABCDEFGHIJKLMNOPQRSTUVWXYZ'); | |
$p = str_split($input); | |
foreach ($p as &$v) | |
if (($k = array_search($v, $a)) !== false) { | |
$t = $k+$shift; | |
if ($t < 0) $t = count($a)+$t; | |
$v = $a[$t]; | |
} | |
return implode('', $p); | |
} | |
function caesar_cipher_lazy($shift, $input) { | |
$a = str_split('ABCDEFGHIJKLMNOPQRSTUVWXYZ'); | |
$b = str_split('XYZABCDEFGHIJKLMNOPQRSTUVW'); | |
$p = str_split($input); | |
foreach ($p as &$v) | |
if (($k = array_search($v, $a)) !== false) | |
$v = $b[$k]; | |
return implode('', $p); | |
} | |
$input = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
echo 'Input "'.$input.'"'; | |
echo '<br> -> Output (smart) "'.caesar_cipher_smart(-3, $input).'"'; | |
echo '<br> -> Output (lazy) "'.caesar_cipher_lazy(-3, $input).'"'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment