iOS/Swift中的椭圆曲线Diffie Hellman

编程入门 行业动态 更新时间:2024-10-26 02:30:39
本文介绍了iOS/Swift中的椭圆曲线Diffie Hellman的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

iOS是否公开用于密钥生成和使用ECDH进行秘密密钥派生的API?

Does iOS expose API for key generation, and secret key derivation using ECDH?

据我所知,苹果内部使用它(特别是x25519),但我看不到它是通过公共加密或其他方式公开为公共API的.

From what I see, apple are using it (and specifically x25519) internally but I don't see it exposed as public API by common crypto or otherwise.

谢谢

Z

推荐答案

使用Xcode 8.3.3在操场上完成,使用EC为Alice,Bob生成私钥/公钥,然后使用Alice的private和鲍勃(Bob)的公开,并使用鲍勃(Bob)的私有和爱丽丝(Alice)的公开,为鲍勃(Bob)共享秘密,并最终断言它们是平等的.

Done in playground with Xcode 8.3.3, generates a private/public key using EC for Alice, Bob, then calculating the shared secret for Alice using Alice's private and Bob's public, and share secret for Bob using Bob's private and Alice's public and finally asserting that they're equal.

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

Thanks @Mats for sending me in the right direction..3

更多推荐

iOS/Swift中的椭圆曲线Diffie Hellman

本文发布于:2023-11-16 23:14:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1607896.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:椭圆   曲线   Swift   iOS   Diffie

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!