Any cocoa source code for AES encryption decryption?
我在AES加密上搜索一些可可代码,我在谷歌上搜索了一下。我发现这个链接很有用-http://iphonedevelopment.blogspot.com/2009/02/strong-encryption-for-cocoa-cocoa-touch.html。所以我试过了,但不管用。
有人能给我推荐一些有用的链接或源代码来帮助我在示例应用程序中实现它吗?
我在
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | #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 |
号
AES128加密在iPhone上的CommonCrypto框架中可用。相关函数在commoncryptor.h头中。
你可以这样创建一个密码器:
1 2 3 4 5 6 7 8 | // 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的示例实现。我不建议这样做-使用苹果的实现!
看起来http://pastie.org/297563.txt也可以帮助您,但我还没有测试过。
我发现的所有示例都不适用于我,因此我更改了上面的解决方案。这一个适用于我,使用GoogleLib来处理base64的东西:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | + (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; } |
感谢您的大类扩展。我发现了一件事——当你使用比64位更强的CCcrypt算法时,你需要遵守国际清算银行的出口法规。有关详细信息,请参阅iTunes Connect常见问题解答。即使你使用苹果的内置加密API,你也需要得到国际清算银行的批准。
之前有一个关于sf的讨论(在使用ssl的上下文中):
在iPhone应用程序中使用SSL-导出合规性
顺祝商祺!克里斯