Skip to content

Instantly share code, notes, and snippets.

@MAGANER
Created December 30, 2022 17:25
Show Gist options
  • Save MAGANER/c3cd7b24a8c20bf2ba80e7bfb1be4247 to your computer and use it in GitHub Desktop.
Save MAGANER/c3cd7b24a8c20bf2ba80e7bfb1be4247 to your computer and use it in GitHub Desktop.
haskell module to process XOR encryption/decryption
module Coder(encode,decode) where
import Data.Word
import Data.Char(ord,chr)
import Data.Bits(xor)
import qualified Data.ByteString.Char8 as C
import qualified Data.ByteString as B
tp n ls = takeWhile(not.null) $ map(take n.flip drop ls) [0,n..]
conv_key key = [fromIntegral $ ord x | x <- key]
xored a b = [xor x y | x <- a, y <- b]
process chunked bin_key = foldl (++) [] [xored bin_key x | x <- chunked]
encode::String -> [Word8] -> [Word8]
encode key bin =
let
chunked = tp (length key) bin
bin_key = conv_key key
in
process chunked bin_key
decode::String -> [Word8] -> String
decode key bin =
let
chunked = tp (length key) bin
bin_key = conv_key key
in
C.unpack $ B.pack $process chunked bin_key
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment