我正在尋找一些關於AES加密的可可代碼,我做了一些谷歌搜索。我發現這個非常有用的鏈接 - http://iphonedevelopment.blogspot.com/2009/02/strong-encryption-for-cocoa-cocoa-touch.html。所以我試了一下,但對我沒有用。用於AES加密解密的任何可可源代碼?
任何人都可以建議我一些有用的鏈接或源代碼,可以幫助我在我的示例應用程序中實現它。
我正在尋找一些關於AES加密的可可代碼,我做了一些谷歌搜索。我發現這個非常有用的鏈接 - http://iphonedevelopment.blogspot.com/2009/02/strong-encryption-for-cocoa-cocoa-touch.html。所以我試了一下,但對我沒有用。用於AES加密解密的任何可可源代碼?
任何人都可以建議我一些有用的鏈接或源代碼,可以幫助我在我的示例應用程序中實現它。
AES128加密在CommonCrypto框架中的iPhone上可用。相關函數位於CommonCryptor.h頭文件中。
您可以創建一個像這麼cryptor:
// Assume key and keylength exist
CCCryptorRef cryptor;
if(kCCSuccess != CCCryptorCreate(kCCEncrypt, kCCAlgorithmAES128, 0, key, keyLength, NULL, &cryptor))
; //handle error
// Repeatedly call CCCryptorUpdate to encrypt the data
CCCryptorRelease(cryptor);
它的問題和你正在尋找AES的示例實現的鏈接似乎。我不會推薦這個 - 使用Apple的實現!
它看起來像http://pastie.org/297563.txt也可以幫助你,但我沒有測試過它。
感謝sooth [http://pastie.org/297563.txt][1]正在工作 是否有任何來源的基地128編碼/解碼? – Devarshi 2010-01-13 04:59:02
請注意,所提供的粘貼中有一個來自受限密鑰空間的硬編碼密鑰(也就是說它比完整的密鑰空間密鑰更容易猜測),並且不使用IV,所以它很容易受到重放攻擊的影響。對於簡單的用法,它看起來寫得很好,但不適用於實際會遭到攻擊的東西。 – 2011-09-02 14:49:42
我使用NSData
上的一個簡單類別,它使用內置的CommonCrypto
框架來執行AES 256位加密。我在Mac上使用它,但它也應該在iPhone上正常工作:
#import <CommonCrypto/CommonCryptor.h>
@implementation NSData (AESAdditions)
- (NSData*)AES256EncryptWithKey:(NSString*)key {
// 'key' should be 32 bytes for AES256, will be null-padded otherwise
char keyPtr[kCCKeySizeAES256 + 1]; // room for terminator (unused)
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
// fetch key data
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
//See the doc: For block ciphers, the output size will always be less than or
//equal to the input size plus the size of one block.
//That's why we need to add the size of one block here
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void* buffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES256,
NULL /* initialization vector (optional) */,
[self bytes], dataLength, /* input */
buffer, bufferSize, /* output */
&numBytesEncrypted);
if (cryptStatus == kCCSuccess)
{
//the returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}
free(buffer); //free the buffer;
return nil;
}
- (NSData*)AES256DecryptWithKey:(NSString*)key {
// 'key' should be 32 bytes for AES256, will be null-padded otherwise
char keyPtr[kCCKeySizeAES256 + 1]; // room for terminator (unused)
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
// fetch key data
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
//See the doc: For block ciphers, the output size will always be less than or
//equal to the input size plus the size of one block.
//That's why we need to add the size of one block here
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void* buffer = malloc(bufferSize);
size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES256,
NULL /* initialization vector (optional) */,
[self bytes], dataLength, /* input */
buffer, bufferSize, /* output */
&numBytesDecrypted);
if (cryptStatus == kCCSuccess)
{
//the returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
}
free(buffer); //free the buffer;
return nil;
}
@end
irsk,謝謝你的回覆! 但是我們在你的代碼中面臨着與以前的代碼相同的問題! – Devarshi 2010-01-13 05:00:29
嘿,我使用NSBundle + pathForResource作爲文件路徑並選擇要加密的文件,但是如何將它連接到實際加密。我知道這是一個愚蠢的問題,但你怎麼稱呼加密和解密NSData方法在按鈕上使用?像[自我無論],但我無法找到'鑰匙'的輸入謝謝! – 2011-08-05 21:55:46
我遇到過這樣的代碼塊,它有很多安全問題。我已經用一些替代代碼在這裏寫下了它們:http://robnapier.net/blog/aes-commoncrypto-564。 – 2011-09-02 14:52:45
感謝您的大類擴展。我發現的一件事情是 - 當您使用CCCrypt的算法強於64位時,您需要符合BIS出口法規。有關更多詳細信息,請參閱iTunes Connect FAQ。即使你使用Apple的inbuild加密API,你也需要獲得BIS的批准。
有關於之前這個話題上SF的討論(在SSL使用的上下文中):
Using SSL in an iPhone App - Export Compliance
問候 克里斯
所有的例子,我發現,我沒有工作,所以我改變了上面的解決方案。這一個適用於我,並使用Google-Lib的Base64的東西:
+ (NSData *)AES256DecryptWithKey:(NSString *)key data:(NSData*)data encryptOrDecrypt:(CCOperation)encryptOrDecrypt {
// 'key' should be 32 bytes for AES256, will be null-padded otherwise
char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
// fetch key data
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
if (encryptOrDecrypt == kCCDecrypt)
{
data = [GTMBase64 decodeData:data];
}
NSUInteger dataLength = [data length];
//See the doc: For block ciphers, the output size will always be less than or
//equal to the input size plus the size of one block.
//That's why we need to add the size of one block here
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(encryptOrDecrypt,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding,
keyPtr,
kCCKeySizeAES256,
NULL /* initialization vector (optional) */,
[data bytes], dataLength, /* input */
buffer, bufferSize, /* output */
&numBytesDecrypted);
if (cryptStatus != kCCSuccess){
NSLog(@"ERROR WITH FILE ENCRYPTION/DECRYPTION");
return nil;
}
NSData *result;
if (encryptOrDecrypt == kCCDecrypt)
{
result = [NSData dataWithBytes:(const void *)buffer length:(NSUInteger)numBytesDecrypted];
}
else
{
NSData *myData = [NSData dataWithBytes:(const void *)buffer length:(NSUInteger)numBytesDecrypted];
result = [GTMBase64 encodeData:myData];
}
free(buffer); //free the buffer;
return result;
}
無論你最終使用,它是一個好主意,把它(如果它不是已經)在幾個方法進行加密和解密,然後創建一個測試案例,試圖使用這些方法來加密和解密示例字符串,就像您的應用一樣。如果樣品從另一端出來,與原始樣品相比較,您知道它正在工作。如果沒有,你知道它已經壞了。 (當你在它的時候,還要測試加密文本是不是等於或甚至包含明文的子字符串,你*真的不希望被破壞。) – 2010-01-11 12:37:42
我認爲你應該使用OpenSSL加密。其相當不錯 看到這個http://deusty.blogspot.in/2007/01/using-openssl-in-cocoa.html – Imdad 2013-06-06 22:27:58