HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 为什么SecKeyEncrypt会为超过246字节的输入字符串返回paramErr(-50)?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用SecKeyEncrypt和 JSON格式的字符串作为输入.如果通过SecKeyEncrypt的plaintextLength小于246,它可以工作.如果我传递的长度为246或更多,则失败并返回值:paramErr(-50).

它可能是字符串本身的问题.我可能发送SecKeyEncrypt的一个例子是:

{"handle":"music-list","sym_key":"MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALeaEO7ZrjgOFGLBzBHZtQuzH2GNDymLWP+fIFNu5Y+59C6HECY+jt0yOXXom2mzp/WYYI/9G+Ig8OD6YiKv2nMCAwEAAQ==","app_id":"xgfdt.LibraryTestApp","api_key":"7e080f74de3625b90dd293fc8be560a5cdfafc08"}

第245个字符为’0′.

在此工作之间切换的唯一输入是plaintextLength. SecKeyGetBlockSize()返回256给我,所以任何长达256个字符的输入都应该有效.

这是我的加密方法

+ (NSData*)encrypt:(NSString*)data usingPublicKeyWithTag:(NSString*)tag
{

    OSStatus status = noErr;

    size_t cipherBufferSize;
    uint8_t *cipherBuffer;

    // [cipherBufferSize]
    size_t dataSize = 246;//[data lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
    const uint8_t* textData = [[data dataUsingEncoding:NSUTF8StringEncoding] bytes];

    SecKeyRef publicKey = [Encryption copyPublicKeyForTag:tag];

    NSAssert(publicKey,@"The public key being referenced by tag must have been stored in the keychain before attempTing to encrypt data using it!");

    //  Allocate a buffer

    cipherBufferSize = SecKeyGetBlockSize(publicKey);
    // this value will not get modified,whereas cipherBufferSize may.
    const size_t fullCipherBufferSize = cipherBufferSize;
    cipherBuffer = malloc(cipherBufferSizE);

    NSMutableData* accumulatedEncryptedData = [NSMutableData dataWithCapacity:0];

    //  Error handling

    for (int ii = 0; ii*fullCipherBufferSize < dataSize; ii++) {
        const uint8_t* dataToEncrypt = (textData+(ii*fullCipherBufferSizE));
        const size_t subsize = (((ii+1)*fullCipherBufferSizE) > dataSizE) ? fullCipherBufferSize-(((ii+1)*fullCipherBufferSizE) - dataSizE) : fullCipherBufferSize;

        // Encrypt using the public key.
        status = SecKeyEncrypt(    publicKey,kSecPaddingPKCS1,dataToEncrypt,subsize,cipherBuffer,&cipherBufferSize
                               );

        [accumulatedEncryptedData appendBytes:cipherBuffer length:cipherBufferSize];
    }

    if (publicKey) CFRelease(publicKey);

    free(cipherBuffer);

    return accumulatedEncryptedData;
}

解决方法

从文档:

(强调我的)

您正在使用PKCS1填充.因此,如果块大小为256,则一次最多只能加密245个字节.

大佬总结

以上是大佬教程为你收集整理的ios – 为什么SecKeyEncrypt会为超过246字节的输入字符串返回paramErr(-50)?全部内容,希望文章能够帮你解决ios – 为什么SecKeyEncrypt会为超过246字节的输入字符串返回paramErr(-50)?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。