LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   sending RSA public/private keys into calling function (http://www.linuxquestions.org/questions/programming-9/sending-rsa-public-private-keys-into-calling-function-831178/)

krishna_san 09-09-2010 07:47 AM

sending RSA public/private keys into calling function
 
Hi,

I am newbie to openssl...

My calling application will accept only strings and interger.

we are replacing RSA bsafe library to openssl. using RSA bsafe, we have generated the private and public key in BER format. Then convert the keys, BER format into ASCII format to send the calling function. (these everything done by using RSA supplied bsafe library)

same way i have to right using openssl..i m now able convert the RSA public and private key into DER format

rsa = RSA_generate_key(1024, 3, NULL, NULL);
{
size_t size;
unsigned char *iend, *keybuf;
int i;
size = i2d_RSAPublicKey(rsa, NULL);
printf("size %d\n",size);
keybuf = (unsigned char *) malloc(size * sizeof( unsigned
char));
iend = keybuf;
size = i2d_RSAPublicKey(rsa, &iend);
/* size returns the size of public key in bytes */
printf("\n");
printf("key :");
for(i=0;i<size;++i) {
printf("\n%02X", keybuf[i]);

1. is it possible to convert this DER format into ASCII?

2. I am able to print the keybuf value into stdout in the as mentioned below

size 138
key : 30818702818100BAEF6AB1AD2503FFDC900B612BA2BCED9AF74E337A43B21D1FF69A30651AD7A492C1E199CB40A9DF693

is it possible to convert the key into DER format again?

=========================================
if i use %c instead %X it is printing some junk..is it right?

for(i=0;i<size;++i) {
printf("\n%c", keybuf[i]);

ou^Y9eP>@3nJp%}K8sY* O\

Thanks in advance,
kris

neonsignal 10-10-2010 05:55 AM

Quote:

Originally Posted by krishna_san (Post 4092438)
1. is it possible to convert this DER format into ASCII?

You don't say what you mean by an ASCII format. If you just mean a hex string (as you gave in your example), then you can use sprintf to convert it in a buffer (or code the conversion yourself). If you mean base64 strings, then there are libraries you can use.

Quote:

2. Is it possible to convert the key into DER format again?
Same as above, use sscanf or a conversion library.

Quote:

if i use %c instead %X it is printing some junk..is it right?
It is as expected, because you are printing out binary data as if it is characters; some of these characters are not printable, and some are not even ASCII (top bit set). This is not a usable way to represent the data.


All times are GMT -5. The time now is 05:01 PM.