J'essaie d'envoyer la clé publique RSA du serveur C# à l'iPhone, afin que je puisse crypter des informations sur l'iPhone et le décrypter dans le serveur C#. Mais quand j'enregistre la clé publique reçue dans l'iPhone, elle n'est pas sauvegardée. Je créer la clé en C# comme ceci:RSA Clé publique créée en C# n'est pas enregistrée dans le trousseau iPhone
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024);
byte [] body = rsa.exportCspBlob(false);
Sur Iphone J'utilise le code de la classe de pomme 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);
Après cette opération sanitycheck est 0, c'est ok. Mais:
peerKeyRef = [self getKeyRefWithPersistentKeyRef:persistPeer];
retours 0x0 dans peerKeyRef
et la clé ne sont pas enregistrées.
- (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 oublié de dire que tableau d'octets du serveur a 148 octets. Si je génère une clé RSA sur un iPhone, la clé publique a 139 octets. Peut-être un problème dans cela? –
avez-vous trouvé un moyen d'importer le format de clé publique XML dans iOS? – yasirmturk