用 C# 创建的 RSA 公钥未保存在 iPhone 钥匙串中
我正在尝试将 RSA 公钥从 C# 服务器发送到 iPhone,这样我就可以在 iPhone 上加密信息并在 C# 服务器中解密。但是当我在 iPhone 中保存收到的公钥时,它没有保存。 我在 C# 中创建密钥,如下所示:
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];
在 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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 MSDN 页面:
所以我认为你没有理由期望 iPhone 软件能够理解它。
使用 ToXml() 可能会取得更大成功
From the MSDN page:
So I think you have no reason to expect the IPhone software to understand it.
You may hae more success with ToXml()