objective-cencryptioncryptographyaes

AES string encryption in Objective-C


My Objective-C app needs to do string encryption (specifically ).

I've found that AES is the most secure encryption methodology available for consumer use. I also have an understanding of how to convert strings to NSData and back... (just a beginner). Unfortunately many webpages and Q&As about encryption with AES are unclear. None of them state how to use the code given.

This may be less of an "encryption" question, and more of a "how do I use these methods" question, so please bear with me.

I've found these example methods to encrypt and decrypt an NSString:

#import "<CommonCrypto/CommonCryptor.h>"
@implementation NSMutableData(AES)

For encryption:

- (NSMutableData *)encryptAES:(NSString *)key {
    char keyPtr[kCCKeySizeAES256+1];
    bzero(keyPtr, sizeof(keyPtr));

    [key getCString: keyPtr maxLength: sizeof(keyPtr) encoding: NSUTF16StringEncoding];
    size_t numBytesEncrypted = 0;

    NSUInteger dataLength = [self length];

    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);

    NSMutableData *output = [[NSData alloc] init];

    CCCryptorStatus result = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, keyPtr, kCCKeySizeAES256, NULL, [self mutableBytes], [self length], buffer, bufferSize, &numBytesEncrypted);
 
    output = [NSMutableData dataWithBytesNoCopy:buffer length:numBytesEncrypted];

    if (result == kCCSuccess) {
        return output;
    }
    return NULL;
}

For decryption:

- (NSMutableData *)decryptAES:(NSString *)key andForData:(NSMutableData *)objEncryptedData {
    char  keyPtr[kCCKeySizeAES256+1];
    bzero( keyPtr, sizeof(keyPtr) );

    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF16StringEncoding];

    size_t numBytesEncrypted = 0;

    NSUInteger dataLength = [self length];

    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer_decrypt = malloc(bufferSize);    
    NSMutableData *output_decrypt = [[NSData alloc] init];
    CCCryptorStatus result = CCCrypt(kCCDecrypt , kCCAlgorithmAES128, kCCOptionPKCS7Padding, keyPtr, kCCKeySizeAES256, NULL, [self mutableBytes], [self length], buffer_decrypt, bufferSize, &numBytesEncrypted);

    output_decrypt = [NSMutableData dataWithBytesNoCopy:buffer_decrypt length:numBytesEncrypted];

    if (result == kCCSuccess) {
        return output_decrypt;
    } 
    return NULL;
}

This is a method I wrote that I would like to use with the above methods:

- (void)encrypt {
    // Convert NSString to NSData so that it can be used to encrypt the Input
    NSString *input = [inputBox text];
    NSData *inputData = [input dataUsingEncoding: NSUTF8StringEncoding];
    // What to do here...?
}

How do I use the encryptAES and decryptAES methods? Where do they go in my implementation file?


Solution

  • 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.