views:

174

answers:

1

I have this encryption/decryption implemented and executes with no error, but i am unable to decrypt the data and im not sure whether the data is encrypted, kindly help.

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

char keyPtr[kCCKeySizeAES256+1]; 
bzero(keyPtr, sizeof(keyPtr));


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


NSUInteger dataLength = [strData length];
data = [[NSData alloc] init];
data = [strData dataUsingEncoding:NSUTF8StringEncoding];


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

size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                      keyPtr, kCCKeySizeAES256,
                                      NULL /* initialization vector (optional) */,
                                      [data bytes], dataLength, /* input */
                                      [data bytes], bufferSize, /* output */
                                      &numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
    return [NSData dataWithBytesNoCopy:data length:numBytesEncrypted];
}}

The decryption is :

-(NSData *)AES256DecryptWithKey:(NSString *)key andForData:(NSData *)objDataObject {
char keyPtr[kCCKeySizeAES256+1];
bzero(keyPtr, sizeof(keyPtr)); 
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

NSUInteger dataLength = [objDataObject length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);

size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                      keyPtr, kCCKeySizeAES256,
                                      NULL,
                                      [objDataObject bytes], dataLength,
                                      [objDataObject bytes], bufferSize,
                                      &numBytesDecrypted);

if (cryptStatus == kCCSuccess) {
    return [NSData dataWithBytesNoCopy:objDataObject length:numBytesDecrypted];
}

}

i call the above methods like this:

CryptoClass *obj = [CryptoClass new];
NSData *objData = [obj AES256EncryptWithKey:@"hell"];

NSData *val = [obj AES256DecryptWithKey:@"hell" andForData:objData ];

NSLog(@"decrypted string is : %@ AND LENGTH IS %d",[val description],[val length]);

Decryption doesnt seem to happen at all and the encryption - i am not sure about it, kindly help me here.

+1  A: 

I have learnt it by myself,

a complete implementation which can bye-pass ccats is:

http://pastie.org/966473

Thanks to all who helped.

Futur