Tuesday, 16 December 2014

AES 128 bit encryption / decryption in iOS

// NSData+AES128.h


//
//  NSData+AES128.h
//  AES128
//
//  Created by GauravDS on 16/12/14.
//

#import 

@interface NSData (AES128)

- (NSData *)AES128EncryptedDataWithKey:(NSString *)key;

- (NSData *)AES128DecryptedDataWithKey:(NSString *)key;

- (NSData *)AES128EncryptedDataWithKey:(NSString *)key iv:(NSString *)iv;

- (NSData *)AES128DecryptedDataWithKey:(NSString *)key iv:(NSString *)iv;

@end


// NSData+AES128.m
//
//  NSData+AES128.m
//  AES128
//
//  Created by GauravDS on 16/12/14.
//

#import "NSData+AES128.h"
#import 

@implementation NSData (AES128)

- (NSData *)AES128EncryptedDataWithKey:(NSString *)key
{
    return [self AES128EncryptedDataWithKey:key iv:nil];
}

- (NSData *)AES128DecryptedDataWithKey:(NSString *)key
{
    return [self AES128DecryptedDataWithKey:key iv:nil];
}

- (NSData *)AES128EncryptedDataWithKey:(NSString *)key iv:(NSString *)iv
{
    return [self AES128Operation:kCCEncrypt key:key iv:iv];
}

- (NSData *)AES128DecryptedDataWithKey:(NSString *)key iv:(NSString *)iv
{
    return [self AES128Operation:kCCDecrypt key:key iv:iv];
}

- (NSData *)AES128Operation:(CCOperation)operation key:(NSString *)key iv:(NSString *)iv
{
    char keyPtr[kCCKeySizeAES128 + 1];
    bzero(keyPtr, sizeof(keyPtr));
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
    
    char ivPtr[kCCBlockSizeAES128 + 1];
    bzero(ivPtr, sizeof(ivPtr));
    if (iv) {
        [iv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];
    }
    
    NSUInteger dataLength = [self length];
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);
    
    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(operation,
                                          kCCAlgorithmAES128,
                                          kCCOptionPKCS7Padding | kCCOptionECBMode,
                                          keyPtr,
                                          kCCBlockSizeAES128,
                                          ivPtr,
                                          [self bytes],
                                          dataLength,
                                          buffer,
                                          bufferSize,
                                          &numBytesEncrypted);
    if (cryptStatus == kCCSuccess) {
        return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
    }
    free(buffer);
    return nil;
}

@end

Calling this methods as
- (void)AES128Test {

    
    NSString *key = @"e8ffc7e56311679f12b6fc91aa77a5eb";
    
    NSData *cipherData;
    NSString *base64Text;
    NSString *plainText;
    
    //############## Request(crypt) ##############
    ////----    Plain text -> AES128
    plainText  = @"Hello, world!";
    cipherData = [[plainText dataUsingEncoding:NSUTF8StringEncoding] AES128EncryptedDataWithKey:key];
    
    NSLog(@"crypt AES128: %@", cipherData);
    
    ////----    AES128 -> base64
    // base 64 encryption
    base64Text = [cipherData base64EncodedStringWithOptions:0];
    
    NSLog(@"crypt AES128+base64: %@", base64Text);
    
    //############## Response(decrypt) ##############
    ////---- AES128 -> plain text
    plainText  = [[NSString alloc] initWithData:[cipherData AES128DecryptedDataWithKey:key]
                                       encoding:NSUTF8StringEncoding];
    
    NSLog(@"decrypt AES128: %@",plainText);
    
    ////----    base64 -> AES128 -> plain text
    // NSData from the Base64 encoded str
    cipherData = [[NSData alloc] initWithBase64EncodedString:base64Text
                                                     options:0];
    
    plainText  = [[NSString alloc] initWithData:[cipherData AES128DecryptedDataWithKey:key]
                                       encoding:NSUTF8StringEncoding];
    NSLog(@"decrypt AES128+base64: %@", plainText);
}


for more detail Codes,   Blog details.

No comments:

Post a Comment