gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 08:16:13 25 4
gpt4 key购买 nike

我在设备上使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com