Coder Social home page Coder Social logo

iosrsapublickeyencryption's Introduction

iOSRSAPublicKeyEncryption describes how to encrypt data from a PUBLIC KEY in iOS using RSA.

The main functions are in SecKeyHelper.h:

// Loads a certificate located at certPATH (usually in your bundle)
SecKeyRef SecKeyFromPathAndSaveInKeyChain( NSString* certPATH, CFDataRef keyChainId )

// Loads a SecKeyRef from Keychain (that you previously loaded from some certPATH)
SecKeyRef SecKeyFromKeyChain( CFDataRef keyChainId )

Example of how to use is in the testSecKey function in ViewController.m

      FACTS      

  1. YOU'RE NOT SUPPOSED TO LOAD PUBLIC KEYS IN IOS FROM ANYTHING OTHER THAN A "CERTIFICATE". NO BASE64 ENCODED ----- BEGIN PUBLIC KEY ------ STRINGS ARE SUPPORTED ON IOS BY DEFAULT.

  2. CERTIFICATES ARE EASY TO CREATE USING OpenSSL OR certutil ON WINDOWS The basic steps are:

         HOW TO MAKE A CERTIFICATE
    

Make the -----RSA PRIVATE KEY----- file in PEM format

$ openssl genrsa -out privKey.pem 2048

Make the -----CERTIFICATE REQUEST-----

$ openssl req -new -key privKey.pem -out certReq.pem

Make the actual -----CERTIFICATE-----

$ openssl x509 -req -days 2000 -in certReq.pem -signkey privKey.pem -out certificate.pem

Make the DER certificate.crt file from the certificate.pem

$ openssl x509 -outform der -in certificate.pem -out certificate.cer

SEE ALSO: stackoverflow SEE ALSO: OpenSSL HOWTO

DO NOT FOLLOW WINGOFHERMES' METHOD FOR LOADING PUBLIC KEYS FROM BASE64 CODED STRINGS. THIS IS NOT SUPPORTED FOR A REASON AND IS NOT THE RECOMMENDED CODE PATH.

YOU'VE BEEN WARNED.

RELEVANT DEVFORUMS.APPLE THREADS:

  1. USE CERTIFICATES:

In general we recommend that you distribute key material to clients as either a certificate (for public keys) or a PKCS#12 (for private keys or identities). iPhone OS has good support for importing these types of data.

  1. IF YOU HAVE THE DER DATA, YOU CAN CREATE A CERTIFICATE

If you have a blob of data in DER form, you can create a SecCertificateRef from it using SecCertificateCreateWithData. Once you have a certificate ref, you can extract the public key using SecTrustCopyPublicKey. There's one gotcha with this, as explained in the following post. https://devforums.apple.com/message/114555#114555

  1. HOW TO LOAD A CERTIFICATE

This is surprisingly easy. You don't need to add the certificate to the keychain to handle this case. Rather, just load the certificate data (that is, the contents of a .cer file) in your application (you can either get this from your bundle or off the network) and then create a certificate ref using SecCertificateCreateWithData. From there you can extract a public key ref using a SecTrust object (SecTrustCreateWithCertificates, SecTrustEvaluate -- you can choose to ignore the resulting SecTrustResultType -- and SecTrustCopyPublicKey).
And from there you can encrypt and verify using the SecKey APIs (SecKeyEncrypt, SecKeyRawVerify).

License

The code in this package is released under the ZLib license.

Copyright (C) 2013 William Sherif

This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.

Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

iosrsapublickeyencryption's People

Contributors

superwills 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

Watchers

 avatar  avatar  avatar  avatar

iosrsapublickeyencryption's Issues

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.