1

I am trying this functions. And the first - cipher - is working perfectly, I verified it, but when I make decipher it doesnt match the word.
Can someone figure out why doesn't work?

// Polybius square
  1   2   3   4   5   6
1 A   Ă   Â   B   C   D
2 E   F   G   H   I   Î
3 J   K   L   M   N   O
4 P   Q   R   S   Ș   T
5 Ț   U   V   W   X   Y
6 Z   .   ,   ?   -   !
function cipher($text) {
$alphabet = array('a','ă','â','b','c','d','e','f','g','h','i','î','j','k','l','m','n','o','p','q','r','s','ș','t','ț','u','v','w','x','y','z','.',',','?','-','!');

    $polybios  = array('11','12','13','14','15','16','21','22','23','24','25','26','31','32','33','34','35','36','41','42','43','44','45','46','51','52','53','54','55','56','61','62','63','64','65','66');
    $output  = str_ireplace($alphabet, $polybios, $text);
    return($output);
   }

function decipher($string) {    
    $alphabet = array('a','ă','â','b','c','d','e','f','g','h','i','î','j','k','l','m','n','o','p','q','r','s','ș','t','ț','u','v','w','x','y','z','.',',','?','-','!');

    $polybios  array('11','12','13','14','15','16','21','22','23','24','25','26','31','32','33','34','35','36','41','42','43','44','45','46','51','52','53','54','55','56','61','62','63','64','65','66');
    $output  = str_ireplace($polybios, $alphabet, $string);   
    return($output);
}

$mesaj='cupcake';
$cipherText = cipher($mesaj);

echo $cipherText;

$decipherText = decipher($cipherText);
echo $decipherText;
Martin
  • 22,212
  • 11
  • 70
  • 132
  • 1
    I hope this `cypher` is for playing around only and not something you hope to use to secure any form of communication – Martin Mar 12 '17 at 21:58
  • What does your PHP Error log say? – Martin Mar 12 '17 at 21:58
  • What actually is the problem, why is the problem `str_ireplace`? – Martin Mar 12 '17 at 21:58
  • Do you know that PHP is **NOT** multi-byte safe by default? [Read more](http://stackoverflow.com/questions/12716064/why-are-php-string-functions-not-multi-byte-safe-by-default) – Martin Mar 12 '17 at 22:01
  • Have you [Read the manual](http://php.net/manual/en/function.str-replace.php)? : **Replacement order gotcha**: `Because str_replace() replaces left to right, it might replace a previously inserted value when doing multiple replacements.` – Martin Mar 12 '17 at 22:03
  • Can you show us examples of a cypher text and it's plaintext counter part – Martin Mar 12 '17 at 22:03

0 Answers0