Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --1--
  2. normalize :: String -> String
  3. normalize text = if (any isDigit text) then "Error" else map toUpper (filter (\x -> x /= ' ' && x /= ',' && x/= '.') text)
  4. --1--
  5. --2--
  6.  
  7. encode :: [Char] -> Char -> Int -> Char
  8. encode alphabet ch offset
  9.      |not (isLetter ch) = error "error: unsupported symbol"
  10.      |offset <0 && (index + offset < 0) = alphabet!!(abs(length alphabet + index + offset)`mod` length alphabet)
  11.      |offset >0 && (index + offset >= length alphabet) = alphabet!!(abs(length alphabet - index - offset) `mod` length alphabet)
  12.      |otherwise = alphabet!!(index + offset)
  13.       where
  14.         index = fromJust $ findIndex (\ x -> x == ch ) alphabet
  15.  
  16. --
  17. encrypt :: [Char] -> Int -> String -> String
  18. encrypt alphabet offset normalized = [encode alphabet x offset | x <- normalized]
  19.  
  20. decrypt :: [Char] -> Int -> String -> String
  21. decrypt alphabet offset encrypted = [encode alphabet x (offset*(-1)) | x <- encrypted]
  22. --2--
  23. --3--
  24. crackall :: [Char] -> String -> [String]
  25. crackall alphabet encrypted = [encrypt alphabet x encrypted| x <- [1..(length alphabet-1)]]
  26.  
  27. substring :: String -> String -> Bool
  28. substring sub str = isInfixOf sub str
  29.  
  30. crackcandidates :: [Char] -> [String] -> String -> [String]
  31. crackcandidates alphabet commonwords encrypted =
  32.        filter(\x -> helper5 commonwords x == True ) (crackall alphabet encrypted)
  33.        where
  34.             helper5 :: [String] -> String -> Bool
  35.             helper5 commonwords encrypted = True `elem` [substring x encrypted| x<-commonwords ]
  36. --3--
  37. --4--
  38. polyencrypt :: [Char] -> Int -> Int -> Int -> String -> String
  39. polyencrypt alphabet offset step blockSize normalized
  40.  |normalized == [] = []
  41.  |otherwise = encrypt alphabet offset (take blockSize normalized) ++ polyencrypt alphabet (offset+step) step blockSize (drop blockSize normalized)
  42. polydecrypt :: [Char] -> Int -> Int -> Int -> String -> String
  43. polydecrypt alphabet offset step blockSize encrypted
  44.   |encrypted == [] = []
  45.   |otherwise = decrypt alphabet (offset) (take blockSize encrypted) ++ polydecrypt alphabet (offset+step) step blockSize (drop blockSize encrypted)
  46. --4--
  47. --5--
  48. enigmaencrypt :: [Char] -> [Rotor] -> String -> String
  49. enigmaencrypt alphabet rotors normalized
  50.  |rotors == [] = normalized
  51.  |otherwise = enigmaencrypt alphabet (drop 1 rotors) (polyencrypt alphabet (getOffset (take 1 rotors)) (getStep (take 1 rotors)) (getBlockSize (take 1 rotors)) normalized)
  52.  
  53. enigmadecrypt :: [Char] -> [Rotor] -> String -> String
  54. enigmadecrypt alphabet rotors normalized
  55.   |rotors == [] = normalized
  56.   |otherwise = enigmadecrypt alphabet (drop 1 rotors) (polydecrypt alphabet (getOffset (take 1 rotors)) (getStep (take 1 rotors)) (getBlockSize (take 1 rotors)) normalized)
  57.  
  58. type Rotor = (Int,Int,Int)
  59. getOffset :: [Rotor] -> Int
  60. getOffset [(r, _,_)] = r
  61. getStep :: [Rotor] -> Int
  62. getStep [(_,s,_)] = s
  63. getBlockSize :: [Rotor] -> Int
  64. getBlockSize [(_,_,b)] = b
  65. --5--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement