Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
45
Task/Vigen-re-cipher/PHP/vigen-re-cipher.php
Normal file
45
Task/Vigen-re-cipher/PHP/vigen-re-cipher.php
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
$str = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
|
||||
$key = "VIGENERECIPHER";
|
||||
|
||||
printf("Text: %s\n", $str);
|
||||
printf("key: %s\n", $key);
|
||||
|
||||
$cod = encipher($str, $key, true); printf("Code: %s\n", $cod);
|
||||
$dec = encipher($cod, $key, false); printf("Back: %s\n", $dec);
|
||||
|
||||
function encipher($src, $key, $is_encode)
|
||||
{
|
||||
$key = strtoupper($key);
|
||||
$src = strtoupper($src);
|
||||
$dest = '';
|
||||
|
||||
/* strip out non-letters */
|
||||
for($i = 0; $i <= strlen($src); $i++) {
|
||||
$char = substr($src, $i, 1);
|
||||
if(ctype_upper($char)) {
|
||||
$dest .= $char;
|
||||
}
|
||||
}
|
||||
|
||||
for($i = 0; $i <= strlen($dest); $i++) {
|
||||
$char = substr($dest, $i, 1);
|
||||
if(!ctype_upper($char)) {
|
||||
continue;
|
||||
}
|
||||
$dest = substr_replace($dest,
|
||||
chr (
|
||||
ord('A') +
|
||||
($is_encode
|
||||
? ord($char) - ord('A') + ord($key[$i % strlen($key)]) - ord('A')
|
||||
: ord($char) - ord($key[$i % strlen($key)]) + 26
|
||||
) % 26
|
||||
)
|
||||
, $i, 1);
|
||||
}
|
||||
|
||||
return $dest;
|
||||
}
|
||||
|
||||
?>
|
||||
Loading…
Add table
Add a link
Reference in a new issue