Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --1--
- normalize :: String -> String
- normalize text = if (any isDigit text) then "Error" else map toUpper (filter (\x -> x /= ' ' && x /= ',' && x/= '.') text)
- --1--
- --2--
- encode :: [Char] -> Char -> Int -> Char
- encode alphabet ch offset
- |not (isLetter ch) = error "error: unsupported symbol"
- |offset <0 && (index + offset < 0) = alphabet!!(abs(length alphabet + index + offset)`mod` length alphabet)
- |offset >0 && (index + offset >= length alphabet) = alphabet!!(abs(length alphabet - index - offset) `mod` length alphabet)
- |otherwise = alphabet!!(index + offset)
- where
- index = fromJust $ findIndex (\ x -> x == ch ) alphabet
- --
- encrypt :: [Char] -> Int -> String -> String
- encrypt alphabet offset normalized = [encode alphabet x offset | x <- normalized]
- decrypt :: [Char] -> Int -> String -> String
- decrypt alphabet offset encrypted = [encode alphabet x (offset*(-1)) | x <- encrypted]
- --2--
- --3--
- crackall :: [Char] -> String -> [String]
- crackall alphabet encrypted = [encrypt alphabet x encrypted| x <- [1..(length alphabet-1)]]
- substring :: String -> String -> Bool
- substring sub str = isInfixOf sub str
- crackcandidates :: [Char] -> [String] -> String -> [String]
- crackcandidates alphabet commonwords encrypted =
- filter(\x -> helper5 commonwords x == True ) (crackall alphabet encrypted)
- where
- helper5 :: [String] -> String -> Bool
- helper5 commonwords encrypted = True `elem` [substring x encrypted| x<-commonwords ]
- --3--
- --4--
- polyencrypt :: [Char] -> Int -> Int -> Int -> String -> String
- polyencrypt alphabet offset step blockSize normalized
- |normalized == [] = []
- |otherwise = encrypt alphabet offset (take blockSize normalized) ++ polyencrypt alphabet (offset+step) step blockSize (drop blockSize normalized)
- polydecrypt :: [Char] -> Int -> Int -> Int -> String -> String
- polydecrypt alphabet offset step blockSize encrypted
- |encrypted == [] = []
- |otherwise = decrypt alphabet (offset) (take blockSize encrypted) ++ polydecrypt alphabet (offset+step) step blockSize (drop blockSize encrypted)
- --4--
- --5--
- enigmaencrypt :: [Char] -> [Rotor] -> String -> String
- enigmaencrypt alphabet rotors normalized
- |rotors == [] = normalized
- |otherwise = enigmaencrypt alphabet (drop 1 rotors) (polyencrypt alphabet (getOffset (take 1 rotors)) (getStep (take 1 rotors)) (getBlockSize (take 1 rotors)) normalized)
- enigmadecrypt :: [Char] -> [Rotor] -> String -> String
- enigmadecrypt alphabet rotors normalized
- |rotors == [] = normalized
- |otherwise = enigmadecrypt alphabet (drop 1 rotors) (polydecrypt alphabet (getOffset (take 1 rotors)) (getStep (take 1 rotors)) (getBlockSize (take 1 rotors)) normalized)
- type Rotor = (Int,Int,Int)
- getOffset :: [Rotor] -> Int
- getOffset [(r, _,_)] = r
- getStep :: [Rotor] -> Int
- getStep [(_,s,_)] = s
- getBlockSize :: [Rotor] -> Int
- getBlockSize [(_,_,b)] = b
- --5--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement