我正在尝试将RSA公钥从C#服务器发送到iPhone,因此我可以在iPhone上加密信息并在C#服务器中解密。但是当我在iPhone中保存收到的公钥时,它没有保存。 我在C#中创建了这样的键:
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024);
byte [] body = rsa.exportCspBlob(false);
在Iphone上我使用来自apple SecKeyWrapper
class的代码:
NSString *peerName = [NSString stringWithFormat:@"%@%@",peerNamePrefix, serverID ];
NSData * peerTag = [[NSData alloc] initWithBytes:(const void *)[peerName UTF8String] ength:[peerName length]];
NSMutableDictionary * peerPublicKeyAttr = [[NSMutableDictionary alloc] init];
[peerPublicKeyAttr setObject:(id)kSecClassKey forKey:(id)kSecClass];
[peerPublicKeyAttr setObject:(id)kSecAttrKeyTypeRSA forKey:(id)kSecAttrKeyType];
[peerPublicKeyAttr setObject:peerTag forKey:(id)kSecAttrApplicationTag];
[peerPublicKeyAttr setObject:publicKey forKey:(id)kSecValueData];
[peerPublicKeyAttr setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecReturnPersistentRef];
sanityCheck = SecItemAdd((CFDictionaryRef) peerPublicKeyAttr, (CFTypeRef *)&persistPeer);
在此操作之后,sanityCheck为0,即可。但是:
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;
}
答案 0 :(得分:0)
从MSDN页面:
ExportCspBlob方法返回一个 包含关键信息的blob 与非托管兼容 Microsoft Cryptographic API
所以我认为你没理由期望iPhone软件能够理解它。
使用ToXml()
可能会取得更大的成功