Hi Trung, One thing I will conform you is " we can not get public key from a private key ". I do not remember where i studied , but it is true. So do not try to get a public key from private key. And I have mistakenly typed as BEGIN RSA CERTIFICATE , as you told it is BEGIN RSA PUBLIC KEY only.
So PEM_read_RSAPublicKey() or PEM_read_RSA_PUBKEY() can be used to load public key from a private key The main concept (and also the general concept )in my program ( or any other programs )is we have to load public key from the certificate only . In the certificte if the public key does not have header like BEGIN RSA PUBLIC KEY , then these functions wont work . so I have used other procedure to get the RSA public key .. if you still have doubts , mail me ... --- Shankar On 4/13/08, Thà nh Trung Nguyễn <[EMAIL PROTECTED]> wrote: > > Thank you for fast response ! > > But it's ok if I use PEM_read_RSAPublicKey() to load public key from file > save by using PEM_write_RSAPublicKey(). > And even publickey extract by openssl command doesn't have any line as > "BEGIN RSA CERTIFICATE". It just has line as "BEGIN RSA PUBLIC KEY". > > I've tried your code, but it can't read public key save by openssl. > > So can PEM_read_RSAPublicKey() or PEM_read_RSA_PUBKEY() can load public > key from a private key ? In my program, I use PEM_write_RSAPublicKey() to > store public key and use PEM_read_RSAPublicKey() to load it. I just want to > know if user can use my program to read public key from a private key. > > p/s : > I used openssl to extract publickey like this ( I don't know if there > is another way) : > openssl rsa -in privatekey.pem -out publickey.pem -pubout > > > *shankar ks <[EMAIL PROTECTED]>* wrote: > > Hi Trung, > The error which u got was you can not read the public key as in the > certificate it is not finding the lines as "BEGIN RSA CERTIFICATE" > so here i am giving you the code , use it directly , it will work fine as > i refined it many times ... and one more thing is error string funtion will > give you the reason also , so it is human readible message only... > > > pubk = RSA_new(); > if(pubk == NULL) > printf("error to create new RSA structure \n"); > fp = fopen (recevercert, "r"); > if (fp == NULL){ > pri ntf("err "); > exit (1); > } > x509 = PEM_read_X509(fp, NULL, NULL, NULL); > fclose (fp); > > if (x509 == NULL) { > printf("error reading the certificate \n"); > ERR_print_errors_fp (stderr); > exit (1); > } > pub=X509_get_pubkey(x509); > if (pub == NULL) { > printf("error reading the public key \n"); > ERR_print_errors_fp (stderr); > exit (1); > } > pubk = EVP_PKEY_get1_RSA(pub); > > > > > On 4/13/08, Th�nh Trung Nguyễn <[EMAIL PROTECTED]> wrote: > > > > Hi ! > > > > I used PEM_write_RSAPrivateKey() to write private key to a pem file. > > Then I used PEM_read_RSAPublicKey() to read public key from that file, > > but it returned an error. > > The ERR_error_string() only return message like : > > error:0906D06C:lib(9):func(109):reason(108) > > So can any one tell me can i use PEM_read_RSAPublicKey() to load public > > key from a private key ? > > And are the any way to get human understandable error message ? > > > > Thanks ! > > > > > > trungnt __________________________________________________ > > Do You Yahoo!? > > Tired of spam? Yahoo! Mail has the best spam protection around > > http://mail.yahoo.com > > > > > > -- > --Best Regards > Shankar > > > > > trungnt > > __________________________________________________ > Do You Yahoo!? > Tired of spam? Yahoo! Mail has the best spam protection around > http://mail.yahoo.com > -- --Best Regards Shankar