Python OpenSSL generating public and private key pair

Question:

I am having problem finding a command that would generate a public and private key pair using OpenSSL. Could someone show me some example code of this in action.

Thank you

Asked By: DustBunny

||

Answers:

Using the pyOpenSSL bindings:

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

Generate a public/private key pair of the type type (one of TYPE_RSA and TYPE_DSA) with the size bits.

Docs

Answered By: Katriel
def makeCertificate(**kw):
    keypair = 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 
Answered By: Aaditya khandelwal
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.