升级ios9后RSA加密结果是空 问题是SecItemAdd

本文解决了iOS9中SecKeyRef生成为空的问题。在iOS9与Xcode7 Beta5环境下,使用SecItemAdd创建公钥时,尽管返回成功状态,但SecKeyRef却为nil。解决方法是在模数数据前插入一个空字节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://blog.youkuaiyun.com/ylgwhyh/article/details/49756209


问题描述(csdn上的网友):

ret = SecItemAdd((__bridge CFDictionaryRef) publicKeyDict, (CFTypeRef*)&pubKeyRef);
在iOS8系统下下面的上面这句代码没问题,ret = 0,pubKeyRef的值是:
po pubKeyRef
<SecKeyRef algorithm id: 1, key type: RSAPublicKey, version: 3, block size: 1024 bits, exponent: {hex: 10001, decimal: 65537}, modulus: B5CE747C46781C81488F169C72828B72233E6E3B70525D2CE088665EC1B61F3F5FC7FE96CFB8BFFA699D61D0316ACC8C021A03ABF703C75510990F95008E4A3303ACF424B3E7EFEA001D0499CE00EB293A79B2054D11852E66D81EABF9B714A0013611059810C4CD670B50AC5D2E6B743049A5297AD38690C25BB3BBF95A331D, addr: 0x15e0baa00>

在ios9下,这句返回值ret = 0,但是pubKeyRef得到的值为空。------摘录至:http://bbs.youkuaiyun.com/topics/391833626


问题描述:(苹果官方论坛上面网友的描述):

Hi Guys,

Recenlty I have been testing my code in iOS 9 with Xcode 7 Beta 5. In my app, I am using + (SecKeyRef) addPeerPublicKey:(NSString *) peerName withPublicKey:(NSData *) publicKey method to generate public key from exponent and modulus. It turns out that SecKeyRef generate is always nil in iOS 9 but works well in iOS 8. Is it bug for the Xcode 7 Beta 5 or There are several changing that i need to do in order to make it works in iOS 9?

Please help.

Thanks.

Best Regards, 

Chris               摘录至:https://forums.developer.apple.com/thread/15129


解决办法:

原文链接:http://stackoverflow.com/questions/32096304/ios-9-secitemcopymatching-returns-successful-status-code-but-key-is-nil

The bug occurs due to malformed public key referhttps://forums.developer.apple.com/thread/15129

If you use Basic Encoding Rules library here's the solution.

To fix your public key you need to insert nil byte before modulus data.https://github.com/StCredZero/SCZ-BasicEncodingRules-iOS/issues/6#issuecomment-136601437

P.S. For me fix was as simple as:

const char fixByte = 0;
NSMutableData * fixedModule = [NSMutableData dataWithBytes:&fixByte length:1];
[fixedModule appendData:modulusData];
share improve this answer

其他有用的参考资料:https://github.com/StCredZero/SCZ-BasicEncodingRules-iOS/issues/6#issuecomment-136601437

http://bbs.youkuaiyun.com/topics/391833626?page=1#post-400541458


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值