Created
March 15, 2013 09:39
-
-
Save wavewave/5168649 to your computer and use it in GitHub Desktop.
RSA example
This file contains hidden or 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
| {-# LANGUAGE OverloadedStrings #-} | |
| module Main where | |
| import Crypto.Random | |
| import Codec.Crypto.RSA | |
| import qualified Data.Binary as Bi | |
| import qualified Data.ByteString.Base64.Lazy as LB64 | |
| import qualified Data.ByteString.Lazy.Char8 as LB | |
| main :: IO () | |
| main = do | |
| putStrLn "RSA test" | |
| g <- newGenIO :: IO SystemRandom | |
| let (pub,priv,g') = generateKeyPair g 1024 | |
| let pubstr = (LB64.encode . Bi.encode) pub | |
| privstr = (LB64.encode . Bi.encode) priv | |
| let pub' = (Bi.decode . either (error "parse pub") id . LB64.decode) pubstr | |
| priv' = (Bi.decode . either (error "parse priv") id . LB64.decode) privstr | |
| let (bstr,g'') = encrypt g pub' "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." | |
| print (decrypt priv' bstr) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment