Python 将PEM文件转换为DER

Python 将PEM文件转换为DER,python,cryptography,tor,Python,Cryptography,Tor,我目前正在尝试编写一个脚本,允许我从hiddens服务的私钥文件计算Tor HS地址。 为此,需要将文件转换为DER格式 使用OpenSSL可以通过以下方式完成此操作: openssl rsa -in private_key -pubout -outform DER 通过以下方式将其导入python: base64.b32encode(hashlib.sha1(sys.stdin.read()[22:]).digest()[:10]).lower()' 将正确返回地址 但是,我希望仅使用py

我目前正在尝试编写一个脚本,允许我从hiddens服务的私钥文件计算Tor HS地址。 为此,需要将文件转换为DER格式

使用OpenSSL可以通过以下方式完成此操作:

openssl rsa -in private_key -pubout -outform DER
通过以下方式将其导入python:

base64.b32encode(hashlib.sha1(sys.stdin.read()[22:]).digest()[:10]).lower()'
将正确返回地址

但是,我希望仅使用python执行相同的操作。我的问题是,使用pycrypto模块时,DER输出不同,因此地址不正确

key = RSA.importKey(keyfile.read()).publickey()
print(key.exportKey(format='DER'))
将导致与openssl调用不同的输出。 这仅仅是一个允许不同结果的实现问题吗?还是我在某个地方犯了错误

如有任何帮助,请使用python将证书转换为der 首先我们加载文件

cert\u file=keyfile.read()
然后我们将其转换为pem格式

来自OpenSSL导入加密
cert\u pem=crypto.load\u证书(crypto.FILETYPE\u pem,cert\u文件)
现在我们正在生成der输出
i、 e.:
输出等于openssl x509-outform der-in certificate.pem-out certificate.der。

cert\u der=crypto.dump\u证书(crypto.FILETYPE\u ASN1,cert\u pem)

我正在寻找类似的东西,截至2019年3月,OpenSSL建议使用而不是模块。()

下面是您打算做的:将PEM转换为DER

从cryptography.hazmat.backends导入默认\u后端
从cryptography.hazmat.primitives导入序列化
以open(“id_rsa”、“rb”)作为密钥文件:
#加载PEM格式键
pemkey=serialization.load\u pem\u private\u key(
keyfile.read(),
没有一个
默认_backend()
)
#将其序列化为DER格式
derkey=pemkey.private_字节(
序列化.Encoding.DER,
serialization.PrivateFormat.TraditionalOpenSSL,
序列化.NoEncryption()
)
#并将DER格式写入文件
以open(“key.der”、“wb”)作为输出文件:
outfile.write(derkey)
最初的问题是:“从私钥中提取公钥”,这是因为openSSL命令在最初的问题中表示“pubout”

使用OpenSSL可以做到这一点:(注意,“pubout”仅将输出定义为公钥)

但是使用Python加密模块,您可以从私钥中提取公钥(注意,这似乎适用于基于RSA和EC的加密)

使用Python:

from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.backends import default_backend

    # Create private key (example uses elliptic curve encryption)

    priv_key = ec.generate_private_key(ec.SECP256K1, default_backend())

    pub_key = priv_key.public_key()

    pub_key_pem = pub_key.public_bytes(
        encoding=serialization.Encoding.PEM,
        format=serialization.PublicFormat.SubjectPublicKeyInfo
        )

    with open('public_key.pem', 'wb') as outfile:
        outfile.write(public_key_pem)

关于加密文档的更多信息:

我想要转换证书文件,而不是从DER到PEM的密钥文件,但是Google把我带到了这里。感谢@alleen1的回答,我可以将证书或密钥从DER转换为PEM,反之亦然

第一步,加载文件

第二步,将其保存为所需格式

我建议您使用获取“pem_数据”和“der_数据”的过程,您可以从文件或其他任何地方获取它。它们应该是字节而不是字符串,需要时使用method.encode()

from cryptography import x509
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.backends import default_backend

# Step one, load the file. 

# Load key file
# PEM 
key = serialization.load_pem_private_key(pem_data, None, default_backend())
# DER
key = serialization.load_pem_private_key(der_data, None, default_backend())

# Load cert file
# PEM
cert = x509.load_pem_x509_certificate(pem_data, default_backend())
# DER
cert = x509.load_der_x509_certificate(der_data, default_backend())

# Step two,save it to the format you want.
# PEM key
key_val = key.private_bytes(
              serialization.Encoding.PEM,
              serialization.PrivateFormat.TraditionalOpenSSL,
              serialization.NoEncryption()
          )
# DER key
key_val = key.private_bytes(
              serialization.Encoding.DER,
              serialization.PrivateFormat.TraditionalOpenSSL,
              serialization.NoEncryption()
          )

# PEM cert
cert_val = cert.public_bytes(serialization.Encoding.PEM)
# DER cert
cert_val = cert.public_bytes(serialization.Encoding.DER)

在最近的一个项目中,我注意到对于某些格式,
exportKey
方法不是很可靠。我不得不重新实现它的某些部分。使用它时要小心。你能用十六进制来举例说明DER输出吗?PEM输入也可能有用。我在两个不同的答案之间进行猜测。这是一个很有帮助的回答。我实现了相同的功能,但是使用了公钥。对于公钥,AmazonEC2就是这样计算MD5的。
from cryptography import x509
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.backends import default_backend

# Step one, load the file. 

# Load key file
# PEM 
key = serialization.load_pem_private_key(pem_data, None, default_backend())
# DER
key = serialization.load_pem_private_key(der_data, None, default_backend())

# Load cert file
# PEM
cert = x509.load_pem_x509_certificate(pem_data, default_backend())
# DER
cert = x509.load_der_x509_certificate(der_data, default_backend())

# Step two,save it to the format you want.
# PEM key
key_val = key.private_bytes(
              serialization.Encoding.PEM,
              serialization.PrivateFormat.TraditionalOpenSSL,
              serialization.NoEncryption()
          )
# DER key
key_val = key.private_bytes(
              serialization.Encoding.DER,
              serialization.PrivateFormat.TraditionalOpenSSL,
              serialization.NoEncryption()
          )

# PEM cert
cert_val = cert.public_bytes(serialization.Encoding.PEM)
# DER cert
cert_val = cert.public_bytes(serialization.Encoding.DER)