問題描述
我的應用使用 aes 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 加密的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!