ios – 为什么SecKeyEncrypt会为超过246字节的输入字符串返回paramErr(-50)?

ios – 为什么SecKeyEncrypt会为超过246字节的输入字符串返回paramErr(-50)?,第1张

概述我使用SecKeyEncrypt和 JSON格式的字符串作为输入.如果通过SecKeyEncrypt的plainTextLength小于246,它可以工作.如果我传递的长度为246或更多,则失败并返回值:paramErr(-50). 它可能是字符串本身的问题.我可能发送SecKeyEncrypt的一个例子是: {"handle":"music-list","sym_key":"MFwwDQYJK 我使用SecKeyEncrypt和 @L_403_0@格式的字符串作为输入.如果通过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;}
解决方法 从文档:

plainTextLen
Length in bytes of the data in the plainText buffer. This must be less than or equal to the value returned by the SecKeyGetBlockSize function. When PKCS1 padding is performed,the maximum length of data that can be encrypted is 11 bytes less than the value returned by the SecKeyGetBlockSize function (secKeyGetBlockSize() – 11).

(强调我的)

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

总结

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

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

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1106281.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存