Функция декодирования цезаря

This commit is contained in:
2024-11-17 20:19:01 +03:00
parent f1fd3c1dea
commit a69a81d89a
2 changed files with 10 additions and 1 deletions

View File

@@ -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
putStrLn $ take 30 encryptedTextFromBits
let decryptedText = decryptCaesar alphabet caesarShift encryptedTextFromBits
putStrLn $ take 30 decryptedText

View File

@@ -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] ]