luoyanbei / RSA_iOS

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

Donate

Click here to lend your support to: RSA-objc and make a donation at pledgie.com !

License

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

About


Languages

Language:C 77.1%Language:C++ 21.3%Language:Objective-C 1.4%Language:PHP 0.2%