开发者

RSA Public key created in C# is not saved in iPhone keychain

开发者 https://www.devze.com 2022-12-24 14:06 出处:网络
I\'m trying to send RSA public key from C# server to iPhone, so I could encrypt info on iPhone and decrypt it in C# server.But when I save received public key in iPhone, it\'snot saved.

I'm trying to send RSA public key from C# server to iPhone, so I could encrypt info on iPhone and decrypt it in C# server. But when I save received public key in iPhone, it's not saved. I create key in C# like this:

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024);  
byte [] body = rsa.exportCspBlob(false);  

On Iphone I use code from 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);

After this operation sanityCheck is 0, that is ok. But:

peerKeyRef = [self getKeyRefWithPersistentKeyRef:persistPeer];

returns 0x0 in peerKeyRef and key is not saved.

- (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开发者_C百科 release];

return keyRef;
}


From the MSDN page:

The ExportCspBlob method returns a blob containing key information that is compatible with the unmanaged Microsoft Cryptographic API

So I think you have no reason to expect the IPhone software to understand it.

You may hae more success with ToXml()

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号