4
我寫了一個小程序來執行泛化的凱撒密碼。我無法弄清楚爲什麼我的暴力解密功能不起作用。我試圖做的是嘗試每個可能的乘數和偏移小於26的組合。此外,如果任何人都可以建議一個更好的方法爲「tryallcombinations」功能,這將不勝感激。隨機Haskell錯誤。計算永遠不會結束
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
'(\ x-> x ciphertext)'與段「$'相同,例如, '($ ciphertext)' – 2012-03-30 04:37:26