0

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 ?

Tvd
  • 4,463
  • 18
  • 79
  • 125
  • I want to close/delete this question, as I have asked another question which is more clear and to the point. So, please try t answer here - http://stackoverflow.com/questions/23637597/ios-aes-encryption-fail-to-encrypt . I am not sure how do I close/delete my own Question - don't see any appropriate option t select to close this question. – Tvd May 13 '14 at 17:15

1 Answers1

0

Follow the instructions including the comments on the code you chose to use. The implementation creates a category on NSData, read-up on Objective-C categories is you are going to use them. You might consider using code that is not a category but a standalone class.

The [self length] and [self bytes] refer to the data because the code you chose implements the encryption/decryption as a category on NSData.

There is no need to convert the password to hex and then to data, just use

- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding

NSData *encryptedData = [NSData dataWithBytes:[data bytes] length:[data length]];  

Does not call the encryption method.
You never called the AES128Encrypt method.

zaph
  • 111,848
  • 21
  • 189
  • 228
  • In AES128Encrypt method, why am I not able to access [self length] & bytes methods. Those 2 lines gives error - "No visible interface for 'MC_AES' declares the selector 'length". How to get rid of this to use those methods without any parameters ? – Tvd May 12 '14 at 13:50
  • Follow the instructions including the comments on the code you chose to use or choose another implementation that you understand better. – zaph May 12 '14 at 13:57
  • yes I am more comfortable using standalone class. The next line i.e. NSData *decryptedData = [encryptedData AES128Decrypt]; calls AES128Encrypt method. I can't make out how to call that & am stuck over their. Thanks. – Tvd May 12 '14 at 14:54
  • @Zapah, can you please check UPDATE 1 in the question. I tried calling the method and its returning 0. Can you help me with that. Please. I need help. Thanks. – Tvd May 12 '14 at 16:44
  • Right now your problem is understanding how to use Categories, not a cryptographic question. You either need to learn/study/understand how to use them or find another method. Personally I avoid Categories as much as possible. – zaph May 12 '14 at 16:46
  • Ok. I am also not able to get it & hence am not using it. Will remove from this also, but need to implement this code in normal way. I believe the code of the method is written as it can be used in a normal way. And it is working , not throwing any error/warning - though not giving proper result right now. Is their anything wrong ? I am also not able to get any other reference from web which is so clear and precise and of my requirements. Had used other method from web, but failed in that too. You tell what do I do for this now ? Method is not giving proper results - gives 0. – Tvd May 12 '14 at 16:52