This example is to perform RSA encryption & decryption in iOS (Objective-C). I wrote a blog about encryption and decryption. Now I create this example is improvement on that (In my blog post: Encryption is done totally in Objective-C; Decryption cannot support chunks decryption but written in C), This example shows both encryption & decryption in C and support for longer string.
The library can be found here
Just drag the library (with include
& lib
folders only) to the project. Go to project targets -> Build Settings
:
- Look for Header Search Paths, add
"${SRCROOT}/Libraries/openssl/include"
for example - Look for Library Search Paths, add
"${SRCROOT}/Libraries/openssl/lib"
for example
Generate private key
$ openssl genrsa -out private_key.pem 512
Generate public key from private key
$ openssl rsa -in private_key.pem -pubout -out public_key.pem
Include RSA
#include "RSA.h"
Get the path of your private key (make sure is in **Copy Bundle Resources)
![alt text] (https://raw.github.com/jslim89/RSA-Example/master/screenshots/resource_bundle.png "Copy Bundle Resources")
NSString *privateKeyPath = [[NSBundle mainBundle] pathForResource:@"private_key" ofType:@"pem"];
Decrypt using the function js_private_decrypt
char *plainText = js_private_decrypt([cipher UTF8String], [privateKeyPath UTF8String]);
// convert to NSString form
NSString *result = [NSString stringWithUTF8String:plainText];
NSLog(@"Plain text: %@", result);
(NOTE: Encryption did the same way)