Esempio n. 1
0
def testSingleTx():
    if os.path.exists('DSA_pkey.txt') == True and os.path.exists(
            'DSA_skey.txt') == True:
        skeyFile = open('DSA_skey.txt', 'r')
        q = int(skeyFile.readline())
        p = int(skeyFile.readline())
        g = int(skeyFile.readline())
        alpha = int(skeyFile.readline())
        skeyFile.close()
        print("Public key is read from DSA_skey.txt")

        pkeyFile = open('DSA_pkey.txt', 'r')
        lines = pkeyFile.readlines()
        beta = int(lines[3])
        pkeyFile.close()
        print("Public key is read from DSA_pkey.txt")

    else:
        print('DSA_skey.txt or DSA_pkey.txt does not exist')
        sys.exit()

    # pick a random message (string)
    TxFile = open("SingleTransaction.txt", "w")
    lines = TxFile.readlines()
    m = lines[0:9]
    print("message: ", m)

    r = int(lines[9][15:])
    s = int(lines[10][15:])
    print("Signature:")
    print("r: ", r)
    print("s: ", s)

    if DSA.SignVer(m, r, s, p, q, g, beta) == 1:
        print("Signature verifies:))")
    else:
        print("Signature does not verify:((")
        sys.exit()
Esempio n. 2
0
        sys.exit()
    
    TxBlockFile = open(TxBlockFileName, "r")
    lines = TxBlockFile.readlines()
    TxBlockFile.close()

    # read the transaction txNo from the file and verify its signature
    transaction = lines[txNo*TxLen:(txNo+1)*TxLen]
    SignedPart = "".join(transaction[0:TxLen-2])
    p = int(transaction[2][3:])
    q = int(transaction[3][3:])
    g = int(transaction[4][3:])
    beta = int(transaction[5][25:])
    r = int(transaction[8][15:])
    s = int(transaction[9][15:])
    if DSA.SignVer(SignedPart, r, s, p, q, g, beta)==1:
        print "The signature of the transaction verifies:))"
    else:
        print "The signature of the transaction does not verify:(("

    # Check if the transaction really belongs to that block
    # using "LongestChain.txt file"
    # The method is hash tree
    BlockChainFileName = "LongestChain.txt"
    if os.path.exists(BlockChainFileName) == False:
        print "Error: ", BlockChainFileName, "does not exist"
        sys.exit()
    BlockChainFile = open(BlockChainFileName, "r")
    blocks = BlockChainFile.readlines()

    # read the root hash from the BlockChainFileName file
            pkeyFile.close()
            print("Public key is read from DSA_pkey.txt")

        else:
            print('DSA_skey.txt or DSA_pkey.txt does not exist')
            sys.exit()

    # pick a random message (string)
    m = random_string(random.randint(1, 100))
    print("message: ", m)
    (r, s) = DSA.SignGen(m, p, q, g, alpha, beta)
    print("Signature:")
    print("r: ", r)
    print("s: ", s)

    if DSA.SignVer(m, r, s, p, q, g, beta) == 1:
        print("Signature verifies:))")
    else:
        print("Signature does not verify:((")
        sys.exit()

# Generate a random transaction along with its signature
if TxGenOn:
    if KeyGenOn == 0:
        if os.path.exists('DSA_pkey.txt') == True and os.path.exists(
                'DSA_skey.txt') == True:
            skeyFile = open('DSA_skey.txt', 'r')
            q = int(skeyFile.readline())
            p = int(skeyFile.readline())
            g = int(skeyFile.readline())
            alpha = int(skeyFile.readline())