Python加载.pem公钥

问题描述 投票:0回答:2

我有一个 ERCA pem 文件,需要将其作为公钥加载,然后用于解密一些数据。

------------ BEGIN ERCA PK ---------------
/UVDIAD//wHpgHY6REqVJQqVh4LR1UrPwyPSXzlGuBbpL8+dMrQqJhPRo2O05DUyoCZoYynI
lmPMwAH3J4IGtqtlrShxhIpoD2pX2P2h14LJtYEpA+pbZuKpvh2FvdD9rnakYIjXGmF2sfap
hBkQBCTcVtCEaqPIQ5DTUXoPEZLe3/dAkkzbpwAAAAAAAQAB
------------ END ERCA PK ---------------

现在我基本都是这样做的

    file = open(".\dddfiles\EC_PK\EC_PK.pem", 'rb')
    b = file.read()
    file.close()
    rsa.PublicKey.load_pkcs1_openssl_der(b)

但它给了我错误:

File "C:\Users\Oscar\Documents\QanALL_CMD_Tools\src\ddd_parser.py", line 97, in <module>
    _7601_.validate()
  File "C:\Users\Oscar\Documents\QanALL_CMD_Tools\src\parser7601.py", line 89, in validate
    rsa.PublicKey.load_pkcs1_openssl_der(b)
  File "C:\Users\Oscar\AppData\Roaming\Python\Python311\site-packages\rsa\key.py", line 375, in load_pkcs1_openssl_der
    (keyinfo, _) = decoder.decode(keyfile, asn1Spec=OpenSSLPubKey())
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Oscar\AppData\Roaming\Python\Python311\site-packages\pyasn1\codec\ber\decoder.py", line 1618, in __call__
    raise error.PyAsn1Error(
pyasn1.error.PyAsn1Error: <TagSet object, tags 0:32:13> not in asn1Spec: <OpenSSLPubKey schema object, tagSet=<TagSet object, tags 0:32:16>, subtypeSpec=<ConstraintsIntersection object>, componentType=<NamedTypes object, types <NamedType object, type header=<PubKeyHeader schema object, tagSet=<TagSet object, tags 0:32:16>, subtypeSpec=<ConstraintsIntersection object>, componentType=<NamedTypes object, types <NamedType object, type oid=<ObjectIdentifier schema object, tagSet <TagSet object, tags 0:0:6>>>, <NamedType object, type parameters=<Null schema object, tagSet <TagSet object, tags 0:0:5>, subtypeSpec <ConstraintsIntersection object, consts <SingleValueConstraint object, consts b''>>, encoding iso-8859-1>>>, sizeSpec=<ConstraintsIntersection object>>>, <NamedType object, type key=<OctetString schema object, tagSet <TagSet object, tags 0:0:3>, encoding iso-8859-1>>>, sizeSpec=<ConstraintsIntersection object>>

为了我的生活,我尝试了很多,但无法正常工作,有人知道吗?我还有一个 BIN 和 txt 文件。两者都试过了,没有区别。

给出一些上下文:我正在尝试编写代码来验证来自行驶记录仪文件的签名。每章都有用于创建数据签名的证书。如果我理解文档是正确的,我需要使用欧洲公钥解开章节的证书,之后我从制造商那里得到一个新的公钥,然后我可以用它来验证整个章节的签名。但我对这一切可能是错误的,因为文档似乎是由阿尔伯特爱因斯坦撰写的,而我远非他的智慧,或与此相关的任何智慧。

python encryption rsa public-key-encryption
2个回答
0
投票

是pem密钥,不能直接读成文本文件 在这里试试这个,不要忘记安装必要的软件包。

file = ".\dddfiles\EC_PK\EC_PK.pem"

rsa.PublicKey.load_pkcs1_openssl_der(file)

0
投票

您可以在here(来自here),第 4.3.1 章中找到 ERCA 公钥格式的描述:Base64 解码数据以标头 0xFD45432000FFFF01 开头,后跟 128 字节模数 (n)。最后 8 个字节包含公共指数 (e)。都按大端顺序排列。

然后可以使用 Python-RSA 库(您似乎使用它)进行导入,如下所示:

import rsa
import base64

publicErca = b'''------------ BEGIN ERCA PK ---------------
/UVDIAD//wHpgHY6REqVJQqVh4LR1UrPwyPSXzlGuBbpL8+dMrQqJhPRo2O05DUyoCZoYynI
lmPMwAH3J4IGtqtlrShxhIpoD2pX2P2h14LJtYEpA+pbZuKpvh2FvdD9rnakYIjXGmF2sfap
hBkQBCTcVtCEaqPIQ5DTUXoPEZLe3/dAkkzbpwAAAAAAAQAB
------------ END ERCA PK ---------------'''

publicErcaBytes = base64.b64decode(publicErca.replace(b'------------ BEGIN ERCA PK ---------------', b'').replace(b'------------ END ERCA PK ---------------', b'').replace(b'\n', b''))

# Extract n, e
n = int.from_bytes(publicErcaBytes[8:8+128], 'big')
e = int.from_bytes(publicErcaBytes[8+128:], 'big')

# Import key
pubKey = rsa.PublicKey(**{'e': e, 'n': n})

# Do what you want to do, e.g. export as PEM encoded key in PKCS#1 format
print(pubKey.save_pkcs1().decode())

请注意,更现代的 Python 库也可以从 n 和 e 重建公钥(例如,通过

RSA.construct()
的 PyCryptodome 或通过
rsa.RSAPublicNumbers()
的 pyca/cryptography)。

© www.soinside.com 2019 - 2024. All rights reserved.