【问题标题】:Python OpenSSL generating public and private key pairPython OpenSSL 生成公钥和私钥对
【发布时间】:2012-04-05 10:19:32
【问题描述】:

我在查找将使用 OpenSSL 生成公钥和私钥对的命令时遇到问题。有人可以告诉我一些示例代码吗?

谢谢

【问题讨论】:

  • 您的意思是服务器证书和密钥?可以使用openssl 命令行工具吗?您遇到问题的代码是什么?
  • 我正在使用 openssl 命令行,是的,这是用于证书的。我曾尝试使用en.wikibooks.org/wiki/…,但我认为需要将其设为 Pkey 格式 pyopenssl.sourceforge.net/pyOpenSSL.html/openssl-pkey.html
  • pyOpenSSL 与命令行工具无关。
  • 我在 PyOpenSSL 中无法生成密钥后才开始使用命令行生成密钥

标签: python openssl private public


【解决方案1】:

使用pyOpenSSL 绑定:

OpenSSL.crypto.PKey().generate_key(type, bits)

生成type 类型的公钥/私钥对(TYPE_RSATYPE_DSA 之一),大小为bits

Docs

【讨论】:

  • 怎么用这个生成一对key,不是一次生成一个吗?
  • @user:从答案中引用,然后再引用文档:“生成公钥/私钥对”
  • publicKey = Pkey() publicKey.generate_key(TYPE_RSA,128) privateKey = Pkey() privateKey.generate_key(TYPE_RSA,128) ?或者它会产生一个连音。能否提供示例代码
  • 你只需要做一次; PKey 对象包含您需要的所有数据。稍后,您可能会使用密钥对来签署证书。
  • Here's 我在 Google 上找到的一些代码。
【解决方案2】:

def makeCertificate(**kw): 密钥对 = PKey() keypair.generate_key(TYPE_RSA, 1024)

certificate = X509()
certificate.gmtime_adj_notBefore(0)
certificate.gmtime_adj_notAfter(60 * 60 * 24 * 365) # One year
for xname in certificate.get_issuer(), certificate.get_subject():
    for (k, v) in kw.items():
        setattr(xname, k, nativeString(v))

certificate.set_serial_number(counter())
certificate.set_pubkey(keypair)
certificate.sign(keypair, "md5")

return keypair, certificate 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-19
    • 2021-06-12
    • 2011-11-16
    • 2016-09-15
    • 2018-10-07
    • 1970-01-01
    • 2019-06-05
    • 1970-01-01
    相关资源
    最近更新 更多