AES string encryption in Objective-C

This line near the top says you’re adding AES functionality to NSMutableData:

@implementation NSMutableData(AES)

In Objective-C, this is called a category; categories let you extend an existing class.

This code would typically go in a file named NSMutableData-AES.m. Create a header file too, NSMutableData-AES.h. It should contain:

@interface NSMutableData(AES)
- (NSMutableData*) EncryptAES: (NSString *) key;
@end

Include (#import) that header in your main file. Add a call to the encryption function in your code:

NSData *InputData = [Input dataUsingEncoding:NSUTF8StringEncoding];
NSData *encryptedData = [InputData EncryptAES:@"myencryptionkey"];

Similarly for decryption.

Leave a Comment