问题描述
我正在尝试在我的 iOS 应用与其 watchOS 等效应用之间同步私有非对称密钥.我尝试使用 SecKeyCopyExternalRepresentation
将其导出为 CFData
,然后使用 WatchConnectivity
将其发送到手表.但是,当它到达手表时,我无法将数据转换回 SecKey
.我尝试使用 SecKeyCreateWithData
试图重新创建它,但它似乎只适用于对称密钥,因为当我尝试它时它使手表应用程序崩溃.有什么想法吗?
I am trying to sync up private asymmetric keys between my iOS app and its watchOS equivalent. I have tried using SecKeyCopyExternalRepresentation
to export it out as CFData
and then send it to the watch using WatchConnectivity
. However when it gets to the watch I have no way of converting the Data back into a SecKey
. I tried using SecKeyCreateWithData
in an attempt to recreate it, but it seems that that only works with symmetric keys, for when I tried it it crashed the watch app. Any ideas?
iOS 代码:
func sendSharedKeyPair(keyPair: (publicKey: SecKey, privateKey: SecKey)) {
var error: Unmanaged<CFError>?
let publicKeyData = SecKeyCopyExternalRepresentation(keyPair.publicKey, &error)
if let error = error {
return print("Error sending shared key: \(error)")
}
let privateKeyData = SecKeyCopyExternalRepresentation(keyPair.privateKey, &error)
if let error = error {
return print("Error sending shared key: \(error)")
}
if let publicKeyData = publicKeyData, let privateKeyData = privateKeyData {
session.sendMessage(["requestedCommand": WatchControllerCommands.sendSharedKeyPair.rawValue, "keyPair": ["publicKey": publicKeyData, "privateKey": privateKeyData]], replyHandler: nil, errorHandler: { error in
print(error)
})
}
}
watchOS 代码:
watchOS Code:
func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
guard let requestedCommand = (message["requestedCommand"] as? String).flatMap({ WatchControllerCommands(rawValue: $0) }), requestedCommand == .sendSharedKeyPair else { return }
guard let publicKeyData = (message["keyPair"] as? [String: Any])?["publicKey"].flatMap({ $0 as? Data }), let privateKeyData = (message["keyPair"] as? [String: Any])?["privateKey"].flatMap({ $0 as? Data }) else { return print("Couldn't parse keys") }
let publicTag = "myAppTag"
let privateTag = publicTag + ".private"
let privateAttributes = [String(kSecAttrIsPermanent): true,
String(kSecAttrApplicationTag): privateTag] as [String : Any]
let publicAttributes = [String(kSecAttrIsPermanent): true,
String(kSecAttrApplicationTag): publicTag] as [String : Any]
var error: Unmanaged<CFError>?
let publicCFData = publicKeyData as CFData
let privateCFData = privateKeyData as CFData
let publicCFDict = publicAttributes as CFDictionary
let privateCFDict = privateAttributes as CFDictionary
SecKeyCreateWithData(publicCFData, publicCFDict, &error)
if let error = error {
print(error)
}
SecKeyCreateWithData(privateCFData, privateCFDict, &error)
if let error = error {
print(error)
}
}
推荐答案
来自 SecKeyCreateWithData
的标题文档:
@param attributes 包含描述键的属性的字典要进口.该字典中的键是 kSecAttr* 常量来自 SecItem.h.强制性属性是: * kSecAttrKeyType *kSecAttrKeyClass * kSecAttrKeySizeInBits
您的代码仅定义了 kSecAttrIsPermanent
和 kSecAttrApplicationTag
属性.
Your code only defines kSecAttrIsPermanent
and kSecAttrApplicationTag
attributes.
这篇关于将 SecKey 从 iOS 导出到 watchOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!