在 python 3.6 中使用 pycryptodome 或密码学。如何做到这一点?
Using pycryptodome or cryptography in python 3.6. How to achieve this?
想象以下消息:
"This is a message to the signed"
我需要使用 Python 3.6 中的 "pycryptodome" 或 "cryptography" 和以下标准来签署此示例消息:
- 格式:x.509;
- 字符集:UTF-8;
- 编码:Base64;
- PKCS1 v1.5;
- 大小:1024 位;
- 消息格式:SHA-1;
我有所需的 "privatekey.pem" 但我不知道如何在 pycryptodome 或密码学中做到这一点。
更新:
我找到了这个示例代码,但仍然不知道它是否是根据原始消息中定义的标准实现我需要的正确方法。示例代码(用于pycryptodome):
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA1
import base64
from base64 import b64encode, b64decode
key = open('privatekey.pem', "r").read()
rsakey = RSA.importKey(key)
signer = PKCS1_v1_5.new(rsakey)
digest = SHA1.new()
data = 'This the message to be signed'
digest.update(b64decode(data))
sign = signer.sign(digest)
doc = base64.b64encode(sign)
print(doc)
我可以看到我得到了一个 172 个字符的签名哈希,但需要专业建议才能知道这是否符合我描述的标准以及这样做是否正确。
这是一个代码片段,改编自 applicable documentation page:
from Crypto.Signature import pkcs1_15
from Crypto.Hash import SHA1
from Crypto.PublicKey import RSA
from base64 import b64encode
message = 'This the message to be signed'
key = RSA.import_key(open('private_key.der').read())
h = SHA1.new(message)
signature = pkcs1_15.new(key).sign(h)
print(b64encode(signature))
想象以下消息:
"This is a message to the signed"
我需要使用 Python 3.6 中的 "pycryptodome" 或 "cryptography" 和以下标准来签署此示例消息:
- 格式:x.509;
- 字符集:UTF-8;
- 编码:Base64;
- PKCS1 v1.5;
- 大小:1024 位;
- 消息格式:SHA-1;
我有所需的 "privatekey.pem" 但我不知道如何在 pycryptodome 或密码学中做到这一点。
更新: 我找到了这个示例代码,但仍然不知道它是否是根据原始消息中定义的标准实现我需要的正确方法。示例代码(用于pycryptodome):
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA1
import base64
from base64 import b64encode, b64decode
key = open('privatekey.pem', "r").read()
rsakey = RSA.importKey(key)
signer = PKCS1_v1_5.new(rsakey)
digest = SHA1.new()
data = 'This the message to be signed'
digest.update(b64decode(data))
sign = signer.sign(digest)
doc = base64.b64encode(sign)
print(doc)
我可以看到我得到了一个 172 个字符的签名哈希,但需要专业建议才能知道这是否符合我描述的标准以及这样做是否正确。
这是一个代码片段,改编自 applicable documentation page:
from Crypto.Signature import pkcs1_15
from Crypto.Hash import SHA1
from Crypto.PublicKey import RSA
from base64 import b64encode
message = 'This the message to be signed'
key = RSA.import_key(open('private_key.der').read())
h = SHA1.new(message)
signature = pkcs1_15.new(key).sign(h)
print(b64encode(signature))