我試圖從C#服務器發送RSA公鑰到iPhone,所以我可以在iPhone上加密信息並在C#服務器中解密。但是,當我在iPhone中保存收到的公鑰時,它不會被保存。 我C#創建關鍵是這樣的:在C#中創建的RSA公鑰不保存在iPhone鑰匙串
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024);
byte [] body = rsa.exportCspBlob(false);
在iPhone上我使用蘋果SecKeyWrapper
類代碼:
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];
返回0x0在peerKeyRef
和密鑰不保存。
- (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;
}
忘了說了,從服務器的字節數組有148個字節。 如果我在iPhone上生成RSA密鑰,公鑰有139個字節。可能是這個問題? –
您是否找到了在iOS中導入XML公鑰格式的方法? – yasirmturk