Converting RSA public key Modulus and Exponent into PEM file

Some days ago, I battled reading a raw RSA public key on iOS and using it to encrypt some data with on an iPad. Part of that battle was taking a “modulus” and “exponent” value from an RSA public key, and converting it into a PEM encoded public key file. I created this little C program to do this. It is using “openssl” and “libcrypto” to do its work.

In the case others, or I, some day need this, I created a gist for it here:

I’ve developed and tested this on OS X 10.7.4 with Xcode 4.3.2 installed. It can be built using the makefile which is also part of the gist:

I have no idea if this works properly on other platforms.

June 1, 2012 В· polesen В· One Comment
Tags: , , , ,  В· Posted in: Programming

One Response

  1. Carlos - March 16, 2013


    I’ve spent 3 days searching the web until i found you post. I’m not certainly a genius in RSA matters. If i understood correctly your post you are generating the key from a base64 modulus and exponent. My question is what if i have my modulus in hex format like n=”C696034213D7D8546984579D1D0F0EA519CFF8DEFFC429354CF3A871A6F7183F1228DA5C7470C055387100CB935A712C4E2864DF5D64BA93FE7E63E71F25B1E5F5298575EBE1C63AA617706917911DC2A75AC28B251C7EF40F2365912490B939BCA2124A30A28F54402C34AECA331AB67E1E79B285DD5771B5D9FF79EA630B75″ and e=”03″

    Do i have to convert first to base64? or i can handle them in hex format? I tried to convert to BIGNUM using BN_hex2bn but i’m not sure that it is working: