Code Monkey home page Code Monkey logo

rsaescryptor's Introduction

RSA+AES Cryptor

RSA+AES Encryption/Decryption library for iOS. This library uses 2048-bit RSA and 256-bit key with 128-bit block size AES for encryption/decryption.

Encryption

Load public key from X509 Certificate file (DER format), encrypt NSData using AES algorithm with generated Key and IV, returns encrypted NSData with format below.

| AES IV | encrypted AES Key | AES encrypted data |
|  0-15  |       16-271      |        272-n       |

Sample code:

NSString *pubKeyPath = [[NSBundle mainBundle] pathForResource:@"publicKey" ofType:@"cer"];

RSAESCryptor *cryptor = [RSAESCryptor sharedCryptor];
[cryptor loadPublicKey:pubKeyPath];
NSData *encData = [cryptor encryptData:plainData];

// encrypted data format:
// [16 bytes IV] + [256 bytes encrypted Key] + [AES encrypted data].

Decryption

Load private key from PKCS#12/PFX format file, decrypt NSData according to the format described above.

NSString *priKeyPath = [[NSBundle mainBundle] pathForResource:@"privateKey" ofType:@"p12"];

RSAESCryptor *cryptor = [RSAESCryptor sharedCryptor];
[cryptor loadPrivateKey:priKeyPath password:password];
NSData *decData = [cryptor decryptData:encData];

How to create .cer and .p12 in Windows

1. Open Visual Studio Command Prompt
2. C:\> makecert -pe -ss My -sr LocalMachine -n CN="YOUR_CERT_NAME" -sky exchange -len 2048
3. C:\> mmc
4. Add Certificates snap-in in MMC console, choose computer accounts
5. expand Personal/Certificate folder, then export which you want.

How to decrypt using C# (File-based cert)

// get cert from pfx file.
var pfxFileName = @"C:\privateKey.p12";
var password = @"PFX_PASSWORD";
var cert = new X509Certificate2(pfxFileName, password);

var rsa = (RSACryptoServiceProvider)cert.PrivateKey;
byte[] data = SomeClass.GetRSAESCryptorEncryptedByteArray();

var iv = data.Take(16).ToArray();
var encSymKey = data.Skip(16).Take(256).ToArray();
var encData = data.Skip(16 + 256).ToArray();

var symKey = rsa.Decrypt(encSymKey, false);

var aes = AesManaged.Create();
using (var inStream = new CryptoStream(new MemoryStream(encData), aes.CreateDecryptor(symKey, iv), CryptoStreamMode.Read))
{
	// read data from inStream
}

How to get cert from Certificate Store in Windows

// get cert from X.509 store of local machine
var store = new X509Store(StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
var cert = store.Certificates.Cast<X509Certificate2>()
			.FirstOrDefault(c => c.Subject == "CN=YOUR_CERT_NAME");
store.Close();

rsaescryptor's People

Contributors

bigsan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rsaescryptor's Issues

RSAESCryptor SecTrustCreateWithCertificates certificateRef

//RSAESCryptor line 100:
OSStatus status = SecTrustCreateWithCertificates(certificateRef, policyRef, &trustRef);
//error:
Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)
certificateRef = 0x0
//my code in swift:
var filePath = NSBundle.mainBundle().pathForResource("myfile", ofType: "pem")
//file read correct
var cryptor = RSAESCryptor.sharedCryptor()
cryptor.loadPublicKey(filePath!) // error

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.