我正在使用Xcode11 Beta作为IDE和Swift。
下面是我的代码,我的代码可以在Xcode10.3上很好地工作,但是不能在Xcode11上工作。 当我调用函数SecKeyCopyKeyExchangeResult()时,堆栈总是返回错误,“缺少kSecKeyKeyExchangeParameterRequestedSize”,但是在Xcode10.3上可以。 我检查了Apple Developer Portal,没有任何线索。
`
func main(){
testECDH()
}
private func testECDH(){
generateKeyPair()
let alicePublicKey = getPublicKey()
let alicePrivateKey = getPrivateKey()
print(alicePublicKey)
print(alicePrivateKey)
generateKeyPair()
let bobPublicKey = getPublicKey()
let bobPrivateKey = getPrivateKey()
print(bobPublicKey)
print(bobPrivateKey)
let alice_bob_ecdhsecret = ecdhSecretCalculation(publicKey: alicePublicKey, privateKey: bobPrivateKey)!
let bob_alice_ecdhsecret = ecdhSecretCalculation(publicKey: bobPublicKey, privateKey: alicePrivateKey)!
os_log("alice_bob_ecdhsecret = %@", alice_bob_ecdhsecret)
os_log("bob_alice_ecdhsecret = %@", bob_alice_ecdhsecret)
}
private func generateKeyPair(){
let attributes: [String: Any] = [kSecAttrKeySizeInBits as String: 256,
kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom,
kSecPrivateKeyAttrs as String: [kSecAttrIsPermanent as String: false],
kSecPublicKeyAttrs as String:[kSecAttrIsPermanent as String: false]]
var error: Unmanaged<CFError>?
privateKey = SecKeyCreateRandomKey(attributes as CFDictionary, &error)!
if privateKey != nil {
publicKey = SecKeyCopyPublicKey(privateKey!)
}
}
private func getPrivateKey()->SecKey{
return privateKey!
}
private func getPublicKey()->SecKey{
return publicKey!
}
private func ecdhSecretCalculation(publicKey: SecKey, privateKey: SecKey) -> NSData?
{
var error: Unmanaged<CFError>?
let keyPairAttr:[String : Any] = [kSecAttrKeySizeInBits as String: 256,
kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom,
kSecPrivateKeyAttrs as String: [kSecAttrIsPermanent as String: false],
kSecPublicKeyAttrs as String:[kSecAttrIsPermanent as String: false]]
let algorithm:SecKeyAlgorithm = SecKeyAlgorithm.ecdhKeyExchangeStandardX963SHA256//ecdhKeyExchangeStandardX963SHA256
let shared:CFData? = SecKeyCopyKeyExchangeResult(privateKey, algorithm, publicKey, keyPairAttr as! CFDictionary, &error)
return shared
}`
答案 0 :(得分:0)
我只是通过下面的代码片段解决了这个问题
let keyPairAttr:[String : Any] = [kSecAttrKeySizeInBits as String: 256,
SecKeyKeyExchangeParameter.requestedSize.rawValue as String: 32,
kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom,
kSecPrivateKeyAttrs as String: [kSecAttrIsPermanent as String: false],
kSecPublicKeyAttrs as String:[kSecAttrIsPermanent as String: false]]
let algorithm:SecKeyAlgorithm = SecKeyAlgorithm.ecdhKeyExchangeStandardX963SHA256//ecdhKeyExchangeStandardX963SHA256
let shared:CFData? = SecKeyCopyKeyExchangeResult(privateKey, algorithm, publicKey, keyPairAttr as! CFDictionary, &error)
请注意,以下是我为解决此问题而添加的代码:
SecKeyKeyExchangeParameter.requestedSize.rawValue as String: 32,