views:

562

answers:

4

NSString * theString=@"e88d";

NSData * data;

// something I should implement

NSLog(@"%@", theString);

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

I want the results of the two printings are 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 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.

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

  • (NSData*)AES128EncryptWithKey:(NSString*)key;
  • (NSData*)AES128DecryptWithKey:(NSString*)key;
+2  A: 

I think this might answer your question

Similar Stack Overflow Question

Griffo
well,the printed out data is not the same as the string.
probably because %@ you use in `NSLog(@"%@",data);` expects an NSString object, you're passing an NSData object.
Griffo
if I use NSLog(@"%@",[data description]), I hope gdb will show: e88d.how can I get the data?
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.
+1  A: 

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]);
Luuk van Rens
A: 
@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
Overly complicated, a simpler more generic solution: `#define NSLog(...) NSLog(@"Panda!")`
cobbal
A: 

How about this

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