0

I am trying to do a P2MS script. For my script, I am saving the keys into a text file with DER format instead of the usual PEM file. Both key and signatures are saved in a text file and hexlify. Below is my code for the P2MS execution.

from Crypto.PublicKey import DSA
from Crypto.Hash import SHA256
from Crypto.Signature import DSS
from binascii import hexlify, unhexlify
import binascii

message = b"helpsmepls"

# Read scriptPubKey and scriptSig from files
with open('scriptPubKey.txt', 'r') as f:
    readscriptPubKey = f.read().strip()
with open('scriptSig.txt', 'r') as f:
    scriptSig = f.read().strip()

print(type(readscriptPubKey))
tempholder = readscriptPubKey.split()

# Removing the first character and last character
removeend = tempholder[1:-1]

scriptPubKey = []
# Removing front extra headings
for count, x in enumerate(removeend):
    w = bytes(removeend[count][1:-1], encoding = 'utf-8')
    #print(w)
    scriptPubKey.append(w)

# Splitting the Signatures
signatures = scriptSig.split()
hash_obj = SHA256.new(message)

# Going through the pubkeys based on the number of signatures generated
for o, sig in enumerate(signatures): 
    pub_key = DSA.import_key(bytes.fromhex(scriptPubKey[o].decode("utf-8")))
    hash_obj = SHA256.new(message)
    verifier = DSS.new(pub_key, 'fips-183-3')
    # Verifying if the Public key and signatures match, loop will break if False is encountered
    if verifier.verify(hash_obj, sig):
        d = True
    else: 
         d = False
         break
    break

if d == True:
    print("The message is authentic.")
else: print("The message is not authentic.")

Unfortunately before my code can reach the verification, it encountered an error. Full traceback

It seems my DSA key format has an error, but I am not too sure why is it giving me that error. I have also tried unhexlifying my input from the public key text file, but it also did not work. I have tried to hex decode the input to get the DER format of the input, but my type is still just bytes. I am not so sure how to properly import the key with the appropriate DSA key format from a txt file. I am able to do that with a PEM file but would just like to find out how to execute it with a txt file.

My expected outcome is the DSA key is imported properly and i am able to verify the public key with the signatures.

0 Answers0