Why does my initWithData return nil indicating an error after converting NSData to NSString returning from encrypting via CommonCrypto?

StackOverflow https://stackoverflow.com/questions/8815570

Question

Here is my code:

-(IBAction)encryptText:(id)sender
{
key = self.tvKey.text;

CCCryptorStatus status = kCCSuccess;                                        
algorithm = kCCAlgorithmAES128;                                                
CCOptions opts = kCCOptionPKCS7Padding;

NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];

NSString *plainString = [NSString stringWithFormat:@"%@", self.tvEntryText.text];
NSData *plainData = [plainString dataUsingEncoding:NSUTF8StringEncoding];

NSData *encryptedData = [plainData dataEncryptedUsingAlgorithm: algorithm     
                                                           key: keyData       
                                          initializationVector: nil             
                                                       options: opts           
                                                         error: &status];    


if ( status != kCCSuccess ) {
    NSError * err = [NSError errorWithCCCryptorStatus: status];
    self.lblKeyMsg.text = [NSString stringWithFormat:@"Encryption failed: %s", [[err localizedDescription] UTF8String]];
    self.lblKeyMsg.textColor = [UIColor redColor];
    return;
}

NSString *encryptedString = [[[NSString alloc] initWithData:encryptedData encoding:NSUTF8StringEncoding] autorelease];
self.tvResultText.text = encryptedString;

}

The second to last statement I am converting the encrypted NSData to an NSString for display purposes, but it comes back nil. Nil is a valid response indicating and error. I have stopped the execution right after that statement has run and here are the vaues:

(gdb) po keyData
<61616161 61616161 61617373 73737373 73737373 64646464 64646464 64646666>
(gdb) po key
aaaaaaaaaassssssssssddddddddddff
(gdb) po plainString
this is a test.
(gdb) po plainData
<74686973 20697320 61207465 73742e>
(gdb) po encryptedData
<f7074146 b295e340 0d947d53 9ea629cf>
(gdb) po encryptedString
Can't print the description of a NIL object.
(gdb) po status
Can't print the description of a NIL object.
(gdb) 

The status is nil indicating that there was NO error in the encryption. All values appear to be as expected, but the initWithData fails. Here is the operations iPhone view:

The app screen when Encrypt button is pressed

Why is the initWithData failing here ?

Was it helpful?

Solution

As the documentation says, initWithData:encoding: may return nil if the data passed is not valid data for the encoding. Unlike some encodings, you can't just take arbitrary data and pretend it's UTF8 data - it has to conform to the UTF8 specification.

Your encryptedData NSData object will contain seemingly random data - highly unlikely to be valid utf8 data and so initWithData:encoding: is very likely to fail.

If you're just looking for something that you can stick in a text box, you might want to try base64 encoding the encrypted data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top