Advertisement
Guest User

Untitled

a guest
Oct 10th, 2019
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. cbcEncrypt :: Char -> Char -> String -> String
  2. cbcEncrypt k iv plainText = generateCypher plainTextLength
  3.   where
  4.     plainTextLength  = length plainText
  5.     generateCypher :: Int -> String
  6.     -- Generate the cypher text based on length of the plain text
  7.     -- Function uses the CBC model defined as
  8.     -- c_1 = (x_1 + iv) + k
  9.     -- c_i = (x_i + c_(i-1)) + k for 1 < i ≤ l
  10.     -- where "+" reffers to the addition operation defined in the add Haskell
  11.     -- function and x is the plain text string
  12.     generateCypher 0 = []
  13.     generateCypher currentIndex
  14.       | currentIndex == 1
  15.         = [add (add currentEl iv) k]
  16.       | otherwise
  17.         = previousList ++ [add (add currentEl previousListLastEl) k]
  18.       where
  19.         currentEl               = plainText !! (currentIndex - 1)
  20.         previousList            = generateCypher (currentIndex - 1)
  21.         previousListLastEl      = previousList !! (length previousList - 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement