Hi, Thanks for your explanation. i have to create RSA Public/Praivate key and send back to my application. My application will read only character and string format , it will not accept RSA format.. please guide me how to do that?
I had return below program for that but it is not resolving the purpose... #include<string.h> #include<openssl/rsa.h> #include<stdio.h> #include<openssl/pem.h> #include<unistd.h> int main() { RSA *rsa,*pub_rsa,*priv_rsa; unsigned char *priv_key; unsigned char *pub_key; int len, len1, size; unsigned char *encrypted; unsigned char *mess = "test message!"; unsigned char *p; const unsigned char **pp; unsigned char *decrypt_mess; FILE *Keyfd = NULL; FILE *Pubfd = NULL; //Generate key rsa = RSA_generate_key(1024, RSA_3, NULL, NULL); //keeping into char buffer public key size = i2d_RSAPublicKey (rsa, NULL); //how i can get this public key pub_key = p = (unsigned char *) malloc(size * sizeof(unsigned char)); i2d_RSAPublicKey (rsa, &p); pub_rsa = d2i_RSAPublicKey(NULL,&pub_key,size); PEM_write_RSA_PUBKEY(stdout,pub_rsa); ///keeping private key into buffer size = i2d_RSAPrivateKey(rsa, NULL); priv_key = pp = (unsigned char *) malloc(size * sizeof(unsigned char)); i2d_RSAPrivateKey (rsa, &pp); priv_rsa = d2i_RSAPrivateKey(NULL,&priv_key,size); if( priv_rsa==NULL ) { fprintf(stderr,"priv key error!\n"); return 0; } PEM_write_RSAPrivateKey(stdout,priv_rsa,NULL, NULL, 0, NULL, NULL); len1 = (strlen(mess)*sizeof(unsigned char)+1); encrypted = (unsigned char *) malloc ((size_t) RSA_size(pub_rsa)); len= RSA_public_encrypt(len1, mess, encrypted, pub_rsa, RSA_PKCS1_PADDING); printf("encrypted: %s len: %d\n",encrypted, len); if(!(decrypt_mess = (unsigned char *) malloc ((size_t) RSA_size(priv_rsa)))) fprintf(stderr,"can't allocate memory for encrypted text!\n"); printf("decrypting!\n"); len= RSA_private_decrypt(RSA_size(priv_rsa), encrypted, decrypt_mess, priv_rsa, RSA_PKCS1_PADDING); printf("decrypted: %s len:%d\n",decrypt_mess,len); free(encrypted); free(decrypt_mess); RSA_free(pub_rsa); RSA_free(priv_rsa); RSA_free(rsa); } Thanks in advance, Krishnamurthy On Sat, Sep 4, 2010 at 5:16 AM, Dave Thompson <dthomp...@prinpay.com> wrote: > > From: owner-openssl-us...@openssl.org On Behalf Of krishnamurthy > santhanam > > Sent: Thursday, 02 September, 2010 14:17 > > To: openssl-users@openssl.org > > Subject: Re: reading and writing into pem file > > This message is not about this subject. Please use subject > lines that match the message you post. And the indentation > of your code is erratic; it would be easier to read if it > were consistent. If you are using any kind of IDE or > programmer's editor it should be able to do this for you; > otherwise see if you can get GNU indent. > > > Thanks for your input..it worked for me...i am writing > > encryption and decryption using RSA... > > This looks like a learning exercise, which is fine as far > as it goes. I hope you realize that RSA encryption of data > directly is rarely if ever used in practice. With good > padding like OAEP it can be secure, but the data size > limits are too restrictive for most general uses. OAEP > does need some entropy, and depending on your system > environment you may need to seed the PRNG for that to > work, which your current code apparently doesn't do. > The more common symm+RSA/etc. schemes also need entropy. > > > RSA * readPubKey(char *filename) > <snip> > > > rsaEncrypt() > > { > > RSA *pubkey; > > unsigned char *message= "Hello !! can u hear me now !!"; > > unsigned char *encryptmess, *decryptmess; > > int long_message; > > char **key; > > char buff[300]; > > pubkey = readPubKey("public.pem"); > > char test1,test2; > > printf("\nsize of (in byte)s public:private :: > %d:\n",RSA_size(pubkey)); > > > /* Encrypt the message */ > > encryptmess = (unsigned char *) malloc (RSA_size(pubkey)); > > should check for malloc failure > > > test1=sizeof(test2); > > long_message= (strlen(message)&test1+1); > > This makes no sense. It sets long_message to 0 if I've counted > right, or else 2. Neither of these is the length of the data > you apparently want to encrypt. sizeof(char) is always 1 in C, > by definition; this is a FAQ or at rather FGA on comp.lang.c. > > > RSA_public_encrypt(long_message, message, encryptmess, pubkey, > RSA_PKCS1_OAEP_PADDING); > > always check for error and at minimum report something; > usually best to use ERR_print_errors[_fp] (see the man page) > > > printf ("\n message %s\n", message); > > > printf("\nlenght= %d\n",strlen(encryptmess)); > > int i; > > i =strlen(encryptmess); > > printf("length ==%d\n",i); > > memmove(buff,encryptmess,i); > > These are totally wrong. The result of RSA encryption (or any > modern algorithm) is binary data, not a C string. It most likely > contains zero byte values which will cause these to truncate the > value, but by chance might contain no zeros, and not be followed > by one, which will cause this run off into garbage or even fault. > Use the length returned from RSA_public_encrypt, or RSA_size. > Then make sure buff is big enough. And if you are going to use a > fixed size buff, you might as well encrypt into it directly *after > checking it's big enough* and save the clutter of malloc/free/etc. > > Similarly the data that is encrypted (and later decrypted) -- > "plaintext" or "cleartext" -- can be and usually is binary data. > A series of (ASCII) characters is valid, but unusual. > > > int j; > > j=(strlen(buff)*sizeof(char)+1); > > //printf ("\n message=== %s\n", encryptmess); > > printf("j=%d\n",j); > > free(encryptmess); > > printf("%s",buff); > > Even if you moved the correct data into buff and it fit, > you didn't terminate it and hadn't initialized buff > so it probably contained garbage so these compute > the wrong length and print the wrong thing. > Use the length returned from RSA_public_encrypt, or RSA_size. > > > } > > <snip rest which appears to be mostly duplicate> > > > > ______________________________________________________________________ > OpenSSL Project http://www.openssl.org > User Support Mailing List openssl-users@openssl.org > Automated List Manager majord...@openssl.org >