作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我在设备上使用 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/
我是一名优秀的程序员,十分优秀!