tags:

views:

37

answers:

2

I have information about n,p,q where n=pq and p and q are large prime numbers to be used for the RSA algorithm. For example:

n: C053094BFABF26D431CF33E028770DBB15F4AE76820B5150181F1BF42C5CF3AA7BDB459ACA77D610497F94FFA017BC07EF030E3D3450CAE7E706F352B7D7575CA6B89A5B3C953028E562F7F698C97FDD490EDF4788F073362C743B70AF2C61A17FA495E5801CC8EA1A732C9E8985DB4E8A22EAB97407806F8D7CDDF0BF3CD9F3

e: 10001

p: D9CC00CD811FB052A0EF27332597DA89AAA6B042A1A01A8944229EE680C964148BB07AFBD2EBE467CC9B28E41B5897132F9AFDCD7C5B794CD37E3245A0BC18F5

q: E20F35A3B49B49A35DE25E285EE9B2DC5F3B5FDDD281892F4BE3C54768CBE09272667FF137C5ED9CADD42FF18A8B08FFA9A82C0CF26169B0940F60BEF2AD7647

I want to generate a PEM private and public key from that values.

I guess they are on hexadecimal format, but I really don't know which is contained in a key in pem format. I know that PEM just means that the key is base64 with some other additions footers (begin public etc.).

Any suggestions or any example code?

I mean my goal is from that value obtain two file in PEM private and public keys to be passed to openssl for example.

Many thanks, Andrea

+1  A: 

PEM-encoded RSA keys are basically the binary keys in the format described by PKCS#1 (RFC 3447), base-64 encoded and surrounded by an ASCII header and footer.

Note that PKCS#1 format requires that a private key include not just d, e, p, and q but also the other values used for Chinese Remainder Theorem (d mod (p-1), d mod (q-1), and (inverse of q) mod p). These are easily computed if you know p and q.

There's a bit more to it if the keys are encrypted, as you should then also have some PEM headers that describe the encryption.

There is sample code in the OpenSSL sources (though it's hardly commented at all, and quite hard to follow). You're better off reading the relevant RFCs, really, and checking your code against OpenSSL to make sure you have got it right.

dajames
A: 

openssl/pem.h has following functions for reading/writing RSA keys (from a file) PEM_write_RSAPrivateKey PEM_read_PrivateKey

PEM_write_RSAPublicKey PEM_read_RSAPublicKey

if it still unclear how to use them just ask. I shall provide you with a few examples :-)

If you can provide me some example I would appreciate a lot! Many thanks for your kindness.
Kerby82