-3

hiiii freinds please any guys say me why str is NULL i will be creasy

NSString *strData = @"issam  bey"   ;

NSMutableData *objNSData = [NSMutableData dataWithData:[strData dataUsingEncoding:NSISOLatin1StringEncoding]];


objNSData = [objNSData EncryptAES:@"12345678901234561234567890123456"];
//NSString* decryptedStr = [[NSString alloc] initWithData:objNSData encoding:NSASCIIStringEncoding];

NSString *str=[[NSString alloc] initWithData: objNSData encoding:NSUTF8StringEncoding];

NSLog(@"%@",str);
Mehul Mistri
  • 15,037
  • 14
  • 70
  • 94
chikos
  • 85
  • 1
  • 1
  • 8
  • if i try with this code its not null NSString *str=[[NSString alloc] initWithData: objNSData encoding:NSISOLatin1StringEncoding]; – chikos Jul 01 '11 at 10:59

2 Answers2

2

If you are doing encryption and decryption then read Encrypting / Decrypting / Base64 Encode / Decode in iPhone Objective-C.

For AES256 encryption-decryption, read Adding methods to NSData and NSString using categories to provide AES256 encryption on iOS.

Dori
  • 915
  • 1
  • 12
  • 20
Mehul Mistri
  • 15,037
  • 14
  • 70
  • 94
1

NSString's initWithData:encoding: will try to use the specified data to create a string using the encoding you tell it to use. If it can't do that then it will return nil.

The fact that you tried a different encoding and got a value instead of nil should tell you what's wrong - your encoded data can't be represented by a UTF-8 string but can be represented by a Latin-1 string.

Just out of interest, why do you want to encrypt the data and then view it as a string again?

deanWombourne
  • 38,189
  • 13
  • 98
  • 110
  • because i want save the encrypt string in my xml file and after i want decrypte and converted in MSUtableData – chikos Jul 01 '11 at 11:10
  • then you should take a look at base64 encoding it - that will make sure that it can be represented as a string - look at this : http://stackoverflow.com/questions/392464/any-base64-library-on-iphone-sdk – deanWombourne Jul 01 '11 at 15:03
  • (base64 isn't encrypting - you will still need to encrypt it with AES and then base64 encode it :) – deanWombourne Jul 01 '11 at 15:04