From 19b8202fa88d681d23edcf9ef403d394376f600a Mon Sep 17 00:00:00 2001 From: Arity-T Date: Tue, 19 Nov 2024 14:32:34 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=B7=D0=B2=D0=BB=D0=B5=D1=87=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BB=D1=8E=D1=87=D0=B0=20=D0=B8?= =?UTF-8?q?=D0=B7=20=D0=B8=D0=BC=D0=B5=D0=BD=D0=B8=20=D1=84=D0=B0=D0=B9?= =?UTF-8?q?=D0=BB=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lab3/app/Main.hs | 34 ++++++++++++++++++++-------------- lab3/src/Lib.hs | 8 +++++++- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/lab3/app/Main.hs b/lab3/app/Main.hs index 96a7a03..eb574f9 100644 --- a/lab3/app/Main.hs +++ b/lab3/app/Main.hs @@ -16,7 +16,7 @@ sourceTextPath = "resources/biography.txt" sourceImagePath :: String sourceImagePath = "resources/david.bmp" encodedImagePath :: String -encodedImagePath = "tmp/david.bmp" +encodedImagePath = "tmp/david_" ++ show caesarShift ++ ".bmp" decodedTextPath :: String decodedTextPath = "tmp/biography.txt" @@ -51,16 +51,22 @@ main = do putStrLn "\nДекодирование текста из изображения" - readEncodedImageResult <- readImage encodedImagePath - case readEncodedImageResult of - Left err -> putStrLn $ "Ошибка при чтении изображения: " ++ err - Right dynImg -> do - let img = convertRGB8 dynImg - let bits = VU.fromList $ extractBitsFromImage bitsPerByte img - putStrLn $ "10 битов шифра: \"" ++ show (take 10 $ VU.toList bits) ++ "\"" - let encryptedTextFromImage = takeWhile (/= '\NUL') (bitsToText bits) - putStrLn $ "10 символов шифра: \"" ++ take 10 encryptedTextFromImage ++ "\"" - let decryptedText = decryptCaesar alphabet caesarShift encryptedTextFromImage - putStrLn $ "10 символов текста: \"" ++ take 10 decryptedText ++ "\"" - writeFile decodedTextPath decryptedText - putStrLn $ "Текст сохранён по пути: \"" ++ decodedTextPath ++ "\"" \ No newline at end of file + case extractShift encodedImagePath of + Just extractedCaesarShift -> do + putStrLn $ "Из названия файла извлечён ключ: " ++ show extractedCaesarShift + + readEncodedImageResult <- readImage encodedImagePath + case readEncodedImageResult of + Left err -> putStrLn $ "Ошибка при чтении изображения: " ++ err + Right dynImg -> do + let img = convertRGB8 dynImg + let bits = VU.fromList $ extractBitsFromImage bitsPerByte img + putStrLn $ "10 битов шифра: \"" ++ show (take 10 $ VU.toList bits) ++ "\"" + let encryptedTextFromImage = takeWhile (/= '\NUL') (bitsToText bits) + putStrLn $ "10 символов шифра: \"" ++ take 10 encryptedTextFromImage ++ "\"" + let decryptedText = decryptCaesar alphabet extractedCaesarShift encryptedTextFromImage + putStrLn $ "10 символов текста: \"" ++ take 10 decryptedText ++ "\"" + writeFile decodedTextPath decryptedText + putStrLn $ "Текст сохранён по пути: \"" ++ decodedTextPath ++ "\"" + + Nothing -> putStrLn "Не удалось извлечь ключ." \ No newline at end of file diff --git a/lab3/src/Lib.hs b/lab3/src/Lib.hs index ad5615e..01c16f9 100644 --- a/lab3/src/Lib.hs +++ b/lab3/src/Lib.hs @@ -11,6 +11,7 @@ module Lib import Codec.Picture +import Text.Read (readMaybe) import Data.Word (Word8) import Data.Char (ord, chr) import Data.Bits (testBit, shiftL, complement, (.|.), (.&.)) @@ -112,4 +113,9 @@ extractBitsFromImage bitsPerByte img = let width = imageWidth img height = imageHeight img pixels = [ pixelAt img x y | y <- [0..height - 1], x <- [0..width - 1] ] - in concatMap (extractBitsFromPixel bitsPerByte) pixels \ No newline at end of file + in concatMap (extractBitsFromPixel bitsPerByte) pixels + +extractShift :: String -> Maybe Int +extractShift path = + let prefix = takeWhile (`elem` ['0'..'9']) (reverse $ takeWhile (/= '_') (reverse path)) + in readMaybe (reverse prefix)