【问题标题】:Decrypt large amount of RSA encrypted data解密大量 RSA 加密数据
【发布时间】:2014-04-01 09:49:58
【问题描述】:

大家好,我需要简单的 RSA 加密解密。 我尝试了 Apple 开发人员指南中的代码示例,它适用于少量文本,但示例代码不适合大型加密数据的情况。

注意它建议我们“将数据拆分为等于 plainBufferSize 的块”的评论:

    - (NSData*)decryptedDataFromData:(NSData*)data usingKey:(SecKeyRef)key
    {
        OSStatus status = noErr;

        size_t cipherBufferSize = [data length];
        uint8_t *cipherBuffer = (uint8_t *)[data bytes];

        size_t plainBufferSize;
        uint8_t *plainBuffer;

        //  Allocate the buffer
        plainBufferSize = SecKeyGetBlockSize(key);
        plainBuffer = malloc(plainBufferSize);

        if (plainBufferSize < cipherBufferSize) {
            // Ordinarily, you would split the data up into blocks
            // equal to plainBufferSize, with the last block being
            // shorter. For simplicity, this example assumes that
            // the data is short enough to fit.
            printf("Could not decrypt.  Packet too large.\n");
            return nil;
        }

        //  Error handling
        status = SecKeyDecrypt(key,
                               kSecPaddingPKCS1,
                               cipherBuffer,
                               cipherBufferSize,
                               plainBuffer,
                               &plainBufferSize
                               );                              // 3

        //  Error handling
        //  Store or display the decrypted text

        if(key) CFRelease(key);

        NSData *decrypted = [NSData dataWithBytes:(const void *)plainBuffer length:plainBufferSize];
        return decrypted;
    }

关于我应该如何修改此方法以便它将数据分成块以处理大量数据的任何线索?

【问题讨论】:

    标签: objective-c encryption cryptography rsa


    【解决方案1】:

    根据RFC3447 RSAES-PKCS1-v1_5,您使用的加密方案可以对长度为 k - 11 个八位字节的消息进行操作(k 是 RSA 模数的八位字节长度),因此如果您使用的是 2048 位 RSA key 那么要加密的明文数据的最大长度是 245 字节。因此,您需要将纯数据拆分为这种大小的块,然后单独加密它们中的每一个,但这是相当罕见且缓慢的解决方案。生成对称 AES 密钥,使用 AES 算法加密大数据,然后使用 RSA 密钥加密小的 AES 密钥会更好(也很常见)。

    【讨论】:

    • 嗨,jariq,感谢您为我指明了正确的方向。是否有任何现有的objective-c 库可以执行您建议的混合解决方案? (我的最终目标是让应用程序用户能够将数据上传到我的服务器上,这些数据只能由预期的接收者解密,我想确保即使我无法读取它们)
    • 顺便说一句,我想我会使用现有的 Apple 相同的代码 + RNCryptor :D
    猜你喜欢
    • 1970-01-01
    • 2012-03-13
    • 2015-03-16
    • 1970-01-01
    • 2013-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    相关资源
    最近更新 更多