简体   繁体   English

pyOpenSSL创建一个pem文件

[英]pyOpenSSL creating a pem file

I've created a key pair using the following code in python with pyOpenSSL: 我用pyOpenSSL在python中使用以下代码创建了一个密钥对:

from OpenSSL import crypto
k = crypto.PKey()
k.generate_key(crypto.TYPE_RSA, 2048)
  1. Now how can I create the private and public key .pem files from the key object? 现在我如何从密钥对象创建私钥和公钥.pem文件?
  2. If there is any tutorial available please let me know. 如果有任何教程,请告诉我。 I found none. 我找不到。 From the manual, it's difficult to know as I'm new to OpenSSL. 从手册中,我很难知道我是OpenSSL的新手。
  3. What are the chances that the same code will create two same key pairs is there is no specific unique key is being used in RSA? 如果RSA中没有使用特定的唯一密钥,那么相同代码创建两个相同密钥对的可能性有多大?

I hope this will help people in the future, because I had this same need and couldn't find an answer so I did it myself. 我希望这将有助于未来的人们,因为我有同样的需求而无法找到答案所以我自己做了。 Thought I would share it with you. 以为我会和你分享。

1. Creating a PEM file 1.创建PEM文件

bio_pub = _new_mem_buf()  # Memory buffers to write to
bio_priv = _new_mem_buf()

helper = OpenSSL.crypto._PassphraseHelper(OpenSSL.crypto.FILETYPE_PEM, None)

pk = OpenSSL.crypto.PKey()
pk.generate_key(OpenSSL.crypto.TYPE_RSA, n)

# Convert from EVP_PKEY type to RSA type
rsa_pkey = _lib.EVP_PKEY_get1_RSA(pk._pkey)


result_code = _lib.PEM_write_bio_RSAPublicKey(bio_pub, rsa_pkey)
result_code = _lib.PEM_write_bio_RSAPrivateKey(
    bio_priv, rsa_pkey, _ffi.NULL, _ffi.NULL, 0,
    helper.callback, helper.callback_args)

After this part you will have the public and private keys in your buffers. 在此部分之后,您将在缓冲区中拥有公钥和私钥。 To get it as a string you can call the functions: 要将其作为字符串,您可以调用函数:

_bio_to_string(bio_pub), _bio_to_string(bio_priv)

I used these imports for the special "private" functions of OpenSSL.crypto: 我将这些导入用于OpenSSL.crypto的特殊“私有”功能:

import OpenSSL
from OpenSSL._util import lib as _lib, ffi as _ffi
from OpenSSL.crypto import _new_mem_buf, _bio_to_string

I know this is an old question - but as I've just found it I thought I'd add an answer. 我知道这是一个老问题 - 但是我刚刚发现它,我想我会添加一个答案。

The easiest way to do this with Python 3.x is to use PyCryptodome . 使用Python 3.x执行此操作的最简单方法是使用PyCryptodome

The in Python (for a 2048-bit key): 在Python中(对于2048位密钥):

from Cryptodome.PublicKey import RSA
key = RSA.generate(2048)
pv_key_string = key.exportKey()
with open ("private.pem", "w") as prv_file:
    print("{}".format(pv_key_string.decode()), file=prv_file)

pb_key_string = key.publickey().exportKey()
 with open ("public.pem", "w") as pub_file:
    print("{}".format(pb_key_string.decode()), file=pub_file)

If you want to check the private key on the (Linux) command-line use: 如果要检查(Linux)命令行上的私钥,请使用:

$ openssl rsa -check -inform pem -noout -in private.pem 
RSA key ok
...

You can create a .pem key by follow this tutorial at: 您可以按照以下教程创建.pem键:

https://help.ubuntu.com/community/OpenSSL https://help.ubuntu.com/community/OpenSSL

that suppose you want to create a CA(certificate authority) certificate, that is little complicate because you already have to get a CA from somewhere because it's not free. 假设你想创建一个CA(证书颁发机构)证书,这有点复杂,因为你已经必须从某个地方获得一个CA,因为它不是免费的。

if you only want to create a key juste for your ssl connection test it better to create a self-sign certificate. 如果您只想为ssl连接测试创建密钥juste,最好创建自签名证书。

then make sure first you have install openssl and you have resolve the CN (Common Name) on your serve. 然后确保首先安装openssl并解决服务器上的CN(通用名称)问题。 without that you will be in trouble to use the created certificate. 没有它你将有麻烦使用创建的证书。

for the Self-sign certificate use this command line: 对于自签名证书,请使用以下命令行:

$ openssl genrsa -des3 -passout pass:x -out server.pass.key 2048
$ openssl rsa -passin pass:x -in server.pass.key -out server.key
$ rm server.pass.key
$ openssl req -new -key server.key -out server.csr (list of question to answer)
$ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt 

after you got the certificate create you have to activate your server mod-ssl and add the line where is locate your certificate. 获得证书后,您必须激活服务器mod-ssl并添加找到证书的行。 later you have to insert that certificate in your IE certificate list to get it work with you apache ssl connection daemon. 之后,您必须在IE证书列表中插入该证书,以使其与您的apache ssl连接守护程序一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM