小编Nik*_*nov的帖子

奇怪的SecKeyEncrypt行为

我正在尝试使用SecKeyEncrypt函数使用PKCS1填充实现RSA加密.

代码如下:

NSData *encryptText(NSString *text, SecKeyRef publicKey)
{
    NSCParameterAssert(text.length > 0);
    NSCParameterAssert(publicKey != NULL);
    NSData *dataToEncrypt = [text dataUsingEncoding:NSUTF8StringEncoding];
    const uint8_t *bytesToEncrypt = dataToEncrypt.bytes;

    size_t cipherBufferSize = SecKeyGetBlockSize(publicKey);
    NSCAssert(cipherBufferSize > 11, @"block size is too small: %zd", cipherBufferSize);

    const size_t inputBlockSize = cipherBufferSize - 11; // since we'll use PKCS1 padding
    uint8_t *cipherBuffer = (uint8_t *) malloc(sizeof(uint8_t) * cipherBufferSize);

    NSMutableData *accumulator = [[NSMutableData alloc] init];

    @try {

        for (size_t block = 0; block * inputBlockSize < dataToEncrypt.length; block++) {
            size_t …
Run Code Online (Sandbox Code Playgroud)

cocoa-touch rsa objective-c ios

5
推荐指数
0
解决办法
2030
查看次数

标签 统计

cocoa-touch ×1

ios ×1

objective-c ×1

rsa ×1