【发布时间】: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