Hi,

I am using the below code snippet and am able to do the encytption.

But I need to convert the encrypted format to Binary format inorder to pass
the encrypted key to a different system. Any one pls suggest how to do the
same ???

*Code snippet :*
**

*void** EncrCardPin(char *pin,char** *encpin)*

*{*

*unsigned char* encrypted = (unsigned char** *) malloc(256);*

**

**

*int** bufSize;*

*char *pubname="publkey.pem"**;*

**

*FILE *keyfile = NULL;*

*RSA* rsa= NULL;*

*FILE *test = NULL;*

*test=fopen(**"test.txt","wb"**);*

**

*printf(**"Opening the puclic key file: [%s]\n"**,pubname);*

*keyfile = fopen(pubname, **"r"**);*

**

*if** (!keyfile)*

*{*

*printf(**"error occured while reading public key file"**);*

**

*}*

*printf(**"Reading the public key block from: [%s] \n"**, pubname);*

*rsa = PEM_read_RSA_PUBKEY(keyfile, NULL, NULL, NULL);*

*if** (rsa == NULL)*

*{*

*printf(**"Badness has occured! Did not read public key file\n"**);*

**

*}*

*else*

*{*

*printf(**"Opened public key file OK!\n"**);*

*}*

*printf(**"Encrypting the message [%s] using public key \n"**,pin);*

*bufSize = RSA_public_encrypt(strlen(pin), (**unsigned char** *) pin,
encrypted, rsa, RSA_PKCS1_PADDING);*

*if** (bufSize == -1)*

*{*

*printf(**"Badness has occured! encryption failed\n"**);*

*RSA_free(rsa);*

**

*}*

*else*

*{*

*printf(**"Encrypted the message OK! \n"**);*

*//memcpy(encrypted,encpin,1500);*

*sprintf(encpin,**"%s"**,encrypted);*

*fprintf(test,**"%s"**,encpin);*

*printf(**"The buffersize is : [%d]\n ================"**,bufSize);*

*}*

*RSA_free(rsa);*

*fclose(keyfile);*

*fclose(test);*

**

*}*


On Thu, Mar 31, 2011 at 9:47 AM, Adrian D. Sacrez <adrian...@verifone.com>wrote:

>  Hi,
>
> I need help on how to do RSA encryption using Openssl in C language. Is
> there a code snippet on how to do this?
> Also, how do I extract or get the key from RSA_generate_key_ex()? Is the
> key generated from this function a private/public key
>
> Thanks,
>
> Adrian
>
>

Reply via email to