iphone - 将 SecKeyRef 设备生成的公钥/私钥对保存在磁盘上

标签 iphone ios security cryptography

我在设备上使用 SecKeyGeneratePair() 在设备上生成了一个 RSA 对称 key 对。我有每个键的 SecKeyRef 结构指针。那么,如何将 SecKeyRef 保存到磁盘?甚至传输它(我也认为正确编码也存在问题)? Apple 的“证书、 key 和信任服务”指南说明

You can send your public key to anyone, who can then use it to encrypt data.

我特别想保存私钥;所以我可以在已部署的设备上使用它来解密使用公钥加密的数据。

附言我不介意每个键的结果数据是 DER 编码的 ASN.1 还是 base-64;我只需要弄清楚如何从 SecKeyRef 中提取 key 。我也很清楚 OS X 的 SecKeychainItemExport() 不存在。

最佳答案

啊,我自己找到了答案;您可以使用 SecItemCopyMatching() 获取公钥的字节数。

- (NSData *)getPublicKeyBits {
    OSStatus sanityCheck = noErr;
    NSData * publicKeyBits = nil;

    NSMutableDictionary * queryPublicKey = [[NSMutableDictionary alloc] init];

    // Set the public key query dictionary.
    [queryPublicKey setObject:(id)kSecClassKey forKey:(id)kSecClass];
    [queryPublicKey setObject:publicTag forKey:(id)kSecAttrApplicationTag];
    [queryPublicKey setObject:(id)kSecAttrKeyTypeRSA forKey:(id)kSecAttrKeyType];
    [queryPublicKey setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecReturnData];

    // Get the key bits.
    sanityCheck = SecItemCopyMatching((CFDictionaryRef)queryPublicKey, (CFTypeRef *)&publicKeyBits);

    if (sanityCheck != noErr)
    {
        publicKeyBits = nil;
    }

    [queryPublicKey release];

    return publicKeyBits;
}

以上内容来自 Apple 的 CryptoExercise。不过不确定它是否适用于私钥。

关于iphone - 将 SecKeyRef 设备生成的公钥/私钥对保存在磁盘上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5988735/

相关文章:

ios - 在 ios 应用程序中使用 facebook 应用程序登录在 safari "Login with Facebook app"中显示选项并且它在 ios 10.1 中不起作用

ios - 一个查询两个核心数据关系?

ios - 为什么我的表格 View 中的单元格不显示任何文本?

javascript - javascript 文件的身份验证

javascript - 针对未经验证的代码保护 SSJS

iphone - iOS 代码显示白色区域而不是图像系列

ios - 如何在 iOS 中录制音频并将其作为 NSData 获取?

iphone - didSelectRowAtIndexPath : nested push animations

ios - Swift 3 无法使用 UIWindow 调用函数

php - 防止浏览器控制台修改类(安全问题)