Jos*_*sto 4 haskell cryptography
我写了一个小程序来做广义的凯撒密码.我无法弄清楚为什么我的强力解密功能无效.我所要做的就是尝试每个可能的乘数和偏移小于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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
126 次 |
| 最近记录: |