我在 iphone 上使用了之前提到的关于 AES 加密的提示。
NSString *mystr= [[self encryptString:[message valueForKey:@"message"] withKey:@"password"] hexadecimalString];
NSData *mydata= [self encryptString:[message valueForKey:@"message"] withKey:@"password"];
NSLog (@"Immediate decrypt data: %@",[self decryptData:mydata withKey:@"password"]);
NSLog (@"Immediate decrypt string: %@",[self decryptData:[mystr dataUsingEncoding:NSUTF8StringEncoding] withKey:@"password"]);
第一个 NSLog 正确解码字符串,第二个返回 null。此类中的方法:
+ (NSData*) encryptString:(NSString*)plaintext withKey:(NSString*)key {
return [[plaintext dataUsingEncoding:NSUTF8StringEncoding] AES256EncryptWithKey:key];
}
+ (NSString*) decryptData:(NSData*)ciphertext withKey:(NSString*)key {
return [[NSString alloc] initWithData:[ciphertext AES256DecryptWithKey:key]
encoding:NSUTF8StringEncoding] ;
}
和 NSData 的标头(加密)
- (NSData *)AES256EncryptWithKey:(NSString *)key;
- (NSData *)AES256DecryptWithKey:(NSString *)key;