2010-01-11 90 views
4

我正在寻找一些关于AES加密的可可代码,我做了一些谷歌搜索。我发现这个非常有用的链接 - http://iphonedevelopment.blogspot.com/2009/02/strong-encryption-for-cocoa-cocoa-touch.html。所以我试了一下,但对我没有用。用于AES加密解密的任何可可源代码?

任何人都可以建议我一些有用的链接或源代码,可以帮助我在我的示例应用程序中实现它。

+2

无论你最终使用,它是一个好主意,把它(如果它不是已经)在几个方法进行加密和解密,然后创建一个测试案例,试图使用这些方法来加密和解密示例字符串,就像您的应用一样。如果样品从另一端出来,与原始样品相比较,您知道它正在工作。如果没有,你知道它已经坏了。 (当你在它的时候,还要测试加密文本是不是等于或甚至包含明文的子字符串,你*真的不希望被破坏。) – 2010-01-11 12:37:42

+1

我认为你应该使用OpenSSL加密。其相当不错 看到这个http://deusty.blogspot.in/2007/01/using-openssl-in-cocoa.html – Imdad 2013-06-06 22:27:58

回答

6

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也可以帮助你,但我没有测试过它。

+0

感谢sooth [http://pastie.org/297563.txt][1]正在工作 是否有任何来源的基地128编码/解码? – Devarshi 2010-01-13 04:59:02

+1

请注意,所提供的粘贴中有一个来自受限密钥空间的硬编码密钥(也就是说它比完整的密钥空间密钥更容易猜测),并且不使用IV,所以它很容易受到重放攻击的影响。对于简单的用法,它看起来写得很好,但不适用于实际会遭到攻击的东西。 – 2011-09-02 14:49:42

13

我使用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 
+0

irsk,谢谢你的回复! 但是我们在你的代码中面临着与以前的代码相同的问题! – Devarshi 2010-01-13 05:00:29

+0

嘿,我使用NSBundle + pathForResource作为文件路径并选择要加密的文件,但是如何将它连接到实际加密。我知道这是一个愚蠢的问题,但你怎么称呼加密和解密NSData方法在按钮上使用?像[自我无论],但我无法找到'钥匙'的输入谢谢! – 2011-08-05 21:55:46

+10

我遇到过这样的代码块,它有很多安全问题。我已经用一些替代代码在这里写下了它们:http://robnapier.net/blog/aes-commoncrypto-564。 – 2011-09-02 14:52:45

1

感谢您的大类扩展。我发现的一件事情是 - 当您使用CCCrypt的算法强于64位时,您需要符合BIS出口法规。有关更多详细信息,请参阅iTunes Connect FAQ。即使你使用Apple的inbuild加密API,你也需要获得BIS的批准。

有关于之前这个话题上SF的讨论(在SSL使用的上下文中):

Using SSL in an iPhone App - Export Compliance

问候 克里斯

2

所有的例子,我发现,我没有工作,所以我改变了上面的解决方案。这一个适用于我,并使用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; 
}