Извлечение ключа из имени файла
This commit is contained in:
@@ -16,7 +16,7 @@ sourceTextPath = "resources/biography.txt"
|
|||||||
sourceImagePath :: String
|
sourceImagePath :: String
|
||||||
sourceImagePath = "resources/david.bmp"
|
sourceImagePath = "resources/david.bmp"
|
||||||
encodedImagePath :: String
|
encodedImagePath :: String
|
||||||
encodedImagePath = "tmp/david.bmp"
|
encodedImagePath = "tmp/david_" ++ show caesarShift ++ ".bmp"
|
||||||
decodedTextPath :: String
|
decodedTextPath :: String
|
||||||
decodedTextPath = "tmp/biography.txt"
|
decodedTextPath = "tmp/biography.txt"
|
||||||
|
|
||||||
@@ -51,16 +51,22 @@ main = do
|
|||||||
|
|
||||||
|
|
||||||
putStrLn "\nДекодирование текста из изображения"
|
putStrLn "\nДекодирование текста из изображения"
|
||||||
readEncodedImageResult <- readImage encodedImagePath
|
case extractShift encodedImagePath of
|
||||||
case readEncodedImageResult of
|
Just extractedCaesarShift -> do
|
||||||
Left err -> putStrLn $ "Ошибка при чтении изображения: " ++ err
|
putStrLn $ "Из названия файла извлечён ключ: " ++ show extractedCaesarShift
|
||||||
Right dynImg -> do
|
|
||||||
let img = convertRGB8 dynImg
|
readEncodedImageResult <- readImage encodedImagePath
|
||||||
let bits = VU.fromList $ extractBitsFromImage bitsPerByte img
|
case readEncodedImageResult of
|
||||||
putStrLn $ "10 битов шифра: \"" ++ show (take 10 $ VU.toList bits) ++ "\""
|
Left err -> putStrLn $ "Ошибка при чтении изображения: " ++ err
|
||||||
let encryptedTextFromImage = takeWhile (/= '\NUL') (bitsToText bits)
|
Right dynImg -> do
|
||||||
putStrLn $ "10 символов шифра: \"" ++ take 10 encryptedTextFromImage ++ "\""
|
let img = convertRGB8 dynImg
|
||||||
let decryptedText = decryptCaesar alphabet caesarShift encryptedTextFromImage
|
let bits = VU.fromList $ extractBitsFromImage bitsPerByte img
|
||||||
putStrLn $ "10 символов текста: \"" ++ take 10 decryptedText ++ "\""
|
putStrLn $ "10 битов шифра: \"" ++ show (take 10 $ VU.toList bits) ++ "\""
|
||||||
writeFile decodedTextPath decryptedText
|
let encryptedTextFromImage = takeWhile (/= '\NUL') (bitsToText bits)
|
||||||
putStrLn $ "Текст сохранён по пути: \"" ++ decodedTextPath ++ "\""
|
putStrLn $ "10 символов шифра: \"" ++ take 10 encryptedTextFromImage ++ "\""
|
||||||
|
let decryptedText = decryptCaesar alphabet extractedCaesarShift encryptedTextFromImage
|
||||||
|
putStrLn $ "10 символов текста: \"" ++ take 10 decryptedText ++ "\""
|
||||||
|
writeFile decodedTextPath decryptedText
|
||||||
|
putStrLn $ "Текст сохранён по пути: \"" ++ decodedTextPath ++ "\""
|
||||||
|
|
||||||
|
Nothing -> putStrLn "Не удалось извлечь ключ."
|
||||||
@@ -11,6 +11,7 @@ module Lib
|
|||||||
|
|
||||||
import Codec.Picture
|
import Codec.Picture
|
||||||
|
|
||||||
|
import Text.Read (readMaybe)
|
||||||
import Data.Word (Word8)
|
import Data.Word (Word8)
|
||||||
import Data.Char (ord, chr)
|
import Data.Char (ord, chr)
|
||||||
import Data.Bits (testBit, shiftL, complement, (.|.), (.&.))
|
import Data.Bits (testBit, shiftL, complement, (.|.), (.&.))
|
||||||
@@ -112,4 +113,9 @@ extractBitsFromImage bitsPerByte img =
|
|||||||
let width = imageWidth img
|
let width = imageWidth img
|
||||||
height = imageHeight img
|
height = imageHeight img
|
||||||
pixels = [ pixelAt img x y | y <- [0..height - 1], x <- [0..width - 1] ]
|
pixels = [ pixelAt img x y | y <- [0..height - 1], x <- [0..width - 1] ]
|
||||||
in concatMap (extractBitsFromPixel bitsPerByte) pixels
|
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user