Skip to content

Instantly share code, notes, and snippets.

@Dennis960
Dennis960 / VigenereCipher.hs
Created December 6, 2023 23:16
Haskell vigenereCipher
import Data.Char (chr, ord, toLower, isLetter)
-- | Vigenere Cipher
vigenereCipher2 :: String -> String -> String
vigenereCipher2 message key = zipWith (curry shiftCharByKey) message (prepareKey message key) where
-- | Prepares the key to skip foreign chars by setting the key to 'a' at these positions and repeats the key so it matches the messages length
prepareKey :: String -> String -> String
prepareKey [] _ = []
prepareKey (m:message) (k:key) = if isLetter m then k : prepareKey message (key ++ [k]) else 'a' : prepareKey message (k : key)
-- | Shifts a character depending on the position of the key's character in the alphabet