【问题标题】:RSA Public key created in C# is not saved in iPhone keychain在 C# 中创建的 RSA 公钥未保存在 iPhone 钥匙串中
【发布时间】:2010-03-26 11:56:56
【问题描述】:

我正在尝试将 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,就ok了。但是:

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;
}

【问题讨论】:

  • 忘了说,来自服务器的字节数组有 148 个字节。如果我在 iPhone 上生成 RSA 密钥,公钥有 139 个字节。这可能是问题吗?
  • 你找到在 iOS 中导入 XML 公钥格式的方法了吗?

标签: c# objective-c cryptography rsa


【解决方案1】:

来自 MSDN 页面:

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

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

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

【讨论】:

  • 好的,以后在 iPhone 中我可以用 XML 做什么?我没有找到任何与从 iPhone 中的 xml 导入密钥相关的内容
  • 对不起,我不知道 Iphone。但是看看 XML 的元素,它们是相当标准的 RSA 密钥组件。
  • 好的,无论如何,谢谢你的回复,你让我深思
猜你喜欢
  • 1970-01-01
  • 2010-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多