-
Notifications
You must be signed in to change notification settings - Fork 0
/
tally.py
executable file
·81 lines (61 loc) · 2.08 KB
/
tally.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/python
import socket
import hashlib
from Crypto.Cipher import AES
from Crypto.PublicKey import RSA
from Crypto import Random
from Crypto.Random.random import getrandbits
import registrar
import sys
from charm.toolbox.integergroup import RSAGroup
from charm.schemes.pkenc import pkenc_paillier99
from charm.schemes.pkenc.pkenc_paillier99 import Ciphertext
from charm.toolbox.conversion import Conversion
from charm.core.math import integer as specialInt
from charm.toolbox.integergroup import integer
from charm.core.engine.util import objectToBytes,bytesToObject
def tally_vote():
#initialization
group = RSAGroup()
pai = pkenc_paillier99.Pai99(group)
#get voting public key
f=open('./pyssss/VotingPublic','rb')
data = f.read()
public_key = bytesToObject(data,group)
n=public_key['n']
#get voting private key
f=open('./pyssss/VotingPrivate','rb')
data = f.read()
secret_key = bytesToObject(data,group)
#get ciphervotes file
f=open('./CipherVotes','r')
data=f.readlines()
count = 0
#open file for publishing results
f=open('./PublishedResults','w')
#go through all recorded ciphervotes
print '#, ciphervote, plainvote'
for vote in data:
serializedVote = vote.strip()
ciphervote = specialInt.deserialize(serializedVote)
ciphervote = Ciphertext({'c':ciphervote},public_key,'c')
if count == 0:
ciphertotal = ciphervote
else:
ciphertotal=ciphertotal+ciphervote
#publish
print str(count)+','+str(ciphervote['c'])+','+ str(pai.decrypt(public_key,secret_key,ciphervote))
f.write(str(count)+','+str(ciphervote['c'])+','+ str(pai.decrypt(public_key,secret_key,ciphervote))+'\n' )
count = count +1
print 'Our calculated total:', pai.decrypt(public_key,secret_key,ciphertotal)
#get ciphervotes file
f=open('./CipherVotesTotal','r')
data=f.read()
data=data.strip()
ciphervotetotal = specialInt.deserialize(data)
ciphervotetotal = Ciphertext({'c':ciphervotetotal},public_key,'c')
ciphervotetotal = pai.decrypt(public_key,secret_key,ciphervotetotal)
print 'Their calculated total:',ciphervotetotal
return
if __name__ == "__main__":
tally_vote()