【问题标题】:iOS: Adding a Private Key to the devices KeyChainiOS:将私钥添加到设备 KeyChain
【发布时间】:2012-03-29 00:29:26
【问题描述】:

我有一个项目,我从服务器接收用户的加密 RSA 私钥。使用用户提供的信息,我能够将数据解密回预期的格式。但是,我不知道如何将私钥加载到 iOS 钥匙串中以用于 RSA 加密功能。

目前,我有以下代码,这些代码是从各种示例中拼凑而成的。此代码适用于添加公钥,但似乎根本不适用于添加私钥。

[peerPublicKeyAttr setObject:(__bridge id)kSecClassKey forKey:(__bridge id)kSecClass];
[peerPublicKeyAttr setObject:(__bridge id)kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];
[peerPublicKeyAttr setObject:peerTag forKey:(__bridge id)kSecAttrApplicationTag];
[peerPublicKeyAttr setObject:privateKeyData forKey:(__bridge id)kSecValueData];
[peerPublicKeyAttr setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecReturnRef];

sanityCheck = SecItemDelete((__bridge CFDictionaryRef) peerPublicKeyAttr) ;

sanityCheck = SecItemAdd((__bridge CFDictionaryRef) peerPublicKeyAttr, (CFTypeRef *)&privateKey);

当我使用私钥数据(解码为 DER 格式)运行此代码时,SecItemAdd() 调用将 privateKey 变量设置为 NULL。但是,sanityCheck 变量表示“无错误”。我不知道自己错过了什么。

我需要做什么才能让私钥成功加载到钥匙串中?

【问题讨论】:

标签: ios cryptography rsa keychain private-key


【解决方案1】:

Official response from Apple 在这个确切的主题上是它不受支持。将私钥获取到钥匙串中唯一受支持的方法是通过 PKCS#12 文件。

【讨论】:

  • 但是在导入后如何访问该私钥?
  • 导入 P12 文件后,您可以使用 SecItemCopyMatching() 函数访问私钥。
  • 此答案中的链接无效。它重定向到developer.apple.com/account
【解决方案2】:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-07
  • 2023-03-19
  • 1970-01-01
  • 2012-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多