示例#1
0
def readMKFromFile(mkPath='mk'):
    file = open(mkPath, 'rb')
    MK = {}
    MK['pp'] = deserialize(file.readline())
    MK['qq'] = deserialize(file.readline())
    file.close()
    return MK
示例#2
0
 def unserialize(cls, params, data):
     sk = json.loads(data)
     return SecretKey(params,
         LongTermSecretKey.unserialize(params, sk['lts']),
         ShortTermSecretKey.unserialize(params, sk['sts']),
         deserialize(sk['k_c']),
         deserialize(sk['s']))
示例#3
0
    def unserialize(cls, data):
        sig = json.loads(data)

        r = deserialize(sig['r'])
        s = deserialize(sig['s'])

        return SchnorrSignature(r, s)
示例#4
0
def readParamFromFile(paramPath='param'):
    file = open(paramPath, 'rb')
    param = Param()
    param.N2 = deserialize(file.readline())
    param.N = deserialize(file.readline())
    param.k = deserialize(file.readline())
    param.g = deserialize(file.readline())
    file.close()
    return param
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
示例#6
0
    def unserialize(cls, data):
        params = json.loads(data)
        p = deserialize(params['p'])
        q = deserialize(params['q'])
        r = int(params['r'])
        g = deserialize(params['g'])
        num_bits = int(params['num_bits'])

        # TODO: assert num_bits is 'correctish' w.r.t. p/q/g
        ret = Params(p, q, g, num_bits)
        ret.r = r
        return ret
示例#7
0
 def unserialize(cls, params, data):
     pk = json.loads(data)
     return PublicKey(params,
         LongTermPublicKey.unserialize(params, pk['ltp']),
         ShortTermPublicKey.unserialize(params, pk['stp']),
         deserialize(pk['r']),
         LongTermPublicKey.unserialize(params, pk['kgcPk']))
示例#8
0
    def read(self, recordType):
        if recordType.lower() == "general":
            ID = self.General[0]
        elif recordType.lower() == "medical":
            ID = self.Medical[0]
        elif recordType.lower() == "training":
            ID = self.Training[0]
        else:
            print("Please enter the correct record type")
            return

        # 1. Read MySql Database to obtain string object
        # 2. Re-construct Ciphertext by converting it to a byte object, then call Charm's deSerialisation API
        # 3. Pass reconstructed ciphertext to dec() function to get plaintext
        #####################
        #MD: Todo: Add date checking
        #####################
        db = Database()
        rows = db.selectRecord(ID) # Now fetch the ciphertexts and verify the signatures and print the result
        for row in rows :
            ctI_bytes = bytes(row[0], 'utf-8')              # Integer element of CT
            ctI_Reconstruct = deserialize(ctI_bytes)
            ctPg_bytes = bytes(row[1], 'utf-8')             # PairingGroup element of CT
            ctReconstruct = bytesToObject(ctPg_bytes, self.group)
            ctReconstruct['C']['C'] = ctI_Reconstruct       # Complete Ciphertext from Integer and Pairing Group element
            pt = self.dec(recordType, ctReconstruct) # Decrypt the Ciphertext
            signerID = row[2] # get the id of the signer
            sig_bytes = bytes(row[3], 'utf-8')
            signature = bytesToObject(sig_bytes, self.signGroup) # Got the actual signature
            signdate = row[4]

            if self.verifySig(signerID, signdate, pt, signature):
                # Signature is valid, now check if entity was authorised at this date
                # Dont check our own data since we know it's valid if the signature checks out (we are always allowed to write to our own HealthRecord)
                if signerID == self.ID:
                    print("Verified record from ", signerID, ": ", pt, "\n")
                else:
                    rows = db.getAuthorisedEntities(self.ID, recordType, signdate)
                    if rows:
                        for row in rows:
                            if signerID == row[0]:
                                print("Verified record from ", signerID, ": ", pt, "\n")
                            else:
                                print("INVALID record from ", signerID, ": ", pt, "\n")
                    else:
                        #There were no authorisations for this date
                        print("INVALID record from ", signerID, ": ", pt, "\n")
            else:
                print("INVALID signature from ", signerID, ": ", pt, "\n")

        db.done()
示例#9
0
def b64decode(d):
    if isinstance(d, list):
        return [b64decode(a) for a in d]
    if isinstance(d, dict):
        return {
            k: b64decode(v) if k not in [
                "cmd",
                "status",
                "msg",
                "name",
            ] else v
            for k, v in d.items()
        }
    return deserialize(base64.b64decode(d))
def get_vote():
	vote = 2

        group = RSAGroup()
        pai = pkenc_paillier99.Pai99(group)

        f=open('./pyssss/VotingPublic','rb')
        data = f.read()
        public_key = bytesToObject(data,group)

        vote = pai.encode(public_key['n'],vote)
        ciphervote = pai.encrypt(public_key,vote)
        print ciphervote['c']
	ciphertext= specialInt.serialize(ciphervote['c'])
	print ciphertext	
#	ciphervote['c'] = Conversion.bytes2integer(Conversion.IP2OS(7092577248503691820499926901328776752053429606540181306976))

        f=open('./pyssss/VotingPrivate','rb')
        data = f.read()
        secret_key = bytesToObject(data,group)
	ciphertext = specialInt.deserialize(ciphertext)
        #plaintext=pai.decrypt(public_key, secret_key,ciphervote)
	n, n2 = public_key['n'], public_key['n2']
       	plaintext = ((pai.L(ciphertext ** secret_key['lamda'],n) % n) * secret_key['u']) %n 

	print plaintext










        ciphervote=42
        return ciphervote
示例#11
0
 def deserialize(self, bytes_object):
     assert type(bytes_object) == bytes, "cannot deserialize object"
     return deserialize(bytes_object)
示例#12
0
def readKeyPairFromFile(keyPairPath='keyPair'):
    file = open(keyPairPath, 'rb')
    pk = deserialize(file.readline())
    sk = deserialize(file.readline())
    file.close()
    return pk, sk
示例#13
0
    def unserialize(cls, params, data):
        d = json.loads(data)
        x = deserialize(d['x'])

        return LongTermSecretKey(params, x)
示例#14
0
    def unserialize(cls, params, data):
        d = json.loads(data)
        t = deserialize(d['t'])

        return ShortTermSecretKey(params, t)
示例#15
0
    def unserialize(cls, params, data):
        pk = json.loads(data)
        u = deserialize(pk['u'])

        return ShortTermPublicKey(params, u)
示例#16
0
 def deserialize(self, bytes_object):
     assert type(bytes_object) == bytes, "cannot deserialize object"
     return deserialize(bytes_object)
示例#17
0
    def unserialize(cls, params, data):
        pk = json.loads(data)
        y = deserialize(pk['y'])

        return LongTermPublicKey(params, y)