#!/usr/bin/runghc module TranspositionEncrypt (encryptString, messagePad, numOfRows) where import Data.List import Data.List.Split -- | Demonstrate use of chunksOf. -- >>> chunksOf 8 "Common sense is not so common." -- ["Common s","ense is ","not so c","ommon."] -- | -- >>> numOfRows 8 "Common sense is not so common." -- 4 numOfRows :: Int -> String -> Int numOfRows key message = ceiling (genericLength message / fromIntegral key) -- | -- >>> lengthPad 8 "Common sense is not so common." -- 2 lengthPad :: Int -> String -> Int lengthPad key message = (numOfRows key message * key) - length message -- | -- >>> messagePad 8 "Common sense is not so common." -- "Common sense is not so common. " messagePad :: Int -> String -> String messagePad key message = let padding = replicate (lengthPad key message) ' ' in message ++ padding -- | Enrcyption function. -- >>> encryptString 8 "Common sense is not so common." -- "Cenoonommstmme oo snnio. s s c " encryptString :: Int -> String -> String encryptString key = intercalate "" . transpose . chunksOf key . messagePad key main :: IO () main = do let message = "Common sense is not so common." let key = 8 putStrLn "Your encrypted message is:" putStrLn (encryptString key message)