我正在尝试这个功能。而第一个密码 - 工作得很完美,我验证了它,但是当我做出解密时它与这个词不匹配。
有人能弄明白为什么不起作用吗?
// 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;
答案 0 :(得分:0)
我认为问题在于,在对原始字符串进行编码后,您将返回一个大数字。 str_ireplace
然后不知道一个值的开始位置,另一个值是否能够解码它。
相反,如果您将密码字符串拆分为2个字符的块(所有编码值都是2位数字),并将它们分别解码,再次将它们连接在一起之前,它就能正常工作。
function cipher($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');
$encoded = str_replace($alphabet, $polybios, str_split($string, 1));
return implode('', $encoded);
}
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');
$decoded = str_replace($polybios, $alphabet, str_split($string, 2));
return implode('', $decoded);
}
$string = 'cupcake';
$cipher = cipher('cupcake');
$decipher = decipher($cipher);
var_dump($string, $cipher, $decipher);