Thanks Dave, I really thanks for all your inputs and suggestions, I have not pasted fully last time all the output...below is the output
> rsa = RSA_generate_key(2048, 3, NULL, NULL); <snip: get size, alloc keybuf, 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]); size 270n 30821a282110bbc3dbffffc749b71387b38371ca73af4f335250b42103fa6492d7a438d749de0295 164496cd2a634e7b22ee04985ed6f161bcb78b041dc62fe2a16fd8e8c70e08f6f8d9e15fba596e57 71f6212861ba6b5d79193aeada98123b416f126b4614948e60739bde2ac4da874fe51ffb5cf0ff39 2b324ca85fb87bd4d4ce28ff86edc69a9951b7f0ca8a6f9c3fdf7d3cdc8755e90d371d4cdb9e7432 28fc5dd88cc396591501be6bf3efb1a7ebf9fcd31dfd840336e4ae270257df9124b381573ddbf67d cb28e25731d96f61e2cd69fee5208a9f09f255476fea16f712b78f22c2ed6010d845bb6ec4848cc6 77c9ca020ecc8c22a01cf172c0c123101 The below one is private key, rsa = RSA_generate_key(1024, 3, NULL, NULL); size_t size; unsigned char *iend, keybuf[3000]; char keybuf1[3000]; int i,j,n; size = i2d_RSAPrivateKey(rsa, NULL); printf("size %d\n",size); iend = keybuf; size = i2d_RSAPrivateKey(rsa, &iend); /* size returns the size of public key in bytes */ for(i=0;i<size;i++) { printf("%x", keybuf[i]);} size 609 308225d210281810ef1ed52b301ac82bf74553aa4e6d3f8ad967147224cc8dce22ef158d9907b7c7 756823de46474ff745347bdeab79af9b369e1127e94efdf47c85f23865413332da259537b4ac17fb 57d43a5728a57b066b36a562126b77b7de17297c0410f9fa892c745ee2efdfc918a2bec2b6d84291 6c9db7bb77b85981f8a0b7e52132818109f69e3722011dac7fa2e37c6def37fb1e644b84c18885e8 956c9f63b3bb55a7a84f8f017e9842f8aa4d8cda7e9c7a67512246960c54634a94da8594c2598d62 0d80db95bf3a948ef48e34b1e547f10e1fb364cd2e17d3e570567bf18d685611d76614c3adaac433 25ea542975b8ea334adb7153a641f11bb5385e8cdb82b2410f9513655018f8684cc316e4f71c173f f7f46fbac44a015f9a6ec3131ba6f844e23df4d9cf7fd51f59895cf3f38e3c7eb8efadbd9a6c80af 369746e12d12410f587a6b22dae35c247a7682e0d7b47e657972eb6ec762e3a9ae7387aa507d879a 5531ef942396bf51329a8a579df473c366b947c464b1c603cd5a1d55e10d52410a6362438aabb504 58882f434f68f7ffaa2f4b272d86ab95119f2cb5767c4a583417ea2b3bdfaa8e14e65b934d4ad97d a9d09fc929119dab1f79ba2eb4c8b2410a3afc476c91ece81851a457408fcda998fba1f249da41ed 1bc9a25a718afe5a66e3769fb817b9d4e0cc676e4b13f84d2cef262fd843212ead339168e3eb5e32 410a4f0ffc5b4ab5bd25cec788f4ced018e4e4847dc3a3538e2a83fc2e78bb3aa437642113e4ea4e 8d477ab70c3f5b4ab5bda15384dce961929bad9c03e5a thanks for your time, Krishnamurthy On Fri, Sep 10, 2010 at 3:46 AM, Dave Thompson <dthomp...@prinpay.com>wrote: > > From: owner-openssl-us...@openssl.org On Behalf Of krishnamurthy > santhanam > > Sent: Thursday, 09 September, 2010 08:02 > > > My application will accept 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 > > Posted code only does public, which is probably good see below. > > > rsa = RSA_generate_key(1024, 3, NULL, NULL); > <snip: get size, alloc keybuf, 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]); > > This can't be the code that produced your data below. It would > put every hex byte (two chars) on a separate line. (Except the > last, which if left unterminated won't work on some platforms.) > > > 1. is it possible to convert this DER format into ASCII? > > ASCII isn't a format; ASCII is a character code that can be used > for many formats. Code like you show converts (binary) DER to > hex of DER in ASCII, or some people say DER as hex in ASCII. > (And the DER is of PKCS#1 RSAPublicKey, that's implicit here.) > Is that (or will it be) the format 'my application' wants? > > <pedantic> Actually hex of DER in your C implementation's > charcode, very commonly ASCII or a superset but C doesn't > require that; there are still EBCDIC machines -- and OpenSSL > is supposed to work on them, though I can't test </> > > > 2. I am able to print the keybuf value into stdout ... > > size 138 > > key : > > 30818702818100BAEF6AB1AD2503FFDC900B612BA2BCED9AF74E337A43B21D1FF69A30651AD7 > A492C1E199CB40A9DF693 > > This is obviously incomplete (nowhere near 138*2 hex chars) > (which is right for 1024 d=3) so I assume it got truncated. > > > is it possible to convert the key into DER format again? > > If you get (complete) hex-of-DER into another C program, > you can just convert from hex back to (binary) DER. E.g.: > > char hexstr [whatever]; /* contains chars 3 0 8 1 8 7 etc */ > unsigned int hexlen = validlength; /* determined somehow */ > unsigned char der [whatever]; /* or *der = malloc(hexlen/2) */ > unsigned int i, t; > for( i = 0; i < hexlen/2; i++ ){ > if( sscanf (&hexstr[i*2], "%02X", &t) != 1 ) error; > der[i] = t; > } > /* or if you prefer, perhaps slightly more efficient */ > unsigned int i; char t [3]; > for( i = 0; i < hexlen/2; i++ ){ > memcpy (t, &hexstr[i*2], 2); t[2] = '\0'; > der[i] = strtol (t, NULL, 16); > } > > If you want/need to have line breaks in your format, > add and remove or skip them as appropriate. > > Note DER is not directly usable in OpenSSL; it's just a > standard and convenient transfer/storage format. If/once > you have DER of an RSA public key, use d2i_RSAPublicKey(). > Similarly for private key if you actually want to send it, > although as I said before that's usually a bad design. > > > > ______________________________________________________________________ > OpenSSL Project http://www.openssl.org > User Support Mailing List openssl-users@openssl.org > Automated List Manager majord...@openssl.org >