【问题标题】:SecKeyDecrypt does not always decrypt with RSA in iOSSecKeyDecrypt 在 iOS 中并不总是使用 RSA 解密
【发布时间】:2015-05-22 18:10:49
【问题描述】:

我在 .NET 中使用 RSACrpytoServiceProvider 用我的由 iOS 生成的公钥加密数据。 在 iOS 端,使用相同的私钥,有时解密成功有时不成功。我在 .NET 中使用相同的公钥创建不同的密文,并使用 Base64 编码传递它。在 iOS 中,我解码 Base64 并将此方法作为内容发送。 我使用 SecKeyGeneratePair 生成密钥对。我在生成之前删除了具有相同标签的密钥对。 SecKeyDecrypt 返回的错误是:OSStatus return error code -9809 operation could not be completed. 可能是什么问题?

size_t cipherBufferSize = [content length];
void *cipherBuffer = malloc(cipherBufferSize);
[content getBytes:cipherBuffer length:cipherBufferSize];

size_t plainBufferSize = [content length];

uint8_t *plainBuffer = malloc(plainBufferSize);

OSStatus sanityCheck = SecKeyDecrypt(key,
                                     kSecPaddingPKCS1,
                                     cipherBuffer,
                                     cipherBufferSize,
                                     plainBuffer,
                                     &plainBufferSize);

【问题讨论】:

    标签: ios security encryption rsa


    【解决方案1】:

    经过一番挖掘, 我意识到从公钥中提取的模数是 129 字节。它必须是 128。我使用getPublicKeyModFromKeyData 方法来提取模数。 我发现这在开头增加了一个额外的字节。我删除了那个字节现在它可以工作了。感谢您的帮助。

    - (NSData *)getPublicKeyModFromKeyData:(NSData*)pk
    {
    if (pk == NULL) return NULL;
    
    int iterator = 0;
    
    iterator++; // TYPE - bit stream - mod + exp
    [self derEncodingGetSizeFrom:pk at:&iterator]; // Total size
    
    iterator++; // TYPE - bit stream mod
    int mod_size = [self derEncodingGetSizeFrom:pk at:&iterator];
    
    // return [pk subdataWithRange:NSMakeRange(iterator, mod_size)];
    NSData* subData=[pk subdataWithRange:NSMakeRange(iterator, mod_size)];
    return  [subData subdataWithRange:NSMakeRange(1, subData.length-1)];
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-21
      • 2014-11-24
      • 1970-01-01
      • 2014-06-30
      • 2020-02-17
      • 1970-01-01
      • 1970-01-01
      • 2020-02-28
      相关资源
      最近更新 更多