用 C# 创建的 RSA 公钥未保存在 iPhone 钥匙串中

发布于 2024-08-26 20:44:31 字数 1935 浏览 5 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪操控生活 2024-09-02 20:44:31

来自 MSDN 页面:

ExportCspBlob 方法返回一个
包含关键信息的 blob
与非托管兼容
微软加密API

所以我认为你没有理由期望 iPhone 软件能够理解它。

使用 ToXml() 可能会取得更大成功

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文