Discussion:
[openssl-users] Convert RSA key string to PEM file.
(too old to reply)
Sugumar
2016-01-01 09:29:56 UTC
Permalink
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.
If u have any sample programs pls post it also for better understanding.



--
View this message in context: http://openssl.6102.n7.nabble.com/Convert-RSA-key-string-to-PEM-file-tp61971.html
Sent from the OpenSSL - User mailing list archive at Nabble.com.
Ken Goldman
2016-01-01 19:40:49 UTC
Permalink
Post by Sugumar
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);
Sugumar
2016-01-02 04:36:54 UTC
Permalink
Ya your example looks good. Thanks.
And i have another method, i have created BIO by passing that key to
BIO_new_mem_buf function then from that BIO i got a RSA structure using
PEM_read_bio_RSAPublicKey function.

Is that will work for me???

Code:

RSA* CreateRSAStruct(void* data, GetKey type)
{
RSA *rsa= NULL;
BIO *rsaKeyBio;
rsaKeyBio = BIO_new_mem_buf(data, -1); //creates read only memory BIO

if (rsaKeyBio==NULL)
{
std::cout<< "Failed to create key BIO" <<std::endl;
return NULL;
}
if(type)
rsa = PEM_read_bio_RSAPublicKey(rsaKeyBio, &rsa,NULL, NULL);
else
rsa = PEM_read_bio_RSAPrivateKey(rsaKeyBio, &rsa,NULL, NULL);

BIO_free(rsaKeyBio);
return rsa;
}



--
View this message in context: http://openssl.6102.n7.nabble.com/Convert-RSA-key-string-to-PEM-file-tp61971p61977.html
Sent from the OpenSSL - User mailing list archive at Nabble.com.

Loading...