[openssl-users] Convert RSA key string to PEM file.

Ken Goldman kgoldman at us.ibm.com
Fri Jan 1 19:40:49 UTC 2016


On 1/1/2016 4:29 AM, Sugumar wrote:
> Hi,
>
> I have a RSA Public key as string type.
> I need to convert this string to PEM file. Please help me to create PEM file
> from string type.

Convert the string to a binary array.
Convert the array n and e to bignum
         *bn = BN_bin2bn(bin, bytes, *bn);

Create an RSA structure and assign n and e to the bignums
         rsa_pub_key = RSA_new();
         (rsa_pub_key)->n = n;
         (rsa_pub_key)->e = e;
         (rsa_pub_key)->d = NULL;

	RSA *rsa_pub_key
     	EVP_PKEY 	*pkey = NULL;
	pkey = EVP_PKEY_new();
	irc  = EVP_PKEY_assign_RSA(pkey, rsa_pub_key);
	pemFile = fopen(pemFilename, "wb");
	irc = PEM_write_PUBKEY(pemFile, pkey);
	fclose(pemFile);





More information about the openssl-users mailing list