Python Crypto:验证非对称事务是否为有效签名返回false?

问题描述

我正在尝试使用Crypto库验证不对称加密,如下所示


__author__ = 'Umesh'

import logging

import Crypto
import Crypto.Random
from Crypto.PublicKey import RSA
import binascii

from collections import OrderedDict
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA256

from Crypto.Signature.pkcs1_15 import PKCS115_SigScheme

logging.basicConfig(level=logging.DEBUG)



class Transaction(object):
    
    def __init__(self):

        self.senderpublicKey = ''
        self.transaction_amount = ''
        self.senderPrivatekey = ''
        self.signature = ''
        self.reciever_public_key=''
        self.transaction = []

    """
    Create the private key and public key for the user
    """

    def get_private_public_key(self):
        random_gen = Crypto.Random.new().read
        private_key = RSA.generate(1024,random_gen)
        public_key = private_key.publickey()
        self.senderpublicKey = public_key  # binascii.hexlify(public_key.export_key(format('DER'))).decode('ascii')
        self.senderPrivatekey = private_key  # binascii.hexlify(private_key.export_key(format('DER'))).decode('ascii')
        self.reciever_public_key=public_key
        print("Sender Private Key",self.senderPrivatekey)

    def to_dict(self):
        return OrderedDict({
            'sender_public_key': self.senderpublicKey,'reciever_public_key': self.reciever_public_key,'transaction_amount': self.transaction_amount
        })

    def sign_signature(self):
        #private_key = RSA.importKey(self.senderPrivatekey)
        signer = PKCS115_SigScheme(self.senderPrivatekey)
        hash = SHA256.new(str(self.to_dict()).encode('utf8'))
        print("Hash while signed",str(hash))
        self.signature = signer.sign(hash)

    def send_signed_trnasaction(self):
        self.transaction_amount = '123'
        response = {
            'Amount': self.transaction_amount,'sender_public_key': self.senderpublicKey,'recipient_public_key': self.reciever_public_key,'Signature': self.signature
        }
        return response


# print(send_signed_trnasaction())


class Verify_Transacton(Transaction):
    transaction_Obj = Transaction()
    ##Invoke the private_key
    transaction_Obj.get_private_public_key()

    ##Invoke the sign_signature Function
    transaction_Obj.sign_signature()

    ##Printt the signed transacton
    print("call transaction ",transaction_Obj.send_signed_trnasaction())

    def verifySignature(self):
        print("verify method is called")
        recieved_Transaction = self.transaction_Obj.send_signed_trnasaction()
        transaction = OrderedDict({
            'sender_public_key': recieved_Transaction['sender_public_key'],'reciever_public_key': recieved_Transaction['recipient_public_key'],'transaction_amount': recieved_Transaction['Amount']
        })

        public_key = recieved_Transaction['sender_public_key']
        
        verifier = PKCS115_SigScheme(public_key)
        
        hash = SHA256.new(str(transaction).encode('utf8'))
        print("Hash after recieved",str(hash))

        # Verify the transaciton against the signature
        try:

            status = verifier.verify(hash,recieved_Transaction['Signature'])

            print("Status is",status)
            return status

        except ValueError:
            return False


verify_trans_obj = Verify_Transacton()
print(verify_trans_obj.verifySignature())



在这里,即使验证参数(例如pub key和hash)与发件人相同,结果也始终返回“ false”。

我一直在寻找这种用例的解决方案,但没有找到任何可行的方法。 赞赏是否有人可以对此提供帮助? 谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)