jeudi 25 juin 2015

If statement skips first condition

I am trying to make a virtual voting booth that accepts the user id encrypts it, verifies and validates voter id, then checks if the voter has already voted. But it skips the first condition. I'm not an expert programmer. I'm new and just learning python. I need your help! Thank you in advance for your expertise!

These are the steps:

  1. Step 1: Voter generates a pair of private and public keys – for the purpose of digital signature. Voter uses his private key to sign his request and the public key of the CLA to send his/her message. The message must include the voter’s signed id, say, SSN (request) and the voter’s public key. The public keys of the CLA and CTF may be passed to a voter when he/she starts the session.

  2. Step 2: CLA reads the message sent on Step 1 using its private key and, using the voter’ public key, finds the voter’s id . CLA encrypts the validation number using its private key and sends it to the voter. The voter finds the validation number, using the public key of the CLA.

  3. Step 3: The CLA sends the list of validation numbers to the CTF using a symmetric key negotiated between CLA and CTF.

  4. Step 4: The message from the user to the CTF must be encrypted using the public key of CTF.

The result show this:

C:\Python27\python.exe "C:/Python Projects/Virtual Election Booth/VEB.py"

Welcome to the Vitrual Election Booth.

Enter your voter id:1234

You have already voted! Thank you!

Process finished with exit code 0

Here is the code below:

from collections import Counter
import random
from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA
from Crypto import Random
random_generator = Random.new().read
key = RSA.generate(1024, random_generator) #generate pub and priv key
pubkey = key.publickey() # pub key export for exchange

vote_id = raw_input( '''\nWelcome to the Vitrual Election Booth.\n
Enter your voter id:''')#input from user
hash = SHA256.new(vote_id).digest() #creates vote id hash
encdata = pubkey.encrypt(vote_id, 32) #creates encrypted pubkey of hash
signature = key.sign(hash, pubkey)#signs the id and pubkey

assert pubkey.verify(hash, signature)
assert not pubkey.verify(hash[:-1], signature)

m = random.randint(4, 1000)
rand_val_num = random.randint(1, m)
encdata_2 = pubkey.encrypt(rand_val_num, 32)

file = open('cla.txt', 'w') #opens cla file (as a server)
file.write(str(encdata))#writes hash to cla file (saved vote_id hash to server)
decrypt_id = key.decrypt(encdata)
file.write(decrypt_id)
file.write(str(encdata_2))
file.close() #closes file


file = open('cla.txt', 'r')
verify_vote = file.read()#assign verify_vote to read file

if decrypt_id == verify_vote:
     print '\n'
     print 'Your voter id is verified!'
     print '\n'
     Vote = raw_input('Please place your vote: ')
     file = open('ctf.txt', 'a') #open cla file (as a server)
     file.write(str(publickey)) #writes publickey to cla file
     file.write('\n')
     file.write(Vote)
     file.write('\n')
     file.close() #closes file
else:
     print('\n')
     print 'You have already voted! Thank you!'
     print '\n'

Aucun commentaire:

Enregistrer un commentaire