iPhone上的NSString的AES加密 [英] AES Encryption for an NSString on the iPhone

查看:89
本文介绍了iPhone上的NSString的AES加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人可以指出我正确的方向,以加密一个字符串,返回另一个字符串与加密的数据? (我一直在尝试使用AES256加密)我想编写一个方法,它需要两个NSString实例,一个是要加密的消息,另一个是加密它的密码 - 我怀疑我必须生成具有密码的加密密钥,如果密码被提供加密数据,则可以被反转。该方法应该返回从加密数据创建的NSString。



我已经尝试了在这篇文章的第一条评论,但到目前为止我没有运气。苹果的 CryptoExercise 肯定有一些东西,但我不明白。 ..我已经看到很多参考 CCCrypt ,但是在我使用它的每种情况下都失败了。



我也必须能够解密一个加密的字符串,但我希望这就像kCCEncrypt / kCCDecrypt一样简单。

解决方案

由于您没有发布任何代码,很难准确知道哪些问题遇到了但是,您链接到的博文似乎工作正常,除了每次调用 CCCrypt()中引起编译错误的额外逗号之外。



稍后对该信息的评论包括此修改后的代码,其中为我工作,似乎更直截了当。如果您为NSData类别包含代码,可以这样写:(注意: printf()调用仅用于演示各种数据的状态 - 在一个实际的应用程序中,打印这些值是没有意义的。)

  int main(int argc,const char * argv []){
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSString * key = @我的密码;
NSString * secret = @要加密的文本;

NSData * plain = [secret dataUsingEncoding:NSUTF8StringEncoding];
NSData * cipher = [plain AES256EncryptWithKey:key];
printf(%s\\\
,[[cipher description] UTF8String]);

plain = [cipher AES256DecryptWithKey:key];
printf(%s\\\
,[[简单描述] UTF8String]);
printf(%s\\\
,[[NSString alloc] initWithData:plain encoding:NSUTF8StringEncoding] UTF8String]);

[pool drain];
return 0;
}

给定这个代码,加密数据不会总是很好地翻译成一个NSString,它可能会更方便地编写两个方法,包装所需的功能,在正向和反向...

  - (NSData *)encryptString:(NSString *)plaintext withKey:(NSString *)key {
return [[plaintext dataUsingEncoding:NSUTF8StringEncoding] AES256EncryptWithKey:key] (NSString *)key {
}

- (NSString *)decryptData:(NSData *)ciphertext withKey:(NSString *)key {
return [[NSString alloc] initWithData:[ciphertext AES256DecryptWithKey: key]
encoding:NSUTF8StringEncoding] autorelease];
}

这绝对适用于雪豹, @Boz 报告说CommonCrypto是iPhone上Core OS的一部分。 10.4和10.5都有 / usr / include / CommonCrypto ,尽管10.5具有 CCCryptor.3cc 和10.4的手册页没有,所以YMMV。






编辑:请参阅这个后续问题关于使用Base64编码将加密数据字节表示为字符串(如果需要)使用安全无损转换。


Can anybody point me in the right direction to be able to encrypt a string, returning another string with the encrypted data? (I've been trying with AES256 encryption.) I want to write a method which takes two NSString instances, one being the message to encrypt and the other being a 'passcode' to encrypt it with - I suspect I'd have to generate the encryption key with the passcode, in a way that can be reversed if the passcode is supplied with the encrypted data. The method should then return an NSString created from the encrypted data.

I've tried the technique detailed in the first comment on this post, but I've had no luck so far. Apple's CryptoExercise certainly has something, but I can't make sense of it... I've seen lots of references to CCCrypt, but it's failed in every case I've used it.

I would also have to be able to decrypt an encrypted string, but I hope that's as simple as kCCEncrypt/kCCDecrypt.

解决方案

Since you haven't posted any code, it's difficult to know exactly which problems you're encountering. However, the blog post you link to does seem to work pretty decently... aside from the extra comma in each call to CCCrypt() which caused compile errors.

A later comment on that post includes this adapted code, which works for me, and seems a bit more straightforward. If you include their code for the NSData category, you can write something like this: (Note: The printf() calls are only for demonstrating the state of the data at various points — in a real application, it wouldn't make sense to print such values.)

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSString *key = @"my password";
    NSString *secret = @"text to encrypt";

    NSData *plain = [secret dataUsingEncoding:NSUTF8StringEncoding];
    NSData *cipher = [plain AES256EncryptWithKey:key];
    printf("%s\n", [[cipher description] UTF8String]);

    plain = [cipher AES256DecryptWithKey:key];
    printf("%s\n", [[plain description] UTF8String]);
    printf("%s\n", [[[NSString alloc] initWithData:plain encoding:NSUTF8StringEncoding] UTF8String]);

    [pool drain];
    return 0;
}

Given this code, and the fact that encrypted data will not always translate nicely into an NSString, it may be more convenient to write two methods that wrap the functionality you need, in forward and reverse...

- (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] autorelease];
}

This definitely works on Snow Leopard, and @Boz reports that CommonCrypto is part of the Core OS on the iPhone. Both 10.4 and 10.5 have /usr/include/CommonCrypto, although 10.5 has a man page for CCCryptor.3cc and 10.4 doesn't, so YMMV.


EDIT: See this follow-up question on using Base64 encoding for representing encrypted data bytes as a string (if desired) using safe, lossless conversions.

这篇关于iPhone上的NSString的AES加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆