user1431272
user1431272

Reputation: 93

Xcode Openssl RSA decryption function needed

I am reading an encrypted string from an application in xcode and I have to write a function that uses RSA decryption to decode and display the message.

I am completely lost on where to begin with this.

I have Openssl complied in xcode and I am using the openssl/rsa.h file.

I am trying to use the function:

RSA_private_decrypt(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding);

But then I'd read somewhere on the Openssl main site that the function just returns a number and not the actual string. I also have no idea what paramenters to pass through.

The only reference I have found is the openssl/rsa.h file and looking at the functions it contains.

I've tried doing some research the past couple hours but I have no found any answers.

I was wondering if there is a simple function that I can pass my encrypted string and my private key (using a file or hardcoded) and it can return the decrypted string?

If not is there a guide on how to use Openssl with Objective C programming?

Please let me know if you need more information on the issue.

Thank you in advance.

Upvotes: 3

Views: 1728

Answers (1)

Dirk-Willem van Gulik
Dirk-Willem van Gulik

Reputation: 7706

You may want to look Apple's example which uses security transforms (this avoids openssl) in their Security Overview.

With a bit of luck you can do things with apple transforms and go with that programme.

If not - or if for some reason you really want to use openssl; then the openssl source contains the example file openssl-0.9.8t/apps/rsa.c which pretty much allows for selective cut-and-paste to make things work.

Doing man RSA_private_decrypt from the command line will show you the manual page (or from within Xcode to the man page). Or see http://www.openssl.org/docs/crypto/RSA_public_encrypt.html.

Example use for the above:

unsigned char in[] = { 1, 2, ... byte array to decrypt };
// size of that in byte array
int inlen = sizeof(in);

// output buffer size depends on the key type.
char * out = malloc(RSA_size(rsa)); 

int e = RSA_private_decrypt(inlen, in, out, rsa, RSA_PKCS1_PADDING);

where padding is one of the values from the man-page.

The value of rsa is a bit more complex to initialise as this is where you set up your keys and what not. Check the above rsa.c file for examples of various ways of filling it - it normally boils down to something like:

EVP_PKEY *pkey = load_key( ... , password,... );
rsa = EVP_PKEY_get1_RSA(pkey);

where load_key is borrowed from the app examples of openssl.

Upvotes: 1

Related Questions