/RSA-objc

Example for RSA encryption & decryption on iOS

Primary LanguageObjective-CMIT LicenseMIT

RSA for Objective-C

This is a plugin for RSA encryption & decryption in iOS (Objective-C). I wrote a blog about encryption and decryption.

Setup

1. Download openssl library

The library can be found here

2. Include library to project

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

3. Include RSA-objc into your project

Drag the folder RSA-objc to your project.

4. Generate Key pair

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

Drag the key(s) to your project, and make sure it appear in Copy Bundle Resources.

alt text

5. Add bridging header (only if you want to port to Swift)

Go to Project Targets -> select Build Settings, look for Objective-C Bridging Header

alt text

Make sure you set the correct path according to your file structure.

The SwiftBridgingHeader.h is included in this example

How to use

Include RSA

Objective-C

#import "JSRSA.h"

// set the public/private key
[JSRSA sharedInstance].publicKey = @"public_key.pem";
[JSRSA sharedInstance].privateKey = @"private_key.pem";

NSString *plainText = [[JSRSA sharedInstance] privateDecrypt:cipherText];
...

Swift

JSRSA.sharedInstance().privateKey = "private_key.pem"
JSRSA.sharedInstance().publicKey = "public_key.pem"

var plainText = JSRSA.sharedInstance().privateDecrypt(cipherText)

There are 4 methods here:

- (NSString *)publicEncrypt:(NSString *)plainText;
- (NSString *)privateDecrypt:(NSString *)cipherText;
- (NSString *)privateEncrypt:(NSString *)plainText;
- (NSString *)publicDecrypt:(NSString *)cipherText;

Try the demo project

To pull the openssl for iOS

$ git submodule update --init --recursive

Contributors

Credit

License

The RSA-objc is open-sourced software licensed under the MIT license.