Hello Friends Today We will learn how to Encrypt and Decrypt using AES 128 bit key in IOS.

Steps:

  1. Create a new Xcode Project Lets Say AESEncryption.
  2. Now create  a New File Of subclass NSData and name it as NSData+AES.
  3. Change the content of the above File with below Content. Or else you can download this project and directly drag and drop these Files into your Project.

NSData+AES.h

#import <Foundation/Foundation.h>

@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+AES.m

#import “NSData+AES.h”

#import <CommonCrypto/CommonCryptor.h>

@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

Now Go to your View Controller.m. Here we have encrypted and decrypted in two Types.

One from Normal string to Encrypted text and one from base 64 String to encrypted text and  decrypted Text.

So we will write a method for this. just call it from ViewDidLoad.

#import “ViewController.h”

#import “NSData+AES.h”

@interface ViewController (){

    NSString *key;

    NSString *normalText;

}

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    key = @”ioStpoint.wordpress.com”;

    normalText = @”LetsLearnHowToEncryptAndDecrypt”;

    

     [self testAES128];

    // Do any additional setup after loading the view, typically from a nib.

}

– (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

– (void)testAES128 {

    

    NSData *cipherData;

    NSString *base64Text;

    

    // Normal Text To AES128 Conversion

    cipherData = [[normalText dataUsingEncoding:NSUTF8StringEncoding] AES128EncryptedDataWithKey:key];

    

    NSLog(@”Encryptes AES128  %@”, cipherData);

    

    //  AES128 -> base64

    // base 64 encryption

    base64Text = [cipherData base64EncodedStringWithOptions:0];

    

    NSLog(@”Encryptes AES128 and base64  %@”, base64Text);

    

    

    //  AES128 To  Normal text

    normalText  = [[NSString alloc] initWithData:[cipherData AES128DecryptedDataWithKey:key]

                                       encoding:NSUTF8StringEncoding];

    

    NSLog(@”Decryption AES128  %@”,normalText);

    

    //   base64 To  AES128 To  Normal text

    // NSData from the Base64 encoded str

    cipherData = [[NSData alloc] initWithBase64EncodedString:base64Text

                                                     options:0];

    

    normalText  = [[NSString alloc] initWithData:[cipherData AES128DecryptedDataWithKey:key]

                                       encoding:NSUTF8StringEncoding];

    NSLog(@”Decryption AES128 and base64  %@”, normalText);

}

You can download the whole Project from here.

Do share Your reviews .

Enjoy Coding 🙂

 

 

 

Advertisements