views:

201

answers:

1

Hello. 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 release];

return keyRef;
}
A: 

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()

Henk Holterman
Ok, and what can I do with XML later in iPhone? I didn't found anything related to import keys from xml in iPhone
sYgiL
Sorry, I don't know the Iphone. But take a look at the elements of the XML, they're fairly standard RSA key components.
Henk Holterman
Ok, any case, thank you for response, You gave me food for thought
sYgiL