Python 3.x 使用Pycryptodome签名和验证签名总是失败

Python 3.x 使用Pycryptodome签名和验证签名总是失败,python-3.x,encryption,cryptography,rsa,pycryptodome,Python 3.x,Encryption,Cryptography,Rsa,Pycryptodome,嗨,我正在使用Pycryptodome包尝试验证区块链中交易的签名。我的问题是,当尝试添加一个新事务时,我首先创建一个要传递到验证事务方法中的签名,但由于某种原因,它总是失败,即使当我将其与文档进行比较时,逻辑似乎是正确的。如果有人能告诉我哪里出了问题,我将不胜感激。我有3种方法可以处理所有这些问题,我不确定问题出在哪里 生成密钥方法 def generate_keys(self): # generate private key pair private_key

嗨,我正在使用Pycryptodome包尝试验证区块链中交易的签名。我的问题是,当尝试添加一个新事务时,我首先创建一个要传递到验证事务方法中的签名,但由于某种原因,它总是失败,即使当我将其与文档进行比较时,逻辑似乎是正确的。如果有人能告诉我哪里出了问题,我将不胜感激。我有3种方法可以处理所有这些问题,我不确定问题出在哪里

生成密钥方法

def generate_keys(self):
        # generate private key pair
        private_key = RSA.generate(1024, Crypto.Random.new().read)

        # public key comes as part of private key generation
        public_key = private_key.publickey()

        # return keys as hexidecimal representation of binary data
        return (binascii.hexlify(public_key.exportKey(format='DER')).decode('ascii'), binascii.hexlify(private_key.exportKey(format='DER')).decode('ascii'))
签名事务处理方法

def sign_transaction(self, sender, recipient, amount, key):

        # convert transaction data to SHA256 string
        hash_signer = SHA256.new(
            (str(sender) + str(recipient) + str(amount)).encode('utf-8'))

        # sign transaction
        signature = pkcs1_15.new(RSA.importKey(
            binascii.unhexlify(key))).sign(hash_signer)


        # return hexidecimal representation of signature
        return binascii.hexlify(signature).decode('ascii')
@staticmethod
def verify_transaction(transaction):
        # convert public key back to binary representation
        public_key = RSA.importKey(binascii.unhexlify(
            transaction.sender))

        try:
            # create signature from transaction data
            hash_signer = SHA256.new(
                (str(transaction.sender) + str(transaction.recipient) + str(transaction.amount)).encode('utf-8'))


            pkcs1_15.new(public_key).verify(
                hash_signer, binascii.unhexlify(transaction.signature))


            return True
        except ValueError:
            return False

以及验证事务方法

def sign_transaction(self, sender, recipient, amount, key):

        # convert transaction data to SHA256 string
        hash_signer = SHA256.new(
            (str(sender) + str(recipient) + str(amount)).encode('utf-8'))

        # sign transaction
        signature = pkcs1_15.new(RSA.importKey(
            binascii.unhexlify(key))).sign(hash_signer)


        # return hexidecimal representation of signature
        return binascii.hexlify(signature).decode('ascii')
@staticmethod
def verify_transaction(transaction):
        # convert public key back to binary representation
        public_key = RSA.importKey(binascii.unhexlify(
            transaction.sender))

        try:
            # create signature from transaction data
            hash_signer = SHA256.new(
                (str(transaction.sender) + str(transaction.recipient) + str(transaction.amount)).encode('utf-8'))


            pkcs1_15.new(public_key).verify(
                hash_signer, binascii.unhexlify(transaction.signature))


            return True
        except ValueError:
            return False


一旦我生成了我的密钥对并尝试使用它们来签名和验证事务,它总是失败。我知道这一点,因为它总是从verify方法返回false,这使我相信总是会引发值错误。提前谢谢,希望有人能帮我。

我不能重复这个问题。签名和验证时,
sender
真的包含公钥吗?是的,sender包含公钥,所以你说它对你有效?是的,我使用了可以执行的公钥,例如。