0

I get a NSData object in return after decrypting a payload with aes128:

NSData *returnData = [ciphertext AES128DecryptWithKey:keyData withIV:ivData];

I get the following hex output when i try to NSLog this:

<2db88b73 d84599a1 5779c736 09c975b7 92750cf2 d11cb41b 19f13781 4401bc57 b2ad96c8 402e3ccf 851c0219 00aec76b>

I then try to setting it as NSString:

[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

When using NSLog() on the string i get "(null)" as output.

Can someone tell me why and where i should look for the problem?

Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
Madoc
  • 1,605
  • 3
  • 17
  • 30
  • Are you sure that you are using the correct encoding? – Abizern Jun 07 '13 at 13:40
  • What is the cleartext supposed to look like? It appears to me that that NSData is not legit UTF8. Probably not legit characters in any character set. (One would suspect that the decrypt failed.) – Hot Licks Jun 07 '13 at 16:07

1 Answers1

1

Collided with the same issue some time ago, found the answer here.

If the data is not null-terminated, you should use -initWithData:encoding:

NSString* newStr = [[[NSString alloc] initWithData:theData
                                     encoding:NSUTF8StringEncoding] autorelease];

If the data is null-terminated, you should instead use -stringWithUTF8String: to avoid the extra \0 at the end.

NSString* newStr = [NSString stringWithUTF8String:[theData bytes]];

(If you have ARC enabled, remove the -autorelease call.)

Community
  • 1
  • 1
makaron
  • 1,585
  • 2
  • 16
  • 30