2017-03-12 80 views
1

我想這個功能。第一個密碼 - 完美地工作,我驗證了它,但是當我解密時,它不符合這個詞。
有人能弄清楚爲什麼不起作用?腓str_ireplace不工作

// 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; 
+1

我希望這個'cypher'是隻有約打,而不是你希望使用,以確保任何形式的通信 – Martin

+0

的是什麼你的PHP錯誤日誌說些什麼? – Martin

+0

究竟是什麼問題,爲什麼問題'str_ireplace'? – Martin

回答

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);