2

I tried to verify the signature of a file by DSA encryption. I am using Pyton 3.6 and pycryptodomex version 3.4.7. Unhappily the documentation code seems to be outdated (was trying to get a simple example working):

 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"

this is my code, tried to fix the function calls which were not working:

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

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

# Load the public key

pub_key = DSA.import_key(key.publickey().exportKey())
verifyer = DSS.new(pub_key, 'fips-186-3')


hash_obj = SHA256.new(message)


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

Can someone help me with this topic?

flo_type
  • 21
  • 2

1 Answers1

0

works like that

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

# Create a new DSA key
key = DSA.generate(2048)
f = open("public_key.pem", "w")
f.write(base64.b64encode(key.publickey().export_key()).decode("utf-8") )
f.close()

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

# Load the public key
f = open("public_key.pem", "r")
hash_obj = SHA256.new(message)
pub_key = DSA.import_key(base64.b64decode(f.read()))
verifier = DSS.new(pub_key, 'fips-186-3')

# Verify the authenticity of the message
try:
    verifier.verify(hash_obj, signature)
    print("The message is authentic.")
except ValueError:
    print("The message is not authentic.")