If statement skips first condition. Trying to follow 1-4 Steps

时间:2015-06-25 19:13:14

标签: if-statement conditional-statements

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: 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. 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. Step 3: The CLA sends the list of validation numbers to the CTF using a symmetric key negotiated between CLA and CTF. 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 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'

2 个答案:

答案 0 :(得分:0)

从查看代码看起来您​​正在将decrypt_id与文件中包含的信息进行比较,其中不仅包含decrypt_id数据。因此,if语句永远不会被执行,因为数据不一样。

答案 1 :(得分:0)

当您使用file.read()时,您将阅读该文件的全部内容,您之前使用str(encdata)decrpyt_idstr(encdata_2)撰写了该文件。为了便于解释,我们假设encdata为1,decrypt_id为2,而encdata_2为3. verify_vote将是一个包含“123”的字符串 - 因为那是什么你写了 - 而decrypt_id将是一个包含“2”的字符串。 if语句将评估为true的唯一情况是encdataencdata_2都是空字符串。

编辑:再看一下这段代码,看起来if语句的目的是阻止用户多次投票。您可能要考虑做的是创建一个decrypt_ids列表,您每次验证选民的ID时都会添加该列表,然后创建if语句if decrypt_id not in verified_list:

因此,if块将成为

if decrypt_id not in verified_list:
    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)
    verified_list.append(decrypt_id)
else:
    print '\n'
    print 'You have already voted! Thank you!;
    print '\n'

另外需要注意的是,您打开cla.txt进行阅读但从未关闭它。您应该在file.close()之后放置verify_vote = file.read()