1

I am using Rijndael Encryption Algorithm when I am going to encrypt it, it encrypted in NSData. I want that encrypted NSdata into NSString. I tried to convert it into string but it return nil. Have anyone any solutions to get into string.

I am doing like this

NSString *passphrase = @"super-secret";
NSStringEncoding myEncoding = NSUTF8StringEncoding;

NSString *alphaPlain = @"This is a encryption test.";
NSData *alphaDataPlain = [alphaPlain dataUsingEncoding:myEncoding];
NSLog(@"  SimpleText value : %@",alphaPlain);

NSData *alphaDataCypher = [alphaDataPlain AESEncryptWithPassphrase:passphrase];
NSString *alphaStringCypher = [[NSString alloc] initWithData:alphaDataCypher encoding:myEncoding];

NSLog(@"  Encrypted value : %@",alphaStringCypher);

It returns nil value.

Thanks

iPhoneDev
  • 228
  • 1
  • 2
  • 9

1 Answers1

1

The encrypted data is no longer a UTF8 string, it's just some sequence of bytes, so decoding it as UTF8 fails.

What do you want to do with the string? If it's just for logging/debugging purposes, you could use [myData description] to get a hex string (with some extra whitespace for better readability). If you need this to transfer the data in a context where you need a textual representation, converting it to Base64 would be a good idea, see this answer for an easy way to do that.

Community
  • 1
  • 1
omz
  • 53,243
  • 5
  • 129
  • 141
  • Hi, thanks for your reply, encrypted string is required b'coz I have to pass on server.If any other sol. – iPhoneDev Jul 14 '11 at 12:17