Skip to content

Instantly share code, notes, and snippets.

@wavewave
Created March 15, 2013 09:39
Show Gist options
  • Select an option

  • Save wavewave/5168649 to your computer and use it in GitHub Desktop.

Select an option

Save wavewave/5168649 to your computer and use it in GitHub Desktop.
RSA example
{-# 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