【问题标题】:Python Cryptography export key to DERPython Cryptography 导出密钥到 DER
【发布时间】:2019-06-26 23:03:28
【问题描述】:

在过去使用 PyCrypto 时,我能够执行以下操作来生成 RSA 公钥的指纹:

rsa_cipher = PKCS1_v1_5.new(RSA.importKey(pub_rsa_key))
hashlib.sha1(rsa_cipher._key.exportKey("DER")).hexdigest()

如果没有 PyCrypto,我怎样才能达到同样的效果?


编辑

我在pub_rsa_key 中提供的是.perm 文件的内容,即:

-----BEGIN PUBLIC KEY-----
MII...AB
-----END PUBLIC KEY-----

PyCrypto 被认为不安全且不再维护,因此我切换到 Python 的 Cryptography,但它似乎没有足够的功能。

  • Pythons Cryptography API 中是否有我遗漏的类似功能?
  • PyCryptoDome 是否可能是 PyCrypto 的一个值得(稳定且安全)的替代品,用于实现此功能?
  • 如果以上都不是,是否可以通过自写函数以 DER 格式导出该密钥?

执行导出的任何文档或搜索词都会有所帮助。


编辑 2
Maarten Bodewes 的 cmets(谢谢)带我去了一个似乎正是我要找的地方。但是 DER 导出的结果不同:

# Python 3.7 using Cryptography
from cryptography.hazmat.primitives import serialization

with open('pub_key.perm', 'rb') as key_file: 
    public_key = serialization.load_pem_public_key(key_file.read(), backend=default_backend())

pub_der = public_key.public_bytes(encoding=serialization.Encoding.DER, format=serialization.PublicFormat.PKCS1)

print(sha1(pub_der).hexdigest())
# gives "d291c142648b7........c2f4676f4213203c4bd"

在哪里

# Python 2.7 using PyCrypto
from Crypto.Cipher import PKCS1_v1_5
from Crypto.PublicKey import RSA

with open('pub_key.perm', 'r') as key_file:
    public_key = RSA.importKey(key_file.read())

pub_der = public_key.exportKey('DER')  # this assumes PKCS1 by default per the __doc__

print(sha1(pub_der).hexdigest())
# gives "bb070664079f5........64c97fcadbad847cce9"

这是从 Py2 迁移到 Py3 的努力 - 请注意这两个示例使用不同的 Python 版本。这里的编码可能是个问题吗?

【问题讨论】:

  • 谢谢,我希望我现在提供了足够(或至少一些)有价值的信息。
  • 好的,很酷,这是一个很好的问题,因为它现在已经格式化了。我执行了一些格式化以将实际问题捆绑在一起等。如果您不同意,请随意展开。很抱歉第一个尖刻的言论;确实感觉我们不得不无缘无故地欺骗现有的免费库。
  • 我会说loading the public key using the Cryptography API,然后将其序列化到内存中的DER(正下方),这样您就可以散列密钥最有意义。它使用 OpenSSL 作为后端,PEM 是 OpenSSL 的主要格式(因此不太可能出现兼容性问题)。
  • 呃,你当然会使用load_pem_public_key :)
  • 您的公钥具有 SubjectPublicKeyInfo 格式或 X509 格式,而不是 PKCS#1 格式;实际上,PKCS#1 表单是inside SubjectPublicKeyInfo。 PEM 在 PKCS#1 格式的破折号之间使用“BEGIN RSA PUBLIC KEY”(因为 PKSC#1 没有指定算法,而 SubjectPublicKeyInfo 有)。

标签: python cryptography rsa pycrypto python-cryptography


【解决方案1】:

回答我的问题(在 cmets 提供的帮助下已解决,再次感谢)。

为了实现我能够使用 PyCrypto 做的事情:

# Python 2.7 using PyCrypto
from Crypto.Cipher import PKCS1_v1_5
from Crypto.PublicKey import RSA

with open('pub_key.perm', 'r') as key_file:
    public_key = RSA.importKey(key_file.read())

pub_der = public_key.exportKey('DER')  # this assumes PKCS1 by default per the __doc__

print(sha1(pub_der).hexdigest())
# gives "bb070664079f5........64c97fcadbad847cce9"

使用密码学,可以做到以下几点:

# Python 3.7 using Cryptography
from cryptography.hazmat.primitives import serialization

with open('pub_key.perm', 'rb') as key_file: 
    public_key = serialization.load_pem_public_key(key_file.read(), backend=default_backend())

pub_der = public_key.public_bytes(
    encoding=serialization.Encoding.DER,
    format=serialization.PublicFormat.SubjectPublicKeyInfo,
)

print(sha1(pub_der).hexdigest())
# gives "bb070664079f5........64c97fcadbad847cce9"

【讨论】:

    猜你喜欢
    • 2020-04-08
    • 2017-01-31
    • 1970-01-01
    • 2019-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多