Re: Re: creating RSA private/public key pair
Hi, Check out following functions... i2d_RSAPublicKey() and d2i_RSAPublicKey() hope this helps, Manish -- Manish Chablani ([EMAIL PROTECTED]), Graduate Student, Computer Science Department, Indiana University. Make today a LAM/MPI day !!! http://www.lam-mpi.org/ -- >Date: 23 Jul 2002 23:31:05 - >MIME-Version: 1.0 >From: "ganesh kumar godavari" <[EMAIL PROTECTED]> >To: [EMAIL PROTECTED] >Cc: "Aleix Conchillo" <[EMAIL PROTECTED]> >Subject: Re: Re: creating RSA private/public key pair >Hi Alex, >thanks for the help now i am able to encrypt and decrypt them >using public and private keys. > >i have one more question. i want to send the public key of the >client to the server for authentication. is there any way i can >print the RSA public key into a string so that i can send them >over the sockets? > >i cannot find any functions for printing the RSA public key into a >string. > >enclosing my code below > >Thanks a ton, > >ganesh > >#include >#include >#include > >RSA * readPubKey(char *filename) > { > RSA *key; > BIO *bp; > > ERR_load_crypto_strings(); > bp=BIO_new(BIO_s_file()); > > if (BIO_read_filename(bp,filename) <= 0) > { > perror("ERROR: public.pem"); > exit(0); > } > > if ((key=(RSA >*)PEM_read_bio_RSA_PUBKEY(bp,NULL,NULL,NULL)) == NULL) > { > ERR_print_errors_fp(stderr); > key = NULL; > } > > BIO_free(bp); > return key; > } > >RSA * readPrivKey(char *filename) > { > RSA *key; > BIO *bp; > > SSLeay_add_all_algorithms(); > ERR_load_PEM_strings(); > > bp=BIO_new(BIO_s_file()); > if (BIO_read_filename(bp,filename) <= 0) > { > perror("ERROR: rsakey.pem"); > exit(0); > } > > > if ((key=(RSA >*)PEM_read_bio_RSAPrivateKey(bp,NULL,NULL,NULL)) == NULL) { > ERR_print_errors_fp(stderr); > key = NULL; > } > > BIO_free(bp); > return key; > > } > >int main(void) > { >RSA *pubkey; >RSA *privkey; >unsigned char *message= "Howdy ganesh talking on behalf of >zeewaves !! can u hear me now !!"; >unsigned char *encryptmess, *decryptmess; >int long_message; > >pubkey = readPubKey("public.pem"); >privkey = readPrivKey("private.pem"); > >printf("size of (in byte)s pu:pr :: %d:%d\n", >RSA_size(pubkey),RSA_size(privkey)); > >/* Encrypt the message */ >encryptmess= (unsigned char *) malloc (RSA_size(pubkey)); >long_message= (strlen(message)*sizeof(char)+1); >RSA_public_encrypt(long_message, message, encryptmess, pubkey, >RSA_PKCS1_OAEP_PADDING); > >/* decrypt the original message */ >decryptmess= (unsigned char *) malloc(RSA_size(privkey)); >RSA_private_decrypt(RSA_size(privkey), encryptmess, >decryptmess, privkey, RSA_PKCS1_OAEP_PADDING); > >printf (" message %s\n decrypt %s\n", message, decryptmess); > >} > > > >On Tue, 23 Jul 2002 Aleix Conchillo wrote : >>On 23 Jul 2002 17:44:15 -, ganesh kumar godavari wrote: >> >> > hello group, >> > i have create RSA private key using >> > openssl genrsa -out KEY.pem 1024 >> > openssl rsa -in KEY.pem -out private.pem >> > openssl rsa -in KEY.pem -pubout -out public.pem >> > >> >>hi >> >>first, you don't need to call >> >>openssl rsa -in KEY.pem -out private.pem >> >>the first command "genrsa" generates a private key already, so >>you'll >>have: >> >>openssl genrsa -out private.pem 1024 >>openssl rsa -in private.pem -pubout -out public.pem >> >> > >> > i try to read the public.pem the following way >> > >>[snip] >> > >> >if ((x=(RSA *)PEM_read_RSAPublicKey(fp,NULL, NULL,NULL)) >>!= >> > NULL) >>[snip] >> > >> > it gives me an error saying that >> > >> >>that's because there's two ways to read public keys. a public key >>can >>be rsa, dsa and dh (may be more in openssl i don
Re: Re: creating RSA private/public key pair
Hi Alex, thanks for the help now i am able to encrypt and decrypt them using public and private keys. i have one more question. i want to send the public key of the client to the server for authentication. is there any way i can print the RSA public key into a string so that i can send them over the sockets? i cannot find any functions for printing the RSA public key into a string. enclosing my code below Thanks a ton, ganesh #include #include #include RSA * readPubKey(char *filename) { RSA *key; BIO *bp; ERR_load_crypto_strings(); bp=BIO_new(BIO_s_file()); if (BIO_read_filename(bp,filename) <= 0) { perror("ERROR: public.pem"); exit(0); } if ((key=(RSA *)PEM_read_bio_RSA_PUBKEY(bp,NULL,NULL,NULL)) == NULL) { ERR_print_errors_fp(stderr); key = NULL; } BIO_free(bp); return key; } RSA * readPrivKey(char *filename) { RSA *key; BIO *bp; SSLeay_add_all_algorithms(); ERR_load_PEM_strings(); bp=BIO_new(BIO_s_file()); if (BIO_read_filename(bp,filename) <= 0) { perror("ERROR: rsakey.pem"); exit(0); } if ((key=(RSA *)PEM_read_bio_RSAPrivateKey(bp,NULL,NULL,NULL)) == NULL) { ERR_print_errors_fp(stderr); key = NULL; } BIO_free(bp); return key; } int main(void) { RSA *pubkey; RSA *privkey; unsigned char *message= "Howdy ganesh talking on behalf of zeewaves !! can u hear me now !!"; unsigned char *encryptmess, *decryptmess; int long_message; pubkey = readPubKey("public.pem"); privkey = readPrivKey("private.pem"); printf("size of (in byte)s pu:pr :: %d:%d\n", RSA_size(pubkey),RSA_size(privkey)); /* Encrypt the message */ encryptmess= (unsigned char *) malloc (RSA_size(pubkey)); long_message= (strlen(message)*sizeof(char)+1); RSA_public_encrypt(long_message, message, encryptmess, pubkey, RSA_PKCS1_OAEP_PADDING); /* decrypt the original message */ decryptmess= (unsigned char *) malloc(RSA_size(privkey)); RSA_private_decrypt(RSA_size(privkey), encryptmess, decryptmess, privkey, RSA_PKCS1_OAEP_PADDING); printf (" message %s\n decrypt %s\n", message, decryptmess); } On Tue, 23 Jul 2002 Aleix Conchillo wrote : >On 23 Jul 2002 17:44:15 -, ganesh kumar godavari wrote: > > > hello group, > > i have create RSA private key using > > openssl genrsa -out KEY.pem 1024 > > openssl rsa -in KEY.pem -out private.pem > > openssl rsa -in KEY.pem -pubout -out public.pem > > > >hi > >first, you don't need to call > >openssl rsa -in KEY.pem -out private.pem > >the first command "genrsa" generates a private key already, so >you'll >have: > >openssl genrsa -out private.pem 1024 >openssl rsa -in private.pem -pubout -out public.pem > > > > > i try to read the public.pem the following way > > >[snip] > > > >if ((x=(RSA *)PEM_read_RSAPublicKey(fp,NULL, NULL,NULL)) >!= > > NULL) >[snip] > > > > it gives me an error saying that > > > >that's because there's two ways to read public keys. a public key >can >be rsa, dsa and dh (may be more in openssl i don't remember now). >with >the function PEM_read_RSAPublicKey OpenSSL is expecting a >concrete RSA >Public key which will have in the header of the PEM file > >-BEGIN RSA PUBLIC KEY- > >instead of > >-BEGIN PUBLIC KEY- > >if you'd like to load an RSA key with the "BEGIN PUBLIC KEY" >header, >you should use PEM_read_RSA_PUBKEY function instead of the one >you use. > >this header will be common for dsa, rsa and dh keys. > >uppps... got to catch the bus. hope this helps you. > >regards, > >aleix >__ >OpenSSL Project >http://www.openssl.org >User Support Mailing List >[EMAIL PROTECTED] >Automated List Manager >[EMAIL PROTECTED] __ OpenSSL Project http://www.openssl.org User Support Mailing List[EMAIL PROTECTED] Automated List Manager [EMAIL PROTECTED]
Re: creating RSA private/public key pair
On 23 Jul 2002 17:44:15 -, ganesh kumar godavari wrote: > hello group, > i have create RSA private key using > openssl genrsa -out KEY.pem 1024 > openssl rsa -in KEY.pem -out private.pem > openssl rsa -in KEY.pem -pubout -out public.pem > hi first, you don't need to call openssl rsa -in KEY.pem -out private.pem the first command "genrsa" generates a private key already, so you'll have: openssl genrsa -out private.pem 1024 openssl rsa -in private.pem -pubout -out public.pem > > i try to read the public.pem the following way > [snip] > >if ((x=(RSA *)PEM_read_RSAPublicKey(fp,NULL, NULL,NULL)) != > NULL) [snip] > > it gives me an error saying that > that's because there's two ways to read public keys. a public key can be rsa, dsa and dh (may be more in openssl i don't remember now). with the function PEM_read_RSAPublicKey OpenSSL is expecting a concrete RSA Public key which will have in the header of the PEM file -BEGIN RSA PUBLIC KEY- instead of -BEGIN PUBLIC KEY- if you'd like to load an RSA key with the "BEGIN PUBLIC KEY" header, you should use PEM_read_RSA_PUBKEY function instead of the one you use. this header will be common for dsa, rsa and dh keys. uppps... got to catch the bus. hope this helps you. regards, aleix __ OpenSSL Project http://www.openssl.org User Support Mailing List[EMAIL PROTECTED] Automated List Manager [EMAIL PROTECTED]