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

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

问题描述

我的应用程序使用另一个NSString(关键字)使用 aes 256位加密。当我运行我的项目并运行加密方法,没有任何加密文本域只是清除自身。以下是我的代码:

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

可以通过点击此链接下载头文件:包含AES实施的ZIP文件

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.

请参阅使用CommonCrypto使用AES进行正确加密,以解释上述实现的问题,以及如何在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编码用于需要将二进制数据转换为可以通过电子邮件或其他控制字符出现问题的其他地方轻松发送的表单。它以7位ASCII数据转换8位二进制数据。这不是必需的或有用的。

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