iOS SecKeyRef(公钥)将其发送到服务器

Mur*_*ali 9 cryptography rsa ios

现在我的公钥有问题,我使用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;
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮我将SecKeyRef转换为NSString或NSData.

提前致谢!!!

-Murali Krishnan

Jef*_*fro 9

我认为iOS生成的密钥缺少一些标题信息,这些信息是世界其他地方(在您的情况下为Java API)所期望的.因此,为了使Java能够使用iOS安全API生成的密钥,首先需要将此标头信息添加到二进制密钥.

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

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

有关导出由Java应用程序使用的iOS生成的密钥,请参阅此文章 - 它应该让您:

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