我想讓虛擬投票間接受用戶ID加密它,驗證和驗證投票者ID,然後檢查投票者是否已投票。但它跳過了第一個條件。我不是專家程序員。我是新來的,只是學習python。我需要你的幫助!預先感謝您的專業知識!如果語句跳過第一個條件。試圖遵循1-4步驟
這些步驟如下:
步驟1:選民產生一對私鑰和公鑰的 - 用於數字簽名的目的。選民使用他的私鑰在他的請求和CLA的公共密鑰上簽名以發送他/她的消息。該消息必須包括選民的簽名ID,比如SSN(請求)和選民的公鑰。當他/她開始會議時,CLA和CTF的公鑰可能會被傳遞給選民。
步驟2:CLA使用其私鑰讀取步驟1上發送的消息,並使用投票人的公鑰查找投票人的ID。 CLA使用其私鑰對驗證號碼進行加密並將其發送給選民。選民使用CLA的公鑰找到驗證號碼。
步驟3:CLA使用在CLA和CTF之間協商的對稱密鑰將驗證號碼列表發送給CTF。
步驟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'
感謝您的幫助,我會嘗試列表。 – Cyber