升级ios9后RSA加密SecItemAdd出问题的解决方法

升级ios9后RSA加密SecItemAdd出问题的解决方法!

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

ret = SecItemAdd((__bridge CFDictionaryRef) publicKeyDict, (CFTypeRef*)&pubKeyRef);
在iOS8系统下下面的上面这句代码没问题,ret = 0,pubKeyRef的值是:
po pubKeyRef


在ios9下,这句返回值ret = 0,但是pubKeyRef得到的值为空。------摘录至:http://bbs.csdn.net/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

up vote 1 down vote

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.csdn.net/topics/391833626?page=1#post-400541458

你可能感兴趣的:(iOS,RSA加解密)