【问题标题】:How can we retrieve public key from private key ( protected with passphrase ) using python's Cryptography library?我们如何使用 python 的 Cryptography 库从私钥(受密码保护)中检索公钥?
【发布时间】:2018-06-29 10:39:29
【问题描述】:

我想使用 python 的Cryptography library 从私钥获取公钥(也可以通过密码保护)。我怎样才能做到类似于 Python 的 CryptoDome library

【问题讨论】:

  • 使用public_key() 方法。
  • James K,你能指点我的链接吗?我没找到。
  • Here it is 用于 RSA。 here it is 用于椭圆曲线,here 用于 diffie-hellman。每种类型的私钥都有一个名为public_key()的方法。
  • @JamesKPolk 使用这个 API,我没有得到任何 ssh-keygen -y -f 作为公钥返回。显然,给定的私钥可以有无限个公钥,而且它生成的公钥以 BEGIN PUBLIC KEY 开头。上面的 ssh-keygen 命令返回公钥,例如: ssh-rsa ABCD 。无论上述 ssh-keygen 命令返回什么,我都可以获得公钥吗?我使用 Cryptography 库检索公钥的方式是:pastebin.com/UHjKbC0Q
  • 我用谷歌搜索,看起来我必须使用 serialization.Encoding.OpenSSHserialization.PublicFormat.OpenSSH 。对吗?

标签: python ssh cryptography public-key-encryption python-cryptography


【解决方案1】:

这应该适合你:

key_file = open('key.text', 'rb')
private_key = serialization.load_pem_private_key(
    key_file.read(),
    password=None,
    backend=default_backend()
    )
public_key = private_key.public_key()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-09
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    相关资源
    最近更新 更多