summaryrefslogtreecommitdiff
path: root/Haskell-book/11/Cipher.hs
blob: 92e0e7deee8485ee457339a935d5aa79746ca77f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
module Cipher where

import Data.Char
import Data.List

decode :: (Char, Int) -> Char
decode (x, y) =  chr ((mod ((ord x) - 65 + y) 26) + 65)

decodeWithKey :: [Int] -> String -> String
decodeWithKey _      []       = []
decodeWithKey ys     (' ':xs) = ' ' : (decodeWithKey ys xs)
decodeWithKey (y:ys) (x:xs)   = (decode (x, y)) : (decodeWithKey ys xs)

vigenere :: String -> String
vigenere = decodeWithKey (cycle [0, 11, 11, 24])

caeser :: String -> String
caeser = decodeWithKey (cycle [11])

main :: IO ()
main = do
    putStrLn (vigenere "MEET AT DAWN")
    putStrLn (caeser "MEET AT DAWN")
    return ()