Hi Dave, Thanks for your suggestion. I had generated 1024 bytes RSA key. I need to convert this into DER to ASCII. Is this output is right?
int main() { RSA *rsa; int len, len1; rsa = RSA_generate_key(1024, 3, NULL, NULL); size_t size; unsigned char *iend, keybuf[3000]; int i; size = i2d_RSAPrivateKey(rsa, NULL); printf("size %d\n",size); //keybuf=(unsigned char *) malloc(size * sizeof( unsigned char)); 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]);} //printf("%d\n",RSA_check_key(pub_rsa)); RSA_free(rsa); } ki...@kicha-laptop:~/Desktop/FISKrishna$ ./privkey size 608 30|82|2|5c|2|1|0|2|81|81|0|ba|54|61|c0|8e|3d|98|11|ef|3b|a|22|6c|36|5e|45|e8|ee|a2|8a|81|7|82|a1|af|68|7d|25|f1|e7|7a|e6|b5|34|ff|e3|54|5c| bd|d4|7c|24|72|af|6f|b5|7b|ce|ff|4e|f5|b5|cc|24|50|f5|b5|79|96|47|de|93|2c|79|f0|f8|82|fa|b3|2c|97|f7|f5|83|a6|fa|7a|4d|84|c6|51|7|7d|fb|e1|50| 45|67|2c|91|6f|55|d6|6e|c7|79|4a|6e|5f|a5|a4|db|7a|f5|98|8f|5a|de|88|fa|5f|cd|ed|5b|c0|20|70|a3|a5|7b|90|df|b5|a3|8b|7b|9a|3d|2|1|3|2|81|80|7 c|38|41|2b|9|7e|65|61|4a|27|5c|16|f2|ce|e9|83|f0|9f|17|7|0|af|ac|6b|ca|45|a8|c3|f6|9a|51|ef|23|78|aa|97|8d|93|29|38|52|c2|f7|1f|9f|ce|52|89|ff| 89|f9|23|dd|6d|8b|4e|78|fb|b9|85|3f|c|c8|50|27|d1|33|65|9|6c|5e|9f|69|c|58|90|a3|22|6c|62|c9|ca|a8|e3|cc|a3|fc|fa|11|de|d3|d5|8b|fd|f8|c4|3| 59|2a|1e|57|71|f0|7c|df|75|50|17|8|5|34|ad|67|e6|c7|65|78|77|13|d1|7a|8c|21|a0|9a|d3|bc|d3|2|41|0|e4|63|e1|88|5a|46|e6|da|c5|1b|31|e7|9c| 1c|30|48|6c|b3|aa|69|b0|d9|69|4f|a4|35|6|58|f7|94|84|67|68|55|dd|be|94|95|c1|17|a4|6c|4a|3b|69|3|c5|6a|28|85|89|e1|bb|2c|16|b3|c7|5d|80| 5f|d1|e7|e|8f|2|41|0|d0|da|d4|5a|ca|c3|23|2e|12|d5|f0|39|e9|7d|b1|e9|b5|a3|d6|3c|7d|80|e0|a0|6d|8e|2b|3c|8c|dd|4d|eb|dd|12|c2|b9|8d|1a|d1| 22|a4|f3|18|80|93|ee|cb|5f|a8|fc|b|26|80|c4|f1|d|91|b0|2|d2|d1|56|f0|73|2|41|0|98|42|96|5a|e6|d9|ef|3c|83|67|76|9a|68|12|ca|da|f3|22|71|9b| cb|3b|9b|8a|6d|78|ae|e5|fa|63|2|ef|9a|e3|e9|29|b8|63|d6|f|c2|f2|dc|27|9b|57|d8|f1|70|59|6|96|7c|c8|f|22|84|e9|0|3f|e1|44|b4|5f|2|41|0|8b|3c| 8d|91|dc|82|17|74|c|8e|a0|26|9b|a9|21|46|79|17|e4|28|53|ab|40|6a|f3|b4|1c|d3|8|93|89|47|e8|b7|2c|7b|b3|67|36|17|18|a2|10|55|b7|f4|87|95| 1b|52|b2|19|ab|2d|f6|9|b|ca|ac|8c|8b|8f|4a|f7|2|41|0|b1|fa|54|53|2b|27|50|e9|c|77|2c|9|83|e1|d9|fe|67|54|b|74|4e|45|12|cf|9b|34|bc|81|81|30| 71|5e|e0|ff|d4|2e|27|99|9|68|c9|7b|eb|d8|cb|4e|c|7b|b6|eb|37|78|44|9a|10|8b|7|b8|b4|31|84|fa|18|36| For generating public key, int main() { RSA *rsa; int len, len1; rsa = RSA_generate_key(1024, 3, NULL, NULL); size_t size; unsigned char *iend, keybuf[3000]; 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 */ for(i=0;i<size;i++) { printf("%x|", keybuf[i]);} //printf("%d\n",RSA_check_key(pub_rsa)); RSA_free(rsa); ki...@kicha-laptop:~/Desktop/FISKrishna$ ./pubkey size 138 30|81|87|2|81|81|0|c2|58|10|4b|df|c3|c7|a5|9c|a0|1f|5|25|e7|41|9c|4b|4d|42|d5|cf|ed|11|17|d3|f2|10|e7|d6|98|9|21|c8|46|b5|15|8d|ee|86|7|62| 67|3e|65|56|a8|e4|e8|a8|87|ef|da|d9|c8|83|1b|59|6a|44|b|30|ab|cb|f6|dd|3|a1|ba|88|fa|d4|91|cf|19|6c|a8|14|da|94|46|d5|6e|10|fd|ef|5b|b7|bb| 68|e2|41|84|d9|5e|bc|95|4f|d6|7e|89|d|de|89|58|75|40|f2|f5|8|c7|d|90|3a|aa|1f|21|b4|e7|ae|d4|43|47|56|ed|78|6f|99|35|2|1|3| Kris On Sat, Sep 11, 2010 at 2:08 AM, Dave Thompson <dthomp...@prinpay.com>wrote: > > From: owner-openssl-us...@openssl.org On Behalf Of krishnamurthy > santhanam > > Sent: Friday, 10 September, 2010 05:51 > > > 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> > > (The code you posted before, and I quoted, was 1024 not 2048. > Either is valid, but it confuses things to mix them.) > > > > 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 > <snip 6*80+33 hex (lowercase) chars> > > That output wasn't from the code above, and isn't valid hex > for any DER or even any bytes. Probably same bug as below. > > > 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 */ > > If you use a static-size buffer like this keybuf[] > (and it's large enough) you don't need to do the > size=i2d_(,NULL) call first. That is useful if you > want to malloc() the exact space as the code you > posted earlier did, or check overflow before storing. > > > for(i=0;i<size;i++) { > > printf("%x", keybuf[i]);} > > > > size 609 > <snip 14*80+45=1165 hex chars> beginning: > > > > 308225d210281810ef1ed52b301ac82bf74553aa4e6d3f8ad967147224cc8dce22ef158d9907 > b7c7 > > That is different from the code you posted earlier, > and is wrong. %x prints only one hex digit for values > 0-15, so you get only 1165 of the 1218 chars needed > to represent 609 bytes in undelimited hex. 609 bytes > is a good size for a RSA 1024bit privatekey, but its > first bytes must be 30 82 02 5D 02 01 00 02 81 81 00 . > Just look at your output and you can see there's no > way for a receiver to recover those bytes. > > Another possibility would be to use %x but with a > nondigit like comma or space separating each byte. > > > > ______________________________________________________________________ > OpenSSL Project http://www.openssl.org > User Support Mailing List openssl-users@openssl.org > Automated List Manager majord...@openssl.org >