Home > OS >  How to convert a cryptonite RSA.PublicKey to Base64 format?
How to convert a cryptonite RSA.PublicKey to Base64 format?

Time:12-23

I'd like to use the cryptonite package to encrypt and decrypt data that I send between a server and the browser. The library that I'm using in javascript (jsencrypt) expects the key to be in this format:

-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDlOJu6TyygqxfWT7eLtGDwajtN
FOb9I5XRb6khyfD1Yt3YiCgQWMNW649887VGJiGr/L5i2osbl8C9 WJTeucF S76
xFxdU6jE0NQ Z zEdhUTooNRaY5nZiu5PgDB0ED/ZKBUSLKL7eibMxZtMlUDHjm4
gwQco1KRMDSmXSMkDwIDAQAB
-----END PUBLIC KEY-----

This example code below generates a public/private key pair and then prints the public key (the keys are random)

import Crypto.PubKey.RSA --from cryptonite
import Prelude

main :: IO ()
main = do
    (pub, priv) <- generate 1024 65537
    print pub

This is an example output:

PublicKey {public_size = 1024, public_n = 
838810208632138001112335937359712915337605988171901851257333
066049179300171141530075373751582673299603275572461676942377
012821222892095571420901264721091772472668581960494794002128
465070883630864484928336068763609006732043379714710048765805
634996970518442068464314269921483395567949770139392138039869
804840964254095897246931307529911869877679660487847321586270
590643147091317460396672569960631676467382972746529477769411
527861524509326048469254343400361972179753023172099919540360
693236846546000124594252361262642276672554442433133440264594
277209836999361586816548738976463921568319100088954202197688
276704633076760655840253733990270874714659923002535139595211
004737522438506363933030649639801907820288618272117385871231
769206317193775868605405633842741858015227010240594480188395
540742051371928807238862824408681424665848743305280357603511
757167246620696038759449552451115192829743551594438380613327
628041809777911094107497338955314488477505805227855444099773
387799746563394582986779902726940618766326337678868062185153
289512732085048592816005701758541549444352783241333352123943
568145369789314952255045673674625747415426584028996370153747
488837015688428724665234318655388293938106868275505939024937
897661905787602231415607235644852362963568299763544424278925
594069652992439362551971175016895533953014943622370478873549
897527774955029482878353681628232352746557330769981298900705
182867893343776554361360438596086653915646328927671082831289
613771645773887419463084626112329171846455382084999933763720
792331968098934355347172820249196977728184628237367387935219
569277450138645496147189421334500588872066950053824702026885
821811801494040124318201764298861290870360246781998773215160
300191266418888591952637979383215503663267264790507799677274
353108347683531154918836409296771233974313532333998828151170
945544078869704911747651968319603895904398689023311225700598
225001105304818453295597106784867117228082664620538047712482
472312119639175360503229103263315749626509899154059160345812
254236536834916620116566982574865770475369911075028190099269
941741473785879845928403312978453064042611603361515988271416
534914407978791540091287831655967611830557922734997146973141
483575412948328053605437298159882744259837662169403427869004
642786368752300938606783224789001108094902975125177349133801
039920052996856661514316129772040756948911720327941025184110
389620429021283187544775248808015260951326436038221585049716
274917056876763645447699179331504231719644176255511357492448
918413, public_e = 65537}

Now is there some function like keyToBase64FormatExample such that the code below prints the key in the Base64 format just like the first example key such that I can use it in javascript?

import Crypto.PubKey.RSA --from cryptonite
import Prelude

main :: IO ()
main = do
    (pub, priv) <- generate 1024 65537
    print $ keyToBase64FormatExample pub -- this function is just a place holder

CodePudding user response:

As suggested by @Topaco and @Maarten Bodewes I went with this implementation:

import qualified Crypto.PubKey.RSA as Rsa--from cryptonite
import OpenSSL.PEM -- from HsOpenSSL
import OpenSSL.RSA -- from HsOpenSSL
import Prelude

data RsaKey = RsaKey {
        rsaKeyCryptoniteKey :: Rsa.PrivateKey,
        rsaKeyStringRepr :: String
    } deriving Show

openSslKeyToCryptoniteKey :: RSAKeyPair -> Maybe Rsa.PrivateKey
openSslKeyToCryptoniteKey key = do 
                                let d = rsaD key
                                let p = rsaP key
                                let q = rsaQ key
                                let mdP = rsaDMP1 key
                                let mdQ = rsaDMQ1 key
                                let mqinv = rsaIQMP key 
                                let size = rsaSize key
                                let n = rsaN key
                                let e = rsaE key
                                dP <- mdP
                                dQ <- mdQ
                                qinv <- mqinv

                                let pub = Rsa.PublicKey size n e
                                return $ Rsa.PrivateKey pub d p q dP dQ qinv 

genKey :: Int -> IO RsaKey
genKey bits = do 
    keys <- generateRSAKey bits 65537 Nothing -- change '65537' and 'Nothing' to the parameters you'd like to have
    stringRepr <- writePublicKey keys
    let cryptoKey = openSslKeyToCryptoniteKey $ keys
    case cryptoKey of
        Just key -> return $ RsaKey key stringRepr
        otherwise -> genKey bits

main :: IO ()
main = do
    s <- genKey 2048
    print s

The openSslKeyToCryponiteKey function converts a openSSL key to a cryptonite key so I can use the library to encrypt and decrypt the messages. RsaKey holds the cryptonite key and the String representation that I wanted. And genKey generates a random RsaKey with n bits and the String representation that I was originally asking for.

I don't know why rsaDMQ1, rsaDMP1 and rsaIQMP return a Maybe Integer so I just generate a new key in case the conversion fails.

  • Related