Cocoa wrapper for iOS cryptography

Java has many APIs that I miss when programming in Objective-C, but by far the most painful loss has been the Java Cryptography Extension. I’m not an expert in cryptography, but there’s an elegance in that API that is missing from Apple’s CDSA and CommonCrypto mashup available natively on iOS.

So, I thought it would be nice to abstract some of the features behind Objective-C protocols:

  • KeyGenerator: creates new SecretKey objects
  • SecretKey: represents a symmetric key
  • KeySpec: represents pre-existing key material for generating SecretKey objects using a SecretKeyFactory
  • SecretKeyFactory: generates a SecretKey from pre-existing key material
  • Cipher: represents an engine for either encrypting plaintext or decrypting ciphertext with a symmetric algorithm and an existing SecretKey
  • RSAKey: I didn’t do much to abstract this. This should really be called “AsymmetricKey”.

All of the code is up on GitHub. Here’s a quick example of how you can generate a new 128-bit AESKey:

And how you can generate an AESKey from a password using the PBKDF2 specification:

Note: I’m using ARC, so if you’re importing the code into an older iOS application, please keep that in mind.