1

当我在 python 中尝试一些加密库 rsa 签名时,我注意到在pycryptodome和之间生成的签名cryptography是不同的。这是我的代码:

pycryptodome

import base64
from Crypto.Hash import SHA1
from Crypto.PublicKey import RSA
from Crypto.Signature import pkcs1_15

# Getting data to sign.
plain_path = input('File to sign-> ')
plain = None

with open(plain_path, 'rb') as f:
  plain = f.read()

# Calculating data hash.
digest = SHA1.new()
digest.update(plain)

# Loading private key.
private_key_path = input('Private key path-> ')
private_key = None

with open(private_key_path, 'rb') as f:
  private_key = RSA.import_key(f.read())

# Signing data.
signer = pkcs1_15.new(private_key)
signature = signer.sign(digest)

pass

cryptography

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives.asymmetric import utils

# Getting data to sign.
plain_path = input('File to sign-> ')
plain = None

with open(plain_path, 'rb') as f:
  plain = f.read()

# Loading private key.
private_key_path = input('Private key path-> ')
private_key = None

with open(private_key_path, 'rb') as f:
  private_key = serialization.load_pem_private_key(f.read(), None, default_backend())

# Signing data.
signature = private_key.sign(plain, padding.PKCS1v15(), utils.Prehashed(hashes.SHA1()))

pass

结果(见signature变量内容):

pycryptodome
密码学

但是,我的项目的正确结果是cryptography一个,但我的问题是为什么它们不同?我在使用非等效代码吗?

谢谢。

4

0 回答 0