2015-06-25 76 views
0

我想讓虛擬投票間接受用戶ID加密它,驗證和驗證投票者ID,然後檢查投票者是否已投票。但它跳過了第一個條件。我不是專家程序員。我是新來的,只是學習python。我需要你的幫助!預先感謝您的專業知識!如果語句跳過第一個條件。試圖遵循1-4步驟

這些步驟如下:

  1. 步驟1:選民產生一對私鑰和公鑰的 - 用於數字簽名的目的。選民使用他的私鑰在他的請求和CLA的公共密鑰上簽名以發送他/她的消息。該消息必須包括選民的簽名ID,比如SSN(請求)和選民的公鑰。當他/她開始會議時,CLA和CTF的公鑰可能會被傳遞給選民。

  2. 步驟2:CLA使用其私鑰讀取步驟1上發送的消息,並使用投票人的公鑰查找投票人的ID。 CLA使用其私鑰對驗證號碼進行加密並將其發送給選民。選民使用CLA的公鑰找到驗證號碼。

  3. 步驟3:CLA使用在CLA和CTF之間協商的對稱密鑰將驗證號碼列表發送給CTF。

  4. 步驟4:用戶向CTF發送的消息必須使用CTF的公鑰加密。

結果顯示如下:

C:\ Python27 \ python.exe 「C:/ Python項目/虛擬展臺選舉/ VEB.py」

歡迎Vitrual選舉展臺。

輸入您的投票人編號:1234

您已投票!謝謝!

過程完成,退出代碼爲0

這裏是下面的代碼:

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' 

回答

0

從看它看起來像您正在使用的文件中的所有內容比較decrypt_id代碼其中不僅包含decrypt_id數據。因此,if語句將永遠不會執行,因爲數據不一樣。

0

當您讀取file.read()時,您會讀取之前使用str(encdata),decrpyt_idstr(encdata_2)編寫的文件的全部內容。爲了說明起見,假設encdata爲1,decrypt_id爲2,並且encdata_2爲3. verify_vote將是包含「123」的字符串 - 因爲這就是您寫的 - 而decrypt_id將是包含「2」的字符串。 if語句將評估爲真的唯一情況是encdataencdata_2都是空字符串。

編輯:仔細看看這段代碼,它看起來像if語句的目的是防止用戶多次投票。你可能要考慮做的是創造您添加的每個選民的ID驗證時間,然後使decrypt_ids列表中的if語句if decrypt_id not in verified_list:

因此,如果塊將成爲

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閱讀,但從來沒有關閉它。你應該在verify_vote = file.read()之後放置一個file.close()

+0

感謝您的幫助,我會嘗試列表。 – Cyber