I need to encrypt/decrypt data using AES-128-CBC on my project. I got reference from here which I have implemented in my project. But from the available code, I couldn't get thru [self length]
and [self bytes]
lines, hence I added a parameter to the function and implemented the same. Those lines are using NSData values, but I get 'No visible ... '. I am stuck in step 3 i.e. adding code in class where I want to call the Encrypt method. MY code :
HEADER
@interface MC_AES : NSObject
- (NSData*)AES128Decrypt : (NSData*) inputData;
- (NSData*)AES128Encrypt : (NSData*) inputData;
@end
IMPLEMENTATION
#include <CommonCrypto/CommonCryptor.h>
#import "MC_AES.h"
NSString *iv = @"DFR55d+.njT]W-WW";
NSString *key = @"HGYJ4RXc{Kd@5q4+";
@implementation MC_AES
-(NSData*)AES128Encrypt : (NSData*) inputData
{
char ivPtr[kCCKeySizeAES128 + 1];
bzero(ivPtr, sizeof(ivPtr));
// fetch iv data
[iv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];
// 'key' should be 32 bytes for AES256, will be null-padded otherwise
char keyPtr[kCCKeySizeAES128 + 1]; // room for terminator (unused)
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
// fetch key data
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [inputData 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, 0,
keyPtr, kCCKeySizeAES128,
ivPtr/* initialization vector (optional) */,
[inputData 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;
}
CODE in class where I want to call Encrypt method
// I need to encrypt password, so will have to convert password to hex value and pass here to Encrypt
- (void) testActuallyEncrypting :(NSString*) hexString {
NSLog(@"Encrypted HexString : %@", hexString); // password in hex value
// Convert hex to NSData object
NSData *data = [self dataFromHexString:hexString];
// Prepare the NSDAta obj to store the encrypted pswd
NSData *encryptedData = [NSData dataWithBytes:[data bytes] length:[data length]];
// ***** How can I access this here ??
//NSData *decryptedData = [encryptedData AES128Decrypt];
//NSString *decryptedString = [NSString stringWithUTF8String:[decryptedData bytes]];
//NSLog(@"Decrypted String : %@", decryptedString);
.......
........
}
My Query - How can I access AES128Decrypt method - (1) I got to pass it a parameter of (data) I believe i.e. the data that I need to encrypt. (2) Without creating an object of the class or its not even static how can I call this method ?
Importantly, I need to clear confirm with the signature of AES128Encrypt/AES128Decrypt methods, and the way for calling it. I am not able to understand how to handle this and make it working.
If you can help in getting thru [self length] and [self bytes] lines, then also I am fine. I just want to make it working. You may please refer the link for more code. Also I believe this code is proper for implementing AES-128-CBC encryption. Any help is highly appreciated. Thanks.
UPDATE 1 :
As I am with using standalone class MC_AES, I tried to call AES128Encrypt as follows :
- (void) testActuallyEncrypting :(NSString*) str {
NSLog(@"String to Encrypt : %@", str);
//Convert NSString to NSData
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding]; //[self dataFromHexString:hexString];
NSData *encryptedData = [NSData dataWithBytes:[data bytes] length:[data length]];
MC_AES *aes = [[MC_AES alloc]init];
encryptedData = [aes AES128Encrypt:data];
NSString * encryptedStr = [NSString stringWithUTF8String:[encryptedData bytes]];
NSLog(@"Encrypted data - %@", encryptedStr);
The encryptedData that is get is 0. In AES128Encrypt
method, the cryptStatus == kCCsuccess
, inputData
and buffer
is of size 6 & numBytesEncrypted
is 0. Why I don't receive proper encrypted value ? Where am I going wrong ?