Created
December 30, 2022 17:25
-
-
Save MAGANER/c3cd7b24a8c20bf2ba80e7bfb1be4247 to your computer and use it in GitHub Desktop.
haskell module to process XOR encryption/decryption
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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