Skip to content

Instantly share code, notes, and snippets.

@CarstenKoenig
Last active August 29, 2015 14:02
Show Gist options
  • Save CarstenKoenig/eafad747133c5353164b to your computer and use it in GitHub Desktop.
Save CarstenKoenig/eafad747133c5353164b to your computer and use it in GitHub Desktop.
simple Vigenere-cipher
module Vigenere where
import Data.Char
import Test.QuickCheck.Test (quickCheck)
encrypt :: String -> String -> String
encrypt key = zipWith (shiftChar (+)) (cycle key)
decrypt :: String -> String -> String
decrypt key = zipWith (shiftChar (flip (-))) (cycle key)
shiftChar :: (Int -> Int -> Int) -> Char -> Char -> Char
shiftChar op a b = chr . wrap $ op (ord a) (ord b)
where wrap c = c `mod` maxValue
maxValue = 1 + ord (maxBound :: Char)
-- test if decrypt . encrypt = id
decryptIsInverseOfEncrypt :: String -> IO ()
decryptIsInverseOfEncrypt key =
quickCheck (\s -> s == (decrypt key . encrypt key) s)
main :: IO ()
main =
-- run 100 tests with a sample key
decryptIsInverseOfEncrypt "my Key"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment