gpt4 book ai didi

ios - ios/swift 中的椭圆曲线 Diffie Hellman

转载 作者:搜寻专家 更新时间:2023-10-30 22:24:57 26 4
gpt4 key购买 nike

iOS 是否公开用于 key 生成的 API 以及使用 ECDH 的 key 派生?

据我所知,苹果正在内部使用它(特别是 x25519),但我没有看到它通过通用加密或其他方式公开为公共(public) API。

谢谢,

Z

最佳答案

在 Playground 上使用 Xcode 8.3.3 完成,使用 EC 为 Alice、Bob 生成私钥/公钥,然后使用 Alice 的私有(private) key 和 Bob 的公共(public) key 计算 Alice 的共享 key ,并使用 Bob 的私有(private) key 和 Alice 的公共(public) key 为 Bob 计算共享 key 并最终断言它们是相等的。

import Security
import UIKit

let attributes: [String: Any] =
[kSecAttrKeySizeInBits as String: 256,
kSecAttrKeyType as String: kSecAttrKeyTypeEC,
kSecPrivateKeyAttrs as String:
[kSecAttrIsPermanent as String: false]
]

var error: Unmanaged<CFError>?
if #available(iOS 10.0, *) {
// generate a key for alice
guard let privateKey1 = SecKeyCreateRandomKey(attributes as CFDictionary, &error) else {
throw error!.takeRetainedValue() as Error
}
let publicKey1 = SecKeyCopyPublicKey(privateKey1)

// generate a key for bob
guard let privateKey2 = SecKeyCreateRandomKey(attributes as CFDictionary, &error) else {
throw error!.takeRetainedValue() as Error
}
let publicKey2 = SecKeyCopyPublicKey(privateKey2)

let dict: [String: Any] = [:]

// alice is calculating the shared secret
guard let shared1 = SecKeyCopyKeyExchangeResult(privateKey1, SecKeyAlgorithm.ecdhKeyExchangeStandardX963SHA256, publicKey2!, dict as CFDictionary, &error) else {
throw error!.takeRetainedValue() as Error
}

// bob is calculating the shared secret
guard let shared2 = SecKeyCopyKeyExchangeResult(privateKey2, SecKeyAlgorithm.ecdhKeyExchangeStandardX963SHA256, publicKey1!, dict as CFDictionary, &error) else {
throw error!.takeRetainedValue() as Error
}

print(shared1==shared2)


} else {
// Fallback on earlier versions
print("unsupported")
}

感谢@Mats 指引我正确的方向..3

关于ios - ios/swift 中的椭圆曲线 Diffie Hellman,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46301197/

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