cocoa - 有AES加密解密的cocoa源代码吗?

标签 cocoa encryption aes

我正在寻找一些关于 AES 加密的 cocoa 代码,并且我做了一些谷歌搜索。我发现这个非常有用的链接 - http://iphonedevelopment.blogspot.com/2009/02/strong-encryption-for-cocoa-cocoa-touch.html 。所以我尝试了一下,但它对我不起作用。

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

最佳答案

我在 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

关于cocoa - 有AES加密解密的cocoa源代码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2039940/

相关文章:

php - 使用 PHP MYSQL 加密

java - 大于 "s"的字符无法加密?

objective-c - 用一种方法两次更改 nstabview 项目

iphone - UIView 和 NSTimer 不释放内存

objective-c - 如何指定在 Objective-C 中绘制哪个 NSView?

c# - 处理 SymmetricAlgorithm 时 "try-finally" block 与 "using" block

php - AES-256-GCM 在 M1 Macbook 上的 PHP ext-sodium 中不可用

c - C 中的高级加密标准用法

java - 具有特定填充的 RSA 最大消息长度

cocoa - NSTableColumn 仅自动调整第一列的大小