Функция декодирования цезаря
This commit is contained in:
@@ -16,4 +16,6 @@ main = do
|
|||||||
let encryptedTextBits = textToBits encryptedText
|
let encryptedTextBits = textToBits encryptedText
|
||||||
putStrLn $ concat (take 30 (map show encryptedTextBits))
|
putStrLn $ concat (take 30 (map show encryptedTextBits))
|
||||||
let encryptedTextFromBits = bitsToText encryptedTextBits
|
let encryptedTextFromBits = bitsToText encryptedTextBits
|
||||||
putStrLn $ take 30 encryptedTextFromBits
|
putStrLn $ take 30 encryptedTextFromBits
|
||||||
|
let decryptedText = decryptCaesar alphabet caesarShift encryptedTextFromBits
|
||||||
|
putStrLn $ take 30 decryptedText
|
||||||
@@ -2,6 +2,7 @@ module Lib
|
|||||||
(
|
(
|
||||||
createAlphabetFromText,
|
createAlphabetFromText,
|
||||||
encryptCaesar,
|
encryptCaesar,
|
||||||
|
decryptCaesar,
|
||||||
textToBits,
|
textToBits,
|
||||||
bitsToText
|
bitsToText
|
||||||
) where
|
) where
|
||||||
@@ -28,6 +29,12 @@ encryptCaesar alphabet shift text = map caesarChar text
|
|||||||
where
|
where
|
||||||
caesarChar c = alphabet !! ((indexOf alphabet c + shift) `mod` length alphabet)
|
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 :: String -> [Int]
|
||||||
textToBits = concatMap charToBits
|
textToBits = concatMap charToBits
|
||||||
where charToBits c = [ if testBit (ord c) i then 1 else 0 | i <- [7,6..0] ]
|
where charToBits c = [ if testBit (ord c) i then 1 else 0 | i <- [7,6..0] ]
|
||||||
|
|||||||
Reference in New Issue
Block a user