Python:Cryptodomex无法使用DSA验证

时间:2017-11-10 13:11:00

标签: python cryptography pycrypto pycryptodome

我尝试通过DSA加密验证文件的签名。我正在使用Pyton 3.6和pycryptodomex版本3.4.7。 不幸的是,文档代码似乎已经过时了(试图让一个简单的例子工作):

<button id="myBtn" onclick="fillForm()">Try it</button>
<form id="myForm">
  <fieldset>
    <input type="text" id="myInput1" onchange="myFunction(this)" onfocus="focusIt(this)">
  </fieldset>
</form>

这是我的代码,试图修复无效的函数调用:

 from Crypto.PublicKey import DSA
 from Crypto.Signature import DSS
 from Crypto.Hash import SHA256

 # Create a new DSA key
 key = DSA.generate(2048)
 f = open("public_key.pem", "w")
 f.write(key.publickey().exportKey(key))

 # Sign a message
 message = b"Hello"
 hash_obj = SHA256.new(message)
 signer = DSS.new(key, 'fips-186-3')
 signature = key.sign(hash_obj)

 # Load the public key
 f = open("public_key.pem", "r")
 hash_obj = SHA256.new(message)
 pub_key = DSA.import_key(f.read())

 # Verify the authenticity of the message
 if pub_key.verify(hash_obj, signature):
     print "OK"
 else:
     print "Incorrect signature"

有人可以帮我解决这个问题吗?

0 个答案:

没有答案