diff --git a/lab3/app/Main.hs b/lab3/app/Main.hs index e0a98cb..bf692a5 100644 --- a/lab3/app/Main.hs +++ b/lab3/app/Main.hs @@ -16,4 +16,6 @@ main = do let encryptedTextBits = textToBits encryptedText putStrLn $ concat (take 30 (map show encryptedTextBits)) let encryptedTextFromBits = bitsToText encryptedTextBits - putStrLn $ take 30 encryptedTextFromBits \ No newline at end of file + putStrLn $ take 30 encryptedTextFromBits + let decryptedText = decryptCaesar alphabet caesarShift encryptedTextFromBits + putStrLn $ take 30 decryptedText \ No newline at end of file diff --git a/lab3/src/Lib.hs b/lab3/src/Lib.hs index 8b11de5..c4aeca1 100644 --- a/lab3/src/Lib.hs +++ b/lab3/src/Lib.hs @@ -2,6 +2,7 @@ module Lib ( createAlphabetFromText, encryptCaesar, + decryptCaesar, textToBits, bitsToText ) where @@ -28,6 +29,12 @@ encryptCaesar alphabet shift text = map caesarChar text where caesarChar c = alphabet !! ((indexOf alphabet c + shift) `mod` length alphabet) +decryptCaesar :: [Char] -> Int -> String -> String +decryptCaesar alphabet shift = + encryptCaesar alphabet (alphabetLength - (shift `mod` alphabetLength)) + where + alphabetLength = length alphabet + textToBits :: String -> [Int] textToBits = concatMap charToBits where charToBits c = [ if testBit (ord c) i then 1 else 0 | i <- [7,6..0] ]