Zanny
Zanny

Reputation: 58

Getting params out of an RSA keypair

I want to either generate an RSA keypair and log the pubkey parameters (modulus and exponent, n and e), get the parameters out of an existing set of keys (in der format) or generate a pubkey pair from my own parameters.

I was trying to do the first with Crypto++, but the ders it spits out are reported as invalid by openssl. Didn't help the Crypto++ wiki and doc pages were down tonight as well!

I then tried researching ways to generate keypairs in openssl by using some of the random key parameters Crypto++ made, but several hours later I'm still stuck trying to decipher all the command line args.

This isn't meant to be a secure key, just a working one for some unit tests in a project I have that gets pubkeys as (n,e) pairs from JWKs and needs to verify it can actually validate signatures properly. But to do that I need the private der and the public (n,e), and there has to be an easier way than trying to use one of the openssl wrapper libs to just log the key params as it generates a keypair.

Upvotes: 1

Views: 426

Answers (1)

jww
jww

Reputation: 102296

I was trying to do the first with Crypto++, but the ders it spits out are reported as invalid by openssl...

(from linked GitHub):

RSA::PublicKey publicKey(params);
FileSink pubsink("pubkey.der");
publicKey.DEREncode(pubsink);

Use publicKey.Save when you want to save the subjectPublicKeyInfo. subjectPublicKeyInfo is the RSA key you are thinking about plus the outer X.509 packaging like OID and version number.


I then tried researching ways to generate keypairs in openssl by using some of the random key parameters Crypto++ made, but several hours later I'm still stuck trying to decipher all the command line args.

You might find Keys and Formats from the Crypto++ wiki helpful. It also supplies the OpenSSL and GnuTLS commands.


I want to either generate an RSA keypair and log the pubkey parameters (modulus and exponent, n and e), get the parameters out of an existing set of keys (in der format) or generate a pubkey pair from my own parameters.

To loop back around to this... There are lots of examples on the wiki at RSA Cryptography RSA Encryption Schemes and RSA Signature Schemes.

Generally speaking, Crypto++ encodes all keys in ASN.1/DER. Load and Save provide the "subject info" part, and DEREncode and BERDecode provide just the "raw key" part. Again, take a look at Keys and Formats.

If you want to process PEM encoded keys, then you need the PEM Pack. Its an add-on, and you have to download it and then build the library with the PEM Pack included.

If you provide concrete data, like a hex encoded key you are trying to process, then we can say more about what you should do.

Upvotes: 1

Related Questions