iOS SecKeyRef(公钥)将其发送到服务器 [英] iOS SecKeyRef (Public Key) send it to server

查看:838
本文介绍了iOS SecKeyRef(公钥)将其发送到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

   - (NSData *)getPublicKeyBits {
OSStatus sanityCheck = noErr;
NSData * publicKeyBits = nil;

NSMutableDictionary * queryPublicKey = [[NSMutableDictionary alloc] init];

//设置公钥查询字典。
[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];

//获取关键位。
sanityCheck = SecItemCopyMatching((CFDictionaryRef)queryPublicKey,(CFTypeRef *)& publicKeyBits);

if(sanityCheck!= noErr)
{
publicKeyBits = nil;
}

[queryPublicKey release];

return publicKeyBits;
}

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



提前感谢!!!



-Murali Krishnan

解决方案

我认为iOS生成的键缺少一些标题信息,世界其他地方(你的情况下的Java API)期望看到。因此,为了使Java能够处理由iOS安全API生成的密钥,您首先需要将此标题信息添加到二进制密钥。



相反,为了使iOS Sec * API能够处理由openssl或ssh-keygen生成的键(例如),您必须先 remove

具体来说,iOS不喜欢ASN.1 OID,世界其他地方。



有关导出iOS生成的键以供Java应用程序使用的文章,请参阅此文章:



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


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

解决方案

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

这篇关于iOS SecKeyRef(公钥)将其发送到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆