Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
43 lines (30 sloc) 1.13 KB
import Control.Monad
import Data.List
import Data.Maybe
import OpenSSL
import OpenSSL.EVP.Cipher
import OpenSSL.EVP.Open
import OpenSSL.EVP.PKey
import OpenSSL.EVP.Seal
import OpenSSL.PEM
import OpenSSL.RSA
import Text.Printf
main = withOpenSSL $
do putStrLn "cipher: DES-CBC"
des <- liftM fromJust $ getCipherByName "DES-CBC"
putStrLn "generating RSA keypair..."
rsa <- generateRSAKey 512 65537 Nothing
let plainText = "Hello, world!"
putStrLn ("plain text to encrypt: " ++ plainText)
putStrLn ""
putStrLn "encrypting..."
(encrypted, [encKey], iv) <- seal des [fromPublicKey rsa] plainText
putStrLn ("encrypted symmetric key: " ++ binToHex encKey)
putStrLn ("IV: " ++ binToHex iv)
putStrLn ("encrypted message: " ++ binToHex encrypted)
putStrLn ""
putStrLn "decrypting..."
let decrypted = open des encKey iv rsa encrypted
putStrLn ("decrypted message: " ++ decrypted)
binToHex :: String -> String
binToHex bin = concat $ intersperse ":" $ map (printf "%02x" . fromEnum) bin
Jump to Line
Something went wrong with that request. Please try again.