2
NSString * theString=@"e88d";
NSData * data;

// something I should implement

NSLog(@"%@", theString);

NSLog(@"%@",[data description]);

I want the results of the two printings to be the same.


AES encryption and decryption:

(1).The server:

If the plaintext is @"abcd";

The AES encrypted data(NSData data type) is "d882830c dc892036 4345839f 13c7516a"

(2).in my local app, my code is:

NSData*data=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://..."]]; 
NSString * mystring= [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

However, to decrypt data successfully, I must have a data(NSData date type) which equals to "d882830c dc892036 4345839f 13c7516a". But it is the mystring(NSString data type) not the data(NSData data type) that equals the right value.

The encryption and decryption function both need a data(NSData data type) as input datas.

- (NSData*)AES128EncryptWithKey:(NSString*)key;
- (NSData*)AES128DecryptWithKey:(NSString*)key;
makes
  • 6,438
  • 3
  • 40
  • 58
user230965
  • 31
  • 1
  • 1
  • 5
  • You mean you want to convert the string `@"e88d"` into a data having 2 bytes `0xe8,0x8d`? – kennytm Mar 11 '10 at 09:01
  • I mean, if I try NSLog(@"%@",data); gdb will show: "e88d" – user230965 Mar 11 '10 at 09:05
  • What is it you're trying to achieve? I mean what are you going to use the NSData object for? Maybe we can provide clearer answers then. – conorgriffin Mar 11 '10 at 09:30
  • NSString * theString=@"abcd"; NSData * data; // code NSLog(@"%@", theString); NSLog(@"%@",[data description]); I want the results of the two printing are the same. – user230965 Mar 11 '10 at 09:47
  • 1
    @someone: That's impossible unless you want to break a lot of conventions. The description of NSData is always in the form `<12345678 abcdef01 99>` – kennytm Mar 11 '10 at 10:16
  • @someonemaybe When I asked what you're trying to achieve, I mean how are you using this in your code? What are you trying to use it for, not just what values are you trying to display. It looks like what you want to do is enter a hex value as a string like so "e88d" and then get a real hex value back from [data description]. However, as I understand it, [data description] returns a string value which describes, in hex, the value contained in `data`. Therefore, I don't think this is how you should be trying to achieve what you want. – conorgriffin Mar 11 '10 at 13:41

4 Answers4

3

I think this might answer your question

How do I convert a NSString value to NSData?

Community
  • 1
  • 1
conorgriffin
  • 4,282
  • 7
  • 51
  • 88
  • well,the printed out data is not the same as the string. – user230965 Mar 11 '10 at 09:16
  • probably because %@ you use in `NSLog(@"%@",data);` expects an NSString object, you're passing an NSData object. – conorgriffin Mar 11 '10 at 09:26
  • if I use NSLog(@"%@",[data description]), I hope gdb will show: e88d. how can I get the data? – user230965 Mar 11 '10 at 09:42
  • AES encryption and decryption: server: if the plaintext is :@"abcd"; the AES encrypted data(NSData data type) is :"d882830c dc892036 4345839f 13c7516a" in my local app, my code is : NSData*data=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://..."]]; NSString * mystring= [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; however, to decrypt the AES encrypted string, I must got a data(NSData date type) which equals to "d882830c dc892036 4345839f 13c7516a".But it is the mystring (NSString data type) not the data(NSData data type) that equals to the right value. – user230965 Mar 12 '10 at 08:03
1

the description you wanna set is not an instance specific value. It's the description of the class/object. NSData will have a description of like: 'this is a data object'. You can override this value thou by overriding the method.


- (NSString *)description {
    return @"e88d"; //normally used for class description
}

Ofcourse you will have to inherit the NSData object for that and then override the description like code above.

PS. I dont think you wanna use description for this just explaining what the use of it is in every class.


What you might want is:


NSString * theString=@"e88d";
NSData * data=[theString dataUsingEncoding:NSUTF8StringEncoding];

NSLog(@"%@", theString);
NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
0
@interface NSString (Joke)
- (NSString *)description;
@end
@implementation NSString (Joke)
- (NSString *)description
{
    return @"Panda!";
}
@end

@interface NSData (Joke)
- (NSString *)description;
@end
@implementation NSData (Joke)
- (NSString *)description
{
    return @"Panda!";
}
@end
rpetrich
  • 32,196
  • 6
  • 66
  • 89
  • 1
    Overly complicated, a simpler more generic solution: `#define NSLog(...) NSLog(@"Panda!")` – cobbal Mar 12 '10 at 09:40
0

How about this

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://..."]];
NSData *decryptedData = [data AES128DecryptWithKey:key];
NSString *mystring = [[NSString alloc] initWithData:decryptedData encoding:NSUTF8StringEncoding];
conorgriffin
  • 4,282
  • 7
  • 51
  • 88