iOS 中的 AES256 NSString 加密 [英] AES256 NSString Encryption in iOS

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

问题描述

我的应用使用 256 位加密.当我运行我的项目并运行 encrypt 方法时,没有任何内容被加密,文本字段只会自行清除.这是我的代码:

My app encrypts and decrypts (or it should) an NSString (the text to be encrypted / decrypted) with another NSString (the keyword) using aes 256-Bit Encryption. When I run my project and run the encrypt method, nothing gets encrypted the textfield just clears itself. Here is the code I have:

-(void)EncryptText {
    //Declare Keyword and Text
    NSString *plainText = DataBox.text;
    NSString *keyword = Keyword.text;

    //Convert NSString to NSData
    NSData *plainData = [plainText dataUsingEncoding:NSUTF8StringEncoding];

    //Encrypt the Data
    NSData *encryptedData = [plainData AESEncryptWithPassphrase:keyword];

    //Convert the NSData back to NSString
    NSString* cypherText = [[NSString alloc] initWithData:encryptedData encoding:NSUTF8StringEncoding];

    //Place the encrypted sting inside the Data Box
    NSLog(@"Cipher Text: %@", cypherText);
}

可以通过点击这个链接下载头文件:ZIP File contains AES implementation

The header files can be downloaded by clicking this link: ZIP File containing AES Implementation

有人告诉我,我需要使用我的字符串的 Base-64 编码来获得任何结果.如果这是真的,那我该怎么做呢?

I have been told that I need to use Base-64 encoding of my string to get any result. If this is true, then how do I do it?

我还被告知 iOS 5 中的加密发生了变化,我的应用程序是 iOS 5+ 专用应用程序.如果这是真的,那么我必须做些什么才能使这种加密在 iOS 5 上工作,或者我在哪里可以找到另一个适用于 NSString 的 AES 256 位实现.

I have also been told that encryption changed in iOS 5, and my app is an iOS 5+ ONLY app. If this is true, then what do I have to do to make this encryption work on iOS 5 or where can I find another AES 256-bit implementation that will work on NSString.

为什么这段代码没有产生结果?

Why doesn't this code produce a result?

推荐答案

下面的链接指的是较旧的实现.最新版本称为 RNCryptor.

您的代码未使用 iOS 的内置 AES 实现.它有自己的自定义实现.AESEncryptWithPassphrase: 也错误地生成了密钥,丢弃了密码中的大部分熵.

Your code doesn't use iOS's built-in AES implementation. It has its own custom implementation. AESEncryptWithPassphrase: also incorrectly generates the key, throwing away most of the entropy in the passphrase.

在 iOS 上,您应该为 AES 使用 CCCrypt*() 函数.您还应该确保您了解加密和解密例程中发生的情况.编写看起来正确的加密代码非常容易(因为您无法通过检查读取输出),但非常不安全.

On iOS, you should be using the CCCrypt*() functions for AES. You should also make sure that you understand what is happening in your encryption and decryption routines. It is very easy to write encryption code that looks correct (in that you cannot read the output by inspection), but is extremely insecure.

请参阅 Properly encrypting with AES with CommonCrypto 以了解上述问题的解释实施,以及如何在 iOS 上正确使用 AES.请注意,iOS 5 现在有 CCKeyDerivationPBKDF 可用.

See Properly encrypting with AES with CommonCrypto for an explanation of the problems with the above implementation, and how to properly use AES on iOS. Note that iOS 5 now has CCKeyDerivationPBKDF available.

在加密之前不需要对字符串进行 Base-64 编码.Base-64 编码用于需要将二进制数据转换为可以通过电子邮件或其他控制字符存在问题的地方轻松发送的形式的情况.它将 8 位二进制数据转换为 7 位 ASCII 数据.这在这里没有必要或有用.

There is no requirement to Base-64 encode your string prior to encryption. Base-64 encoding is used in cases where you need to convert binary data into a form that can be easily sent over email or other places where control characters would be a problem. It converts 8-bit binary data in 7-bit ASCII data. That's not necessary or useful here.

编辑:仔细阅读有关如何使用此代码的说明至关重要.简单地剪切和粘贴安全代码并希望它有效是危险的.也就是说,RNCryptManager 的完整源代码可作为 iOS 5 编程突破极限 可能会有所帮助.这本书(尽管网站上说了什么,应该会在下周出版)包含关于如何使用此代码的更长时间的讨论,包括如何提高性能和处理非常大的数据集.

EDIT: It is critical that you carefully read the explanation of how to use this code. It is dangerous to simply cut and paste security code and hope it works. That said, the full source to RNCryptManager is available as part of the Chapter 11 example code for iOS 5 Programming Pushing the Limits and may be helpful . The book (which should be available next week despite what the site says) includes a much longer discussion of how to use this code, including how to improve performance and deal with very large datasets.

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

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