Last active
August 29, 2015 14:02
-
-
Save CarstenKoenig/eafad747133c5353164b to your computer and use it in GitHub Desktop.
simple Vigenere-cipher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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