iOS SecKeyRef(公钥)发送给服务器

现在我的公钥存在一个问题,我使用SecKeyGeneratePair来生成公共和私有的。 现在我必须将我的公钥发送到服务器。 我用下面的方法将SecKeyRef转换为NSData,总是得到相同的公钥。 不过,我把它转换成base64格式并发送给服务器。 但它不工作,服务器(Java)开始抛出错误。 这是我的iOS代码:

- (NSData *)getPublicKeyBits { OSStatus sanityCheck = noErr; NSData * publicKeyBits = nil; NSMutableDictionary * queryPublicKey = [[NSMutableDictionary alloc] init]; // Set the public key query dictionary. [queryPublicKey setObject:(id)kSecClassKey forKey:(id)kSecClass]; [queryPublicKey setObject:publicTag forKey:(id)kSecAttrApplicationTag]; [queryPublicKey setObject:(id)kSecAttrKeyTypeRSA forKey:(id)kSecAttrKeyType]; [queryPublicKey setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecReturnData]; // Get the key bits. sanityCheck = SecItemCopyMatching((CFDictionaryRef)queryPublicKey, (CFTypeRef *)&publicKeyBits); if (sanityCheck != noErr) { publicKeyBits = nil; } [queryPublicKey release]; return publicKeyBits; } 

任何一个可以帮助我将SecKeyRef转换为NSString或NSData。

提前致谢!!!

-Murali Krishnan

我认为iOS生成的密钥缺less一些世界其他地方(您的案例中的Java API)希望看到的头文件信息。 因此,为了让Java使用由iOS安全API生成的密钥,您首先需要将此头信息添加到二进制密钥。

相反,为了使iOS Sec * API能够使用由openssl或ssh-keygen生成的密钥(例如),您必须首先从二进制密钥中删除此头信息。

具体而言,iOS不喜欢ASN.1 OID,世界其他地方都这样。

看到这篇文章导出一个iOS生成的密钥供Java应用程序使用 – 它应该让你去:

http://blog.wingsofhermes.org/?p=42