-- Caeser cipher example from chapter 5 of Programming in Haskell, -- Graham Hutton, Cambridge University Press, 2016. -- original source http://people.cs.nott.ac.uk/pszgmh/Code.zip (access Dec. 2025) -- Caeser cipher replaces a letter by the one three positions further -- e.g., "haskell is fun" by "kdvnhoo lv ixq" import Data.Char -- Encoding and decoding -- convert lower-letters between 'a' to 'z' to integers between 0 and 25 -- ord :: Char -> Int (characters to unicode numbers) let2int :: Char -> Int let2int c = ord c - ord 'a' -- let2int 'g' -- convert integers between 0 and 25 to lower-letters between 'a' to 'z' -- chr :: Int -> Char (unicode numbers to characters) int2let :: Int -> Char int2let n = chr (ord 'a' + n) -- int2let 6 -- applies a shift factor to a lower-case letter shift :: Int -> Char -> Char shift n c | isLower c = int2let ((let2int c + n) `mod` 26) | otherwise = c -- shift 2 'c' -- for the decoder we can use the negative integer encode :: Int -> String -> String encode n xs = [shift n x | x <- xs] -- encode 3 "haskell is fun" -- encode (-3) "kdvnhoo lv ixq" -- Frequency analysis -- count the lower-case letters in a string lowers :: String -> Int lowers xs = length [x | x <- xs, x >= 'a' && x <= 'z'] -- lowers "haskell is fun" -- count the occurrences of a char in a string count :: Char -> String -> Int count x xs = length [x' | x' <- xs, x == x'] -- count 'l' "haskell is fun" -- percentage of n in m percent :: Int -> Int -> Float percent n m = (fromIntegral n / fromIntegral m) * 100 -- takes a string and returns a list of 26 percentages, -- one for each lower-case letter from 'a' to 'z' freqs :: String -> [Float] freqs xs = [percent (count x xs) n | x <- ['a'..'z']] where n = lowers xs -- freqs "abbcccddddeeeee" -- chi-square, here os is the list of observed frequencies -- and es is the list of expected frequencies frequencies chisqr :: [Float] -> [Float] -> Float chisqr os es = sum [((o-e)^2)/e | (o,e) <- zip os es] -- chisqr (freqs "abbcccddddeeeee") table -- the idea here is the smaller the value it produces, -- the better the match between the two frequency lists, -- e.g., chi-square of table with table gives 0.0 -- chisqr table table -- expected frequescies of lower-case letters table :: [Float] table = [8.1, 1.5, 2.8, 4.2, 12.7, 2.2, 2.0, 6.1, 7.0, 0.2, 0.8, 4.0, 2.4, 6.7, 7.5, 1.9, 0.1, 6.0, 6.3, 9.0, 2.8, 1.0, 2.4, 0.2, 2.0, 0.1] -- it rotates the elements of a list n places rotate :: Int -> [a] -> [a] rotate n xs = drop n xs ++ take n xs -- rotate 3 "haskell is fun" -- if we are given the encoded stream "kdvnhoo lv ixq" but not the shift factor -- and we wish to determine this number we can use freqs of the encoded stream -- chisqr (rotate 2 (freqs "kdvnhoo lv ixq")) table -- we now rotate it for [0..25] letters -- [chisqr (rotate n (freqs "kdvnhoo lv ixq")) table | n <-[0..25]] -- in which the minimun value is 202.42024 at posstion 3 -- the position of the minimun value is most probable the shift factor -- returns the list of all positions of a value in a list: positions :: Eq a => a -> [a] -> [Int] positions x xs = [i | (x',i) <- zip xs [0..], x == x'] -- positions 0 [1,0,1,1,1,0,1,0] -- crack "kdvnhoo lv ixq" crack :: String -> String crack xs = encode (-factor) xs where factor = head (positions (minimum chitab) chitab) chitab = [chisqr (rotate n table') table | n <- [0..25]] table' = freqs xs -- crack can decode most strings, but it is not always succesfull, -- e.g., too short strings or too unbalance frequencies -- encode 3 "Hello Haskell" -- "Hhoor Hdvnhoo" -- crack "Hhoor Hdvnhoo"