【问题标题】:CCCrypt encryption result issueCCCrypt 加密结果问题
【发布时间】:2012-04-05 15:04:52
【问题描述】:

我正在使用 CCCrypt 加密/解密字符串值。它似乎工作正常,因为解密值等于初始值,但问题是我无法从解密数据中提取正确的 NSString 对象。

@implementation NSData (AES256) 

- (NSData *)AES256EncryptWithKey:(NSString *)key 
{
    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];

    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) { 
        return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
    }

    free(buffer); //free the buffer;
    return nil;
}

- (NSData *)AES256DecryptWithKey:(NSString *)key {
    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];

    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;
}

我不知道做错了什么,所以如果有人能提供帮助,我将不胜感激。

问题的第二部分 - 如何为 AES 加密方法设置 CBC 模式。文档中说默认使用此模式,但我应该将什么作为 CCCrypt 方法中的第三个参数传递?

【问题讨论】:

    标签: objective-c encryption aes encryption-symmetric


    【解决方案1】:

    解决您在上一个答案中评论的问题,initWithData:encoding: 函数将返回 nil,因为加密数据不太可能与 NSUTF8StringEncoding 匹配,因为它是加密的。

    同样的问题和彻底的答案:Why does my initWithData return nil indicating an error after converting NSData to NSString returning from encrypting via CommonCrypto? 虽然顺便说一句,base64 不会使您的加密数据成为可见字符串,而是进一步改变它以制作可显示的 ASCII 字符串。不应假定此字符串与加密数据相同,也不应假定与加密数据相同,但作为字符串使用,例如在钥匙串中,它工作得很好。

    【讨论】:

    • 感谢您证明我的建议! :)
    【解决方案2】:

    要在解密后获得准确的字符串,您必须对 Decrypted Data 进行 base64 解码,然后使用 NSUTF8StringEncoding 创建 NSString。

        NSString decryptedString = [[NSString alloc]initWithData:decryptedData];
    
        NSData *data = [NSData dataByBase64DecodingString:decryptedString];
    
        decryptedString = [data dataUsingEncoding:NSUTF8StrinEncoding];
    

    decryptedString 保持字符串与加密前相同。

    关于 base64 解码,请参阅 this 帖子。

    【讨论】:

    • 解密后的字符串应该是完全一样的。问题是我看不到像字符串一样的加密数据-当我执行NSString *encryptedString = [[NSString alloc] initWithData:encryptedData encoding:NSUTF8StringEncoding]; 并打印那些encryptedString 时,我得到(null)。还是我不应该查看加密数据或类似的方式?
    猜你喜欢
    • 2012-07-11
    • 1970-01-01
    • 2012-10-27
    • 2011-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多