2012-03-30 2 views
4

私は一般化されたシーザー暗号を行うための小さなプログラムを書いた。私は解読のための私の無理な力の機能が働いていない理由を理解できません。私がしようとしているのは、乗数とオフセットが26未満のすべての可能な組み合わせを試してみることです。また、誰かが "tryallcombinations"関数のより良いアプローチを提案できるなら、それは高く評価されます。ランダムなハスケルバグ。計算が決して終わらない

import Data.Char 

caesarencipher::Int->Int->String->String 
caesarencipher r s p = map chr $ map encipher plaintext 
    where 
     plaintext = map (\x->(ord x) - 97) p 
     encipher p = (mod (r*p + s) 26) + 97 

caesardecipher::Int->Int->String->String 
caesardecipher r s c = map chr $ map decipher ciphertext 
    where 
     ciphertext = map (\x->(ord x) - 97) c 
     inverser x | mod (r * x) 26 == 1 = x 
        | otherwise = inverser (x + 1) 
     decipher c = (mod ((inverser 1) * (c - s)) 26) + 97 

tryallcombinations::String->[String] 
tryallcombinations ciphertext = map (\x->x ciphertext) possibilities 
    where 
     rs = map caesardecipher [0..25] 
     possibilities = concat $ map (\x-> map x [0..25]) rs 
+2

'(\ x-> x暗号文)'はセクション化された '$'と同じです。 '($ ciphertext)' –

答えて

7

26は素数でないので、必ずしもすべての数字はinverserが時々返すことはありませんが、永遠に再帰することを意味し、モジュラー逆のmod 26を持っています。

+0

おっと!私はそれを見つけたはずです。私は「手動で」機能を使用していて、自分でそれをチェックしていました。私はそれを説明しなかったことを忘れる。 –

関連する問題