Created
July 25, 2012 05:38
-
-
Save noahmiller/3174605 to your computer and use it in GitHub Desktop.
EncryptionTransformer class with initialization vector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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