Skip to content

Instantly share code, notes, and snippets.

@noahmiller
Created July 25, 2012 05:38
Show Gist options
  • Save noahmiller/3174605 to your computer and use it in GitHub Desktop.
Save noahmiller/3174605 to your computer and use it in GitHub Desktop.
EncryptionTransformer class with initialization vector
- (id)transformedValue:(NSData*)data
{
...
// Use another NSData category method (left as an exercise
// to the reader) to randomly generate the IV data
NSData* iv = [NSData randomDataOfLength:32];
data = [data dataAES256EncryptedWithKey:[self key] Iv:iv];
// Return a data object that includes the IV with the
// encrypted data appended
NSMutableData* mutableData = [NSMutableData dataWithData:iv];
[mutableData appendData:data];
return mutableData;
}
- (id)reverseTransformedValue:(NSData*)data
{
...
// The IV was stored in the first 32 bytes of the data
NSData* iv = [data subdataWithRange:NSMakeRange(0, 32)];
// Remove the IV from the encrypted data and decrypt it
NSMutableData* mutableData = [NSMutableData dataWithData:data];
[mutableData replaceBytesInRange:NSMakeRange(0, 32) withBytes:NULL];
return [mutableData dataAES256DecryptedWithKey:[self key] Iv:iv];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment