如何将 cryptonite RSA.PublicKey 转换为 Base64 格式?
How to convert a cryptonite RSA.PublicKey to Base64 format?
我想使用 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) 希望密钥采用这种格式:
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDlOJu6TyygqxfWT7eLtGDwajtN
FOb9I5XRb6khyfD1Yt3YiCgQWMNW649887VGJiGr/L5i2osbl8C9+WJTeucF+S76
xFxdU6jE0NQ+Z+zEdhUTooNRaY5nZiu5PgDB0ED/ZKBUSLKL7eibMxZtMlUDHjm4
gwQco1KRMDSmXSMkDwIDAQAB
-----END PUBLIC KEY-----
下面的示例代码生成一个 public/private 密钥对,然后打印 public 密钥(密钥是随机的)
import Crypto.PubKey.RSA --from cryptonite
import Prelude
main :: IO ()
main = do
(pub, priv) <- generate 1024 65537
print pub
这是一个示例输出:
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}
现在有没有像 keyToBase64FormatExample
这样的函数可以让下面的代码像第一个示例密钥一样以 Base64 格式打印密钥,这样我就可以在 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
按照@Topaco 和@Maarten Bodewes 的建议,我采用了这个实现:
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
openSslKeyToCryponiteKey
函数将 openSSL 密钥转换为 cryptonite 密钥,因此我可以使用该库来加密和解密消息。
RsaKey
持有我想要的 cryptonite 密钥和字符串表示形式。 genKey
生成一个随机的 RsaKey
,其中包含 n 位和我最初要求的字符串表示形式。
我不知道为什么 rsaDMQ1
、rsaDMP1
和 rsaIQMP
return 一个 Maybe Integer
所以我只是生成一个新密钥以防转换失败。
我想使用 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) 希望密钥采用这种格式:
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDlOJu6TyygqxfWT7eLtGDwajtN
FOb9I5XRb6khyfD1Yt3YiCgQWMNW649887VGJiGr/L5i2osbl8C9+WJTeucF+S76
xFxdU6jE0NQ+Z+zEdhUTooNRaY5nZiu5PgDB0ED/ZKBUSLKL7eibMxZtMlUDHjm4
gwQco1KRMDSmXSMkDwIDAQAB
-----END PUBLIC KEY-----
下面的示例代码生成一个 public/private 密钥对,然后打印 public 密钥(密钥是随机的)
import Crypto.PubKey.RSA --from cryptonite
import Prelude
main :: IO ()
main = do
(pub, priv) <- generate 1024 65537
print pub
这是一个示例输出:
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}
现在有没有像 keyToBase64FormatExample
这样的函数可以让下面的代码像第一个示例密钥一样以 Base64 格式打印密钥,这样我就可以在 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
按照@Topaco 和@Maarten Bodewes 的建议,我采用了这个实现:
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
openSslKeyToCryponiteKey
函数将 openSSL 密钥转换为 cryptonite 密钥,因此我可以使用该库来加密和解密消息。
RsaKey
持有我想要的 cryptonite 密钥和字符串表示形式。 genKey
生成一个随机的 RsaKey
,其中包含 n 位和我最初要求的字符串表示形式。
我不知道为什么 rsaDMQ1
、rsaDMP1
和 rsaIQMP
return 一个 Maybe Integer
所以我只是生成一个新密钥以防转换失败。