2

how convert NSMutableData to NSString?

NSString *strData = @"Bonjour tout le monde, je voudrais vous présenter la Société Futur";
NSLog(@"before encryption : %@",strData);
NSMutableData *objNSData = [NSMutableData dataWithData:[strData dataUsingEncoding:NSUTF16StringEncoding]];

objNSData = [objNSData EncryptAES:@"samplekey"];
   ///NSLog(@"encryption : %@", objNSData);
   NSString * strData1= [[NSString alloc] initWithData:objNSData encoding:NSUTF16StringEncoding];

NSLog(@"encryption : %@", strData1);
oliholz
  • 7,447
  • 2
  • 43
  • 82
djboss
  • 59
  • 1
  • 6

3 Answers3

2

Try with below

NSString * strData1= [[NSString alloc] initWithData:myMutableData encoding:NSUTF8StringEncoding];
Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
  • can i convert NSString to NSMutableData – djboss Jun 30 '11 at 09:04
  • when i use NSString * strData1= [[NSString alloc] initWithData:myMutableData encoding:NSUTF8StringEncoding]; i have encryption : (null) – djboss Jun 30 '11 at 09:12
  • NSString * strData1= [[NSString alloc] initWithData:objNSData encoding:NSUTF8StringEncoding]; NSLog(@"encryption : %@", strData1); i have encryption : (null) – djboss Jun 30 '11 at 09:17
0

Please refer this Link

For different encoding , there is other methods also..

Converting NSString to NSData

NSString* str= @"teststring";
NSData* data=[str dataUsingEncoding:NSUTF8StringEncoding];

Update

For Encryption and decryption, See This Link and download source code...

Community
  • 1
  • 1
Mehul Mistri
  • 15,037
  • 14
  • 70
  • 94
  • i use this code but when i want saved the encrypt data and i want decrypted i have many exception .can i convert NSString to NSMutabedata? – djboss Jun 30 '11 at 08:59
  • the result for vonversion is nullNSString *key = @"my password"; NSString *secret = @"text to encrypt"; NSData *plain = [secret dataUsingEncoding:NSUTF8StringEncoding]; NSData *cipher = [plain AES256EncryptWithKey:key]; //NSString * strData1= [[NSString alloc] initWithData:cipher encoding:NSASCIIStringEncoding]; NSString * strData1= [[NSString alloc] initWithData:cipher encoding:NSUTF8StringEncoding]; – djboss Jun 30 '11 at 09:55
0

Following links may help you -

  1. http://linglong117.blog.163.com/blog/static/27714547201011142423968/

  2. AES Encryption for an NSString on the iPhone

  3. https://gist.github.com/838614

These can help you to go through with the encryption methods and their correct implementation using category.

Community
  • 1
  • 1
Kapil Mandlik
  • 174
  • 1
  • 1