2011-09-16 12:15:43 +00:00
|
|
|
-- Source forum project haskell
|
|
|
|
import Data.Bits
|
|
|
|
import Data.Char
|
|
|
|
import Data.List
|
|
|
|
|
|
|
|
keys = [ [a,b,c] | a <- [97..122], b <- [97..122], c <- [97..122] ]
|
|
|
|
allAlpha a = all (\k -> let a = ord k in ( a >= 32 && a <= 122)) a
|
|
|
|
howManySpaces x = length (elemIndices ' ' x)
|
|
|
|
compareBy f x y = compare (f x) (f y)
|
2011-06-17 13:12:01 +00:00
|
|
|
|
|
|
|
main = do
|
2011-09-16 12:15:43 +00:00
|
|
|
s <- readFile "cipher1.txt"
|
|
|
|
let
|
|
|
|
cipher = (read ("[" ++ s ++ "]") :: [Int])
|
|
|
|
decrypts = [ (map chr (zipWith xor (cycle key) cipher), map chr key) | key <- keys ]
|
|
|
|
alphaDecrypts = filter (\(x,y) -> allAlpha x) decrypts
|
|
|
|
message = maximumBy (\(x,y) (x',y') -> compareBy howManySpaces x x') alphaDecrypts
|
|
|
|
asciisum = sum (map ord (fst message))
|
|
|
|
putStrLn ("The message " ++ (fst message) ++ " was encoded with key " ++ (snd message))
|
|
|
|
putStrLn (show asciisum)
|