2

I have the binary blobs for the components of a private key including modulus, publicExponent, privateExponent, prime1, prime2, exponent1, exponent2 and coefficient, what is the easiest way to generate a PEM/DER file from them? Is there a easy to use Python API that can be used or do I have to use C? Likewise if I have the components of a public key as binary blobs, including modulus and exponent, what's the easiest method there? Another question in this process: are the python APIs to test the property of the components, like weather the prime is a prime or weather the public and private components match?

2 Answers 2

2

This can be achieved pretty easily with pycryptodome

Here are some sample values that were generated as a test

In [1]: from Crypto.PublicKey import RSA
In [2]: k = RSA.generate(1024)
In [3]: k
Out[3]: RsaKey(n=143130316039186356537289646457342957029055874083006179752018267628632429252822850383503747585724799519287456198657693682737294996945803556004091588888862734842876499600553435771912216934557891984348187747076769031035627391163918136413321448399135545921574551907381303095829931709226724033859864712257647103161, e=65537, d=17576525985067546537255398853909945804199790570517932383908982984806045296957723116505762555043916971042700268349132837308234281938749515826493875328098129245342983246848248582675980856522190387684789361785762364603511517782326537037037597067485821717857467592522600776711703683226858254562757226108937574081, p=10753783485237760558106558145489319968952855295476064861811826896190777901077938075707538967825984471343848916518461647244807051451613883176849073895898863, q=13309763604192726567108341676142815607992017489636400387824449743334965995406164568181340806599419803897222088829477205186369416995900434016351317269868247, u=11832702411409256011378550201142107940763428930323983942371881106145078470279115081362293142803804740351361635022958520667847860090448209808590495750654916)

In [4]: n = k.n
In [5]: e = k.e
In [6]: d = k.d
In [7]: p = k.p
In [8]: q = k.q

Once you have a (n,e) tuple you can export a public key. Once you have a (n,e,d) tuple you can generate a private key too (p,q are optional).

In [9]: print(RSA.construct((n,e)).public_key().export_key())
b'-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDL0weqmsQCfkL6OLdalWlVFGgN\nw2FLrXujCsTo9SwEkSyab5UsUvX0fTnl/uH9d18g9136zsQPpsv1Ylh4ElW7/BCX\n8TBa4exSdKUTS7ishHYfNJ2kXKZMlws9aLCkS4weagvF83c9fMjoQ74E69BkqQDE\ndDlIBdcLA3fNSSdMuQIDAQAB\n-----END PUBLIC KEY-----'

In [10]: print(RSA.construct((n,e,d,p,q)).public_key().export_key())
b'-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDL0weqmsQCfkL6OLdalWlVFGgN\nw2FLrXujCsTo9SwEkSyab5UsUvX0fTnl/uH9d18g9136zsQPpsv1Ylh4ElW7/BCX\n8TBa4exSdKUTS7ishHYfNJ2kXKZMlws9aLCkS4weagvF83c9fMjoQ74E69BkqQDE\ndDlIBdcLA3fNSSdMuQIDAQAB\n-----END PUBLIC KEY-----'

In [12]: print(RSA.construct((n,e,d,p,q)).export_key())
b'-----BEGIN RSA PRIVATE KEY-----\nMIICXAIBAAKBgQDL0weqmsQCfkL6OLdalWlVFGgNw2FLrXujCsTo9SwEkSyab5Us\nUvX0fTnl/uH9d18g9136zsQPpsv1Ylh4ElW7/BCX8TBa4exSdKUTS7ishHYfNJ2k\nXKZMlws9aLCkS4weagvF83c9fMjoQ74E69BkqQDEdDlIBdcLA3fNSSdMuQIDAQAB\nAoGAGQehOWIoD+ZRc0jju0v902TeIlKL8C8tr6fy5mi1Lxpkz9JED11gttVp9sSG\nHAo8tF+sOtCJYyKoiUm6c4RM4sB1dSqqnaOQEkCpxEcuFMzHNaXzgFVNM6TNykO4\nV0anZLMEW4/3g4Yxkx4CRHUhhk+s/xlvWvkqwk8Z69woFsECQQDNU2YUAjNm0SJy\nHds3NxVieTR05dZEkcWVscNZiEVcZqvYc6Pz1ME6knrNFi6nany22Wk55YMHFXiE\nh3DCd/7vAkEA/iDE8g3TkAu78l5Yn1+ea+l/9eJbTRbJnQqep8I7VwN+9hPaGX49\nOOOunzM1triAFSpy8ZjYkq1buZZwLoLu1wJAGLAWbgF1vL8YrS/508HDyHtaW1Pn\nV4dPgphFLNa9wEZ4EyaUaBUExs4mBdLM+URMio/JnzSBdLCYNRcz764N8QJBAKXj\nhEyyI+HLFyRO3DElPQgag9JhsdHvxyqBjTHbg9r4SD+gk+XCV3q0fgAkcLLXW5z1\nedUmPnH5QoAyqQZjqD8CQBbJQ2v19z6gHiTXMJ560u9bTD0tJYuwvwGTD5EaW+nD\noH4Hp2POjAqF29CXlZpjCyhWOmPn7LMZgrTDSnDOS3I=\n-----END RSA PRIVATE KEY-----'
1

pycryptodome

You could give pycryptodome a try. Beware, it is meant as a replacement of pycrypto. If you have the need to run pycrypto in parallel, you can use the separate, stand-alone package pycryptodomex.

It implements all crypto primitives from scratch (instead of relying on external libraries) and it offers a Key Construct Method that allows to construct a key from the components. If switched on, it can also perform a consistency check on the entered key components.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.