1

In my project, I am encrypting some data (string) using the 3DES encryption algorithm. The string I pass to the encryption function returns me a NSData object.

Now I want to convert NSData to NSString, so that I can send that string to server.

So I used this code to convert NSData to NSString

NSString *stringCreated = [[NSString alloc] initWithData:encryptedData encoding:NSASCIIStringEncoding];

But when I print this string, It prints very few characters on the console. I think there are few characters which is making a sequence of "\0" due to which it prints the string upto that character only.

I tried to encode the data with NSUTF8StringEncoding but it returns me (null).

I want to send the complete string to the server, what to do now?

Rajan Balana
  • 3,775
  • 25
  • 42

1 Answers1

3

This will not work since encrypted string is binary data. Convert it to Base64, if you need to use textual representation.

Here is described convertion of NSData to NSString : Converting NSData to base64

Community
  • 1
  • 1
Nickolay Olshevsky
  • 13,706
  • 1
  • 34
  • 48
  • Is there any other approach I can follow in order to send the encrypted data to SOAP Webservice, other than the Base64 String? – Rajan Balana Dec 04 '12 at 14:13
  • Sending the hexadecimal representation is another alternative. Remember that you'll need to convert back on the server side, so don't try to get too creative. – Kitsune Dec 04 '12 at 16:44
  • For short strings hex representation would be better and easier-in-implementation solution, I agree. – Nickolay Olshevsky Dec 05 '12 at 15:15