Tuesday, November 9, 2010

AES Encryption in iPhone

Hi ,

Today I would like to share, how to do AES Encryption in iPhone.

*********************************************************************************
AESCryptor.h file

*********************************************************************************
//
// AESCryptor.h
// iPhoneCrypto
//
// Created by Syam Sasidharan on 9/9/10.
//

#import
#import

@interface AESCryptor : NSObject {

CCOptions cryptorOptions;
size_t cryptorKeySize;
}

@property (nonatomic) CCOptions cryptorOptions;
@property (nonatomic) size_t cryptorKeySize;

- (void)initWithCryptorOptions:(CCOptions)options
cryptorKeySize:(size_t)keySize;

- (NSData *)encrypt:(NSData *)objDataToBeEncrypted
withKey:(NSString *)keyData;

- (NSData *)decrypt:(NSData *)objDataToBeDecrypted
withKey:(NSString *)keyData;

@end

*********************************************************************************
AESCryptor.m file

*********************************************************************************
//
// AESCryptor.m
// iPhoneCrypto
//
// Created by Syam Sasidharan on 9/9/10.
//

#import "AESCryptor.h"

@implementation AESCryptor

@synthesize cryptorOptions;
@synthesize cryptorKeySize;

#pragma mark -
#pragma mark Custom Initializer

- (id) init
{
self = [super init];
if (self != nil) {

cryptorOptions=kCCOptionPKCS7Padding;
cryptorKeySize=kCCKeySizeAES256;
}
return self;
}
#pragma mark -
#pragma mark Custom Initializer
- (void)initWithCryptorOptions:(CCOptions)options
cryptorKeySize:(size_t)keySize {
if(self=[super init]){

cryptorOptions=options;
cryptorKeySize=keySize;
}
}

#pragma mark -
#pragma mark Encrypt

//Custom Method
//This method will encrypt data using key
//It has two params the data to be encrypted
//and a key
- (NSData *)encrypt:(NSData *)dataToBeEncrypted
withKey:(NSString *)keyData {

// 'key' should be 32 bytes for AES256, will be null-padded otherwise
char keyPtr[cryptorKeySize+1]; // room for terminator (unused)
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)

// fetch key data
[keyData getCString:keyPtr
maxLength:sizeof(keyPtr)
encoding:NSUTF8StringEncoding];

NSUInteger dataLength = [dataToBeEncrypted length];

//See the doc: For block ciphers, the output size will always be less than or
//equal to the input size plus the size of one block.
//That's why we need to add the size of one block here
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);

size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, cryptorOptions,
keyPtr, cryptorKeySize,
NULL /* initialization vector (optional) */,
[dataToBeEncrypted bytes], dataLength, /* input */
buffer, bufferSize, /* output */
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
//the returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytesNoCopy:buffer
length:numBytesEncrypted];
}

free(buffer); //free the buffer;
return nil;

}

#pragma mark -
#pragma mark Decrypt
//Custom Method
//This method will decrypt encrypted data
//It has two params the data to be encrypted
//and a key
- (NSData *)decrypt:(NSData *)dataToBeDecrypted
withKey:(NSString *)keyData {

// 'key' should be 32 bytes for AES256, will be null-padded otherwise
char keyPtr[cryptorKeySize+1]; // room for terminator (unused)
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)

// fetch key data
[keyData getCString:keyPtr
maxLength:sizeof(keyPtr)
encoding:NSUTF8StringEncoding];

NSUInteger dataLength = [dataToBeDecrypted length];

//See the doc: For block ciphers, the output size will always be less than or
//equal to the input size plus the size of one block.
//That's why we need to add the size of one block here
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);

size_t numberOfBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, /* operation */
kCCAlgorithmAES128, /* algorithm */
cryptorOptions, /* options */
keyPtr, /* key pointer */
cryptorKeySize, /* cryptor key size */
NULL /* initialization vector (optional) */,
[dataToBeDecrypted bytes],
dataLength, /* input */
buffer, bufferSize, /* output */
&numberOfBytesDecrypted);

if (cryptStatus == kCCSuccess) {
//the returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytesNoCopy:buffer
length:numberOfBytesDecrypted];
}

free(buffer); //free the buffer;
return nil;

}

@end

In order to compile these classes you need to add the Security.framework to the project.

References : http://iphonedevelopment.blogspot.com/2009/02/strong-encryption-for-cocoa-cocoa-touch.html
http://greghaygood.com/2009/01/17/symmetric-encryption-with-the-iphone-sdk-and-securityframework

Happy Coding!!! :)

Tuesday, September 7, 2010

Welcome to all......

Hi...

My hearty welcome to all...Syamoo"s dev blog is a platform for iPhone application developers to share their ideas and thoughts regarding iphone development. So here you can expect a bunch of articles useful for iphone application development.

Regards,
Syam