0

I'm using AES Encryption and Decryption in my project which is an apple watch project. So for encryption and decryption I created one category for NSData, And I'm calling those methods in my sample view controller class. Its working fine.

And then Now I copied the same code in to my ServerManager class where I should do actual encryption. But it is showing me this error.

Crash with Error :

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteData AES256EncryptWithKey:]: unrecognized selector sent to instance 0x7ab084a0'

The same code is working in sample code and it is showing me this error in my actual project. I didn't understand what could be the reason. I never heard about NSConcreteData. also. Here is my code follows.

In My ServerManager Class :

    NSError *err;


   NSDictionary *dict = @{@"accountType":@"ALL",@"uId":@"c8ff46be-a083-40

09-8a33-fc2d22cc40e3|123456784",@"deviceId":@"qvxy1234"};

    NSData * jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&err];

    NSString *encryptedData = [[jsonData AES256EncryptWithKey:kABAESCryptionKey] base64EncodedStringWithOptions:0];

    NSLog(@"%@",encryptedData);

    NSData *nsdataFromBase64String = [[NSData alloc]
                                      initWithBase64EncodedString:encryptedData options:0];

and NSData+CustomCategory Class is like this :

- (NSData *)AES256EncryptWithKey:(NSString *)key {

    return [self AES256Operation:kCCEncrypt key:key];
}

- (NSData *)AES256DecryptWithKey:(NSString *)key {

    return  [self AES256Operation:kCCDecrypt key:key];
}

/*!
 * @brief Common method for encrypt and decrypt
 * @param  Operation : pass required crypto CCOperation type
 * @param  key : pass key based on which crption is done
 * @returns NSData object : Encrypted or Decrypted data
 */
- (NSData *)AES256Operation:(CCOperation)operation key:(NSString *)key {

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

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

    NSUInteger dataLength = [self length];

    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);

    size_t numBytesCrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(operation, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                          keyPtr, kCCKeySizeAES256,
                                          NULL /* initialization vector (optional) */,
                                          [self bytes], dataLength, /* input */
                                          buffer, bufferSize, /* output */
                                          &numBytesCrypted);
    if (cryptStatus == kCCSuccess) {

        return [NSData dataWithBytesNoCopy:buffer length:numBytesCrypted];
    }

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

The same code is working in sample but not in my real project...

Addition :

It is not because of base64EncodedStringWithOptions: method.

I tried like this also

NSData *encryptedData = [jsonData AES256EncryptWithKey:kABAESCryptionKey];

So this code is also crashing, So I'm sure the problem is not with base64EncodedStringWithOptions:? some thing is going wrong while I'm using in IWatch extension classes.

Here the kABAESCryptionKey is a proper key which is working fine. in Sample example.

2 Answers2

0

Yeah I got the answer. Its so dumb thing. This category is not added to my watch build target. So it is showing error. Now I added NSData+CustomCategory.m means i checked this file for my watch build target its working fine.

0

i think the problem is in AES256EncryptWithKey ..see link this may sure help

Community
  • 1
  • 1
vaibhav
  • 4,038
  • 1
  • 21
  • 51