Created
July 4, 2014 09:29
-
-
Save rethab/8ad0058c908b1fcc209a to your computer and use it in GitHub Desktop.
cryptocipher en-/decryption example
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
-- contrived from the example in the cryptocipher API | |
module Main where | |
import Crypto.Cipher | |
import qualified Data.ByteString as BS | |
import qualified Data.ByteString.Char8 as CBS | |
main = do | |
let ctx = initAES256 key | |
putStrLn "Enter Plaintext (must be multiple of 16)" | |
plain <- BS.getLine | |
let secret = cbcEncryption ctx iv plain | |
let decrypted = cbcDecryption ctx iv secret | |
putStrLn "Decrypted: " | |
CBS.putStrLn decrypted | |
-- size must equal that of the block for cipher | |
-- should be random | |
iv = BS.replicate 16 3 | |
key = BS.pack [1,2,3,4,5,6,7,8,9,0 | |
,1,2,3,4,5,6,7,8,9,0 | |
,1,2,3,4,5,6,7,8,9,0 | |
,1,2] | |
initAES256 :: BS.ByteString -> AES256 | |
initAES256 = either (error . show) cipherInit . makeKey | |
cbcEncryption :: AES256 -> BS.ByteString -> BS.ByteString -> BS.ByteString | |
cbcEncryption ctx ivRaw plainText = cbcEncrypt ctx iv plainText | |
where iv = maybe (error "invalid IV") id $ makeIV ivRaw | |
cbcDecryption :: AES256 -> BS.ByteString -> BS.ByteString -> BS.ByteString | |
cbcDecryption ctx ivRaw encrypted = cbcDecrypt ctx iv encrypted | |
where iv = maybe (error "invalid IV") id $ makeIV ivRaw |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment