iOS SecKeyRef (Public Key) send it to server [duplicate]

前提是你 提交于 2020-01-01 09:04:25

问题


Now I have a problem with my public key, I used SecKeyGeneratePair to generate public and private. Now i have to send my public key to the server. I have used the below method to convert SecKeyRef to NSData, there always I am getting same public key. However I converted it into base64 format and send it to the server. But its not working at all and server(Java) start throwing errors. Here is my iOS code:

- (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;
}

Can any one help me to convert SecKeyRef to NSString or NSData.

Thanks in advance!!!

-Murali Krishnan


回答1:


I think keys generated by iOS are missing some header information that the rest of the world (Java APIs in your case) expect to see. Therefore, in order for Java to work with a key generated by iOS Security APIs, you first need to add this header information to the binary key.

Conversely, in order for the iOS Sec* APIs to work with a key generated by openssl or ssh-keygen (for example), you must first remove this header information from the binary key.

Specifically, iOS doesn't like the ASN.1 OID, and the rest of the world does.

See this article for exporting an iOS-generated key for use by a Java app -- it should get you going:

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



来源:https://stackoverflow.com/questions/10850306/ios-seckeyref-public-key-send-it-to-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!