iphone encryption not working

Posted by Futur on Stack Overflow See other posts from Stack Overflow or by Futur
Published on 2010-05-15T14:00:00Z Indexed on 2010/05/15 14:04 UTC
Read the original article Hit count: 440

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about iphone-sdk