C# 在C中创建的RSA公钥未保存在iPhone密钥链中

C# 在C中创建的RSA公钥未保存在iPhone密钥链中,c#,objective-c,cryptography,rsa,C#,Objective C,Cryptography,Rsa,我试图将RSA公钥从C服务器发送到iPhone,这样我就可以在iPhone上加密信息并在C服务器上解密。但当我在iPhone中保存收到的公钥时,它不会被保存。 我用C创建密钥,如下所示: RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024); byte [] body = rsa.exportCspBlob(false); 在Iphone上,我使用apple SecKeyWrapper类中的代码: 执行此操作

我试图将RSA公钥从C服务器发送到iPhone,这样我就可以在iPhone上加密信息并在C服务器上解密。但当我在iPhone中保存收到的公钥时,它不会被保存。 我用C创建密钥,如下所示:

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024);  
byte [] body = rsa.exportCspBlob(false);  
在Iphone上,我使用apple SecKeyWrapper类中的代码:

执行此操作后,sanityCheck为0,即ok。但是:

peerKeyRef = [self getKeyRefWithPersistentKeyRef:persistPeer];
在peerKeyRef中返回0x0,并且未保存密钥

- (SecKeyRef)getKeyRefWithPersistentKeyRef:(CFTypeRef)persistentRef
{
OSStatus sanityCheck = noErr;
SecKeyRef keyRef = NULL;

LOGGING_FACILITY(persistentRef != NULL, @"persistentRef object cannot be NULL." );

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

// Set the SecKeyRef query dictionary.
[queryKey setObject:(id)persistentRef forKey:(id)kSecValuePersistentRef];
[queryKey setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecReturnRef];

// Get the persistent key reference.
sanityCheck = SecItemCopyMatching((CFDictionaryRef)queryKey, (CFTypeRef *)&keyRef);
[queryKey release];

return keyRef;
}
从MSDN页面:

ExportCspBlob方法返回一个 包含关键信息的blob 与非托管服务器兼容 Microsoft加密API

所以我认为你没有理由期望IPhone软件能够理解它


使用ToXml可能会更成功,但忘了说,来自服务器的字节数组有148个字节。如果我在iPhone上生成RSA密钥,公钥有139个字节。这可能有问题吗?您找到在iOS中导入XML公钥格式的方法了吗?好的,那么稍后在iPhone中如何处理XML?我在Iphone中没有找到任何与从xml导入密钥相关的内容抱歉,我不知道Iphone。但是看看XML的元素,它们是相当标准的RSA关键组件
- (SecKeyRef)getKeyRefWithPersistentKeyRef:(CFTypeRef)persistentRef
{
OSStatus sanityCheck = noErr;
SecKeyRef keyRef = NULL;

LOGGING_FACILITY(persistentRef != NULL, @"persistentRef object cannot be NULL." );

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

// Set the SecKeyRef query dictionary.
[queryKey setObject:(id)persistentRef forKey:(id)kSecValuePersistentRef];
[queryKey setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecReturnRef];

// Get the persistent key reference.
sanityCheck = SecItemCopyMatching((CFDictionaryRef)queryKey, (CFTypeRef *)&keyRef);
[queryKey release];

return keyRef;
}