在过去,当使用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的密码,但是它似乎没有足够的特性。
执行导出的任何文档或搜索条件都将是有帮助的。
编辑2
Maarten Bodewes的评论(谢谢)把我带到了一个我正在寻找的地方。但出口的结果不同:
# 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的一种努力--请注意,这两个示例使用不同的Py3版本。编码在这里会成为一个问题吗?
发布于 2019-02-06 08:50:44
回答我的问题(这个问题在评论中提供的帮助下解决了,再次感谢)。
为了实现我能够使用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"https://stackoverflow.com/questions/54495255
复制相似问题