51 lines
2.0 KiB
Haskell
51 lines
2.0 KiB
Haskell
module Main (main) where
|
|
|
|
import Codec.Picture
|
|
import qualified Data.Vector.Unboxed as VU
|
|
import Lib
|
|
|
|
|
|
caesarShift :: Int
|
|
caesarShift = 66
|
|
|
|
bitsPerByte :: Int
|
|
bitsPerByte = 1
|
|
|
|
|
|
main :: IO ()
|
|
main = do
|
|
putStrLn "Чтение текста из файла \"resources/biography.txt\""
|
|
inputText <- readFile "resources/biography.txt"
|
|
|
|
putStrLn "\nШифрование текста"
|
|
let alphabet = createAlphabetFromText inputText
|
|
putStrLn $ "Размер алфавита: " ++ show (length alphabet)
|
|
let encryptedText = encryptCaesar alphabet caesarShift inputText
|
|
let encryptedTextBits = textToBits encryptedText
|
|
|
|
putStrLn "\nКодирование текста в изображение"
|
|
readSourceImageResult <- readImage "resources/david.bmp"
|
|
case readSourceImageResult of
|
|
Left err -> putStrLn $ "Ошибка при чтении изображения: " ++ err
|
|
Right dynImg -> do
|
|
let img = convertRGB8 dynImg
|
|
let width = imageWidth img
|
|
let height = imageHeight img
|
|
let totalBits = width * height * 3 * bitsPerByte
|
|
let bitsPadded = encryptedTextBits VU.++ VU.replicate (totalBits - VU.length encryptedTextBits) 0
|
|
|
|
let resultImage = generateImage (encodePixel bitsPerByte img bitsPadded) width height
|
|
saveBmpImage "tmp/david.bmp" (ImageRGB8 resultImage)
|
|
|
|
|
|
putStrLn "\nДекодирование текста из изображения"
|
|
readEncodedImageResult <- readImage "tmp/david.bmp"
|
|
case readEncodedImageResult of
|
|
Left err -> putStrLn $ "Ошибка при чтении изображения: " ++ err
|
|
Right dynImg -> do
|
|
let img = convertRGB8 dynImg
|
|
let bits = VU.fromList $ extractBitsFromImage bitsPerByte img
|
|
let fullText = bitsToText bits
|
|
let encryptedTextFromImage = takeWhile (/= '\NUL') fullText
|
|
let decryptedText = decryptCaesar alphabet caesarShift encryptedTextFromImage
|
|
writeFile "tmp/biography.txt" decryptedText |