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

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

问题描述

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

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.

我已经尝试了 对这篇文章的第一条评论,但到目前为止我没有运气.Apple 的 CryptoExercise 当然有一些东西,但我无法理解... 我看过很多对 CCCrypt,但在我使用过的每种情况下都失败了.

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.

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

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

推荐答案

由于您还没有发布任何代码,因此很难确切知道您遇到了哪些问题.但是,您链接到的博客文章似乎工作得相当不错……除了每次调用 CCCrypt() 时都会出现额外的逗号,这会导致编译错误.

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.

稍后对该帖子的评论包括此改编代码,它对我有用,而且看起来更直接.如果您将他们的代码包含在 NSData 类别中,您可以编写如下内容:(注意:printf() 调用仅用于演示各个点的数据状态 — 在实际应用程序中,打印这些值是没有意义的.)

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
", [[cipher description] UTF8String]);

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

    [pool drain];
    return 0;
}

鉴于此代码,以及加密数据并不总是很好地转换为 NSString 的事实,编写两种包装您需要的功能的方法可能更方便,正向和反向......

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

这绝对适用于 Snow Leopard,并且 @Boz 报告说 CommonCrypto 是 iPhone 核心操作系统的一部分.10.4 和 10.5 都有 /usr/include/CommonCrypto,虽然 10.5 有 CCCryptor.3cc 的手册页,而 10.4 没有,所以 YMMV.

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.

请参阅关于使用 Base64 编码表示加密数据字节的此后续问题作为使用安全无损转换的字符串(如果需要).

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天全站免登陆