You need to enable JavaScript to run this app. iOS基于Objective-C与SwiftUI的RNCryptor AES解密返回null求助
优惠活动
大模型
产品
解决方案
定价
更多

iOS基于Objective-C与SwiftUI的RNCryptor AES解密返回null求助

代码问题定位
  • 密文结构不兼容:加密函数仅返回纯加密后的密文内容,随机生成的IV、salt通过独立出参传递,未拼接到返回的密文中;但解密函数默认将传入加密数据的前16字节作为IV、后续内容作为密文读取,直接错位解析导致IV完全错误,无法完成解密。
  • 密钥逻辑断裂:加密流程使用「用户密码 + 8字节随机salt」通过PBKDF2算法派生AES密钥,解密函数却直接接收外部传入的key参数,没有基于对应salt重新派生匹配密钥的逻辑,传入的密钥内容与加密时使用的密钥完全不一致。
  • 输出缓冲区长度不足:解密时将输出缓冲区长度设置为data.length - ivLength,小于CCCrypt要求的「输入数据长度 + 单块大小」的最小阈值,易触发缓冲不足错误,直接返回解密失败。
  • 缺少数据合法性校验:未校验传入解密流程的数据源长度是否满足最小结构要求,遇到长度过短的无效数据会直接触发内存访问越界。
修复实现

调整加密逻辑将salt、IV拼接到密文返回,解密时从密文头部拆分参数、重新派生密钥,打通完整流程:

  1. 修改加密方法,移除单独的IV、salt出参,返回结构为「8字节随机salt + 16字节随机IV + 密文」的整体数据:
    + (NSData *)encryptedDataForData:(NSData *)data
                            password:(NSString *)password
                              error:(NSError **)error {
        NSData *salt = [self randomDataOfLength:kPBKDFSaltSize];
        NSData *iv = [self randomDataOfLength:kAlgorithmIVSize];
        NSData *key = [self AESKeyForPassword:password salt:salt];
        
        size_t outLength;
        NSMutableData *cipherData = [NSMutableData dataWithLength:data.length + kAlgorithmBlockSize];
        CCCryptorStatus result = CCCrypt(kCCEncrypt,
                                         kAlgorithm,
                                         kCCOptionPKCS7Padding,
                                         key.bytes,
                                         key.length,
                                         iv.bytes,
                                         data.bytes,
                                         data.length,
                                         cipherData.mutableBytes,
                                         cipherData.length,
                                         &outLength);
        if (result != kCCSuccess) {
            if (error) {
                *error = [NSError errorWithDomain:kRNCryptManagerErrorDomain
                                             code:result
                                         userInfo:nil];
            }
            return nil;
        }
        cipherData.length = outLength;
        
        NSMutableData *resultData = [NSMutableData data];
        [resultData appendData:salt];
        [resultData appendData:iv];
        [resultData appendData:cipherData];
        return resultData;
    }
    
  2. 重写解密方法,从密文头部拆分salt和IV,使用相同密码重新派生密钥,预留足够输出缓冲区空间:
    + (NSData *)decryptData:(NSData *)data
                password:(NSString *)password
                   error:(NSError **)error {
        // 校验数据最小长度:salt + IV + 至少1字节密文
        if (data.length < kPBKDFSaltSize + kAlgorithmIVSize + 1) {
            if (error) {
                *error = [NSError errorWithDomain:kRNCryptManagerErrorDomain
                                             code:-1
                                         userInfo:@{NSLocalizedDescriptionKey: @"无效的加密数据长度"}];
            }
            return nil;
        }
        
        // 拆分头部参数
        NSData *salt = [data subdataWithRange:NSMakeRange(0, kPBKDFSaltSize)];
        NSData *iv = [data subdataWithRange:NSMakeRange(kPBKDFSaltSize, kAlgorithmIVSize)];
        NSData *cipherData = [data subdataWithRange:NSMakeRange(kPBKDFSaltSize + kAlgorithmIVSize, data.length - (kPBKDFSaltSize + kAlgorithmIVSize))];
        
        // 派生匹配的密钥
        NSData *key = [self AESKeyForPassword:password salt:salt];
        
        // 预留足够输出缓冲:密文长度 + 单块大小,避免缓冲不足
        size_t outLength;
        NSMutableData *clearData = [NSMutableData dataWithLength:cipherData.length + kAlgorithmBlockSize];
        CCCryptorStatus result = CCCrypt(kCCDecrypt,
                                         kAlgorithm,
                                         kCCOptionPKCS7Padding,
                                         key.bytes,
                                         key.length,
                                         iv.bytes,
                                         cipherData.bytes,
                                         cipherData.length,
                                         clearData.mutableBytes,
                                         clearData.length,
                                         &outLength);
        if (result != kCCSuccess) {
            if (error) {
                *error = [NSError errorWithDomain:kRNCryptManagerErrorDomain
                                             code:result
                                         userInfo:nil];
            }
            return nil;
        }
        clearData.length = outLength;
        return clearData;
    }
    

调整后加密和解密仅需传入对应的用户密码,无需手动维护IV、salt、密钥的传递,从根源上避免参数错位导致的解密失败。

内容的提问来源于stack exchange,提问作者User1075

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.19 09:06:08