Python Pycrypto无法不一致地验证从文件加载的签名

Python Pycrypto无法不一致地验证从文件加载的签名,python,rsa,pycrypto,Python,Rsa,Pycrypto,我正在尝试程序签名,然后稍后验证文件的内容。然而,如果第一次验证总是返回true,那么一旦数据写入文件并再次加载,验证通常会失败,但有时会成功 即使代码失败,两个print signature和print hash.hexdigest()调用的输出在视觉上也是相同的 我的测试代码是: from Crypto.Hash import SHA256 from Crypto.PublicKey import RSA from Crypto.Signature import PKCS1_PSS def

我正在尝试程序签名,然后稍后验证文件的内容。然而,如果第一次验证总是返回true,那么一旦数据写入文件并再次加载,验证通常会失败,但有时会成功

即使代码失败,两个
print signature
print hash.hexdigest()
调用的输出在视觉上也是相同的

我的测试代码是:

from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_PSS
def generate():
    key_file = open("TestPrivateKey")
    private_key = RSA.importKey(key_file)
    public_key = private_key.publickey()

    seed_file = open("Seed")

    plaintext = seed_file.read()

    hash = SHA256.new(plaintext)
    signer = PKCS1_PSS.new(private_key)
    signature =  signer.sign(hash)

    plaintext_file = open("plaintext", 'w')
    plaintext_file.write(plaintext)
    signature_file = open("signature", 'w')
    signature_file.write(signature)
    print signature
    print hash.hexdigest()

    verifier = PKCS1_PSS.new(public_key)

    print verifier.verify(hash, signature)

def verification_test():
    plaintext_file = open("plaintext")
    signature_file = open("signature", 'rb')

    plaintext = plaintext_file.read()
    public_key = RSA.importKey(open("TestPublicKey"))
    signature = signature_file.read()
    print signature

    hash = SHA256.new(plaintext)
    print hash.hexdigest()

    verifier = PKCS1_PSS.new(public_key)
    return verifier.verify(hash, signature)


if __name__ == '__main__':
    generate()
    print verification_test()
有人知道我犯了什么错误吗?当签名被写入文件,然后被读回时,肯定发生了什么事情,但我不知道它是什么

编辑:在运行此脚本之前,我运行初始化函数:

from Crypto.PublicKey import RSA

def create_keys():
    private_key = RSA.generate(4096)
    file = open("TestPrivateKey", 'w')
    file.write(private_key.exportKey())
    file = open("TestPublicKey", 'w')
    file.write(private_key.publickey().exportKey())

def create_seed():
    file = open("Seed", 'w')
    file.write("Test")

我注意到你的代码有两个问题

首先,您正在将任意二进制数据写入为文本打开的文件:

signature_file = open("signature", 'w')  #bad
signature_file.write(signature)
应该是:

signature_file = open("signature", 'wb')  #good
signature_file.write(signature)
第二,你从不关闭你的文件。试试这个:

with open("signature", 'wb') as signature_file:
    signature_file.write(signature)
对于所有其他打开的文件也是如此



我注意到您已经包含了几乎所有的完整工作程序。我希望,如果您再添加两到三行,您将有一个完整的工作示例。请发布一个完整的工作程序,演示您的错误。有关更多信息,请参阅。抱歉,它已经告诉我代码太多。我将添加其余的代码,但它只是用垃圾填充
种子
文件,并生成和存储密钥。到目前为止,您粘贴的内容仍然无法运行。你有任何
import
语句吗?哦,是的,我从PyCrypto导入了必要的模块。好的,我将其更改为打开二进制文件,现在它可以工作了。我最初是以字符串的形式写和读,但是阅读器将字符串中的一些字符解释为EOF,所以我将读取更改为二进制,但我没有更改写入。我还用语句添加了。
from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_PSS
def generate():
    with open("TestPrivateKey") as key_file:
        private_key = RSA.importKey(key_file)
    public_key = private_key.publickey()

    with open("Seed") as seed_file:
        plaintext = seed_file.read()

    hash = SHA256.new(plaintext)
    signer = PKCS1_PSS.new(private_key)
    signature =  signer.sign(hash)

    with open("plaintext", 'w') as plaintext_file:
        plaintext_file.write(plaintext)
    with open("signature", 'wb') as signature_file:
        signature_file.write(signature)
    #print signature
    print hash.hexdigest()

    verifier = PKCS1_PSS.new(public_key)

    print verifier.verify(hash, signature)

def verification_test():
    with open("plaintext") as plaintext_file:
        plaintext = plaintext_file.read()
    with open("signature", 'rb') as signature_file:
        signature = signature_file.read()

    with open("TestPublicKey") as public_key_file:
        public_key = RSA.importKey(public_key_file)
    #print signature

    hash = SHA256.new(plaintext)
    print hash.hexdigest()

    verifier = PKCS1_PSS.new(public_key)
    return verifier.verify(hash, signature)


if __name__ == '__main__':
    generate()
    print verification_test()