def receive_vote(self, message, voter_pub_key, as_pub_key): token, encrypted_selected_candidate, encrypted_i_code, signed_selected_candidate, \ signed_i_code = message v_candidate = verify(encrypted_selected_candidate, signed_selected_candidate, voter_pub_key) v_i_code = verify(encrypted_i_code, signed_i_code, voter_pub_key) if v_i_code and v_candidate: candidate = decrypt(encrypted_selected_candidate, self.key_pair) i_code = decrypt(encrypted_i_code, self.key_pair) verify_t = verify(i_code, token, as_pub_key) if verify_t: if hash(i_code) not in self.voters: self.voters.append(hash(i_code)) self.candidates[candidate] += 1
def response_to_pub_prv_key_request(self, message): decrypted_message = str(decrypt(message=message, key_pair=self.key_pair)) voter_identifications = decrypted_message.split(", ") # considering national code and certificate num mach! i_code = str(voter_identifications[0])[2:] c_num = str(voter_identifications[1])[:-1] if i_code not in self.key_pair_dict.keys(): key = RSA.generate(1024) self.key_pair_dict[hash(i_code)] = ( {"national_code": hash(i_code), "certificate_num": hash(c_num), "key_pair": key}) self.pub_keys[hash(i_code)] = key.publickey()
def response_to_authentication_request_part2(self, message): encrypted_message, signature = message v = verify(encrypted_message, signature, self.pub_keys[hash("AS")]) if v: decrypted_msg = decrypt(encrypted_message, self.key_pair) identifications = decrypted_msg.split(b", ") # identifications = [b"b'002-036-135", b"123-a'"] c_num = str(identifications[1])[2:-2] i_code = str(identifications[0])[4: -1] if hash(i_code) in self.key_pair_dict.keys(): data = self.key_pair_dict[hash(i_code)] if data["certificate_num"] == hash(c_num): my_message =(data["key_pair"].exportKey()) my_encrypted_messages = encrypt_multi_packet(my_message, self.pub_keys[hash("AS")]) my_signatures = sign_multi_packet(my_encrypted_messages, self.key_pair) second_message = i_code encrypted_second_message = encrypt(second_message, self.pub_keys[hash("AS")]) signed_second_message = sign(encrypted_second_message, self.key_pair) return (my_encrypted_messages, my_signatures, encrypted_second_message, signed_second_message) return None
def response_to_authentication_request_part3(self, messages_signatures, ca_pub_key): encrypted_messages, signatures, encrypted_national_code, signed_national_code = messages_signatures v1 = verify_multi_packet(encrypted_messages, signatures, ca_pub_key) v2 = verify(encrypted_national_code, signed_national_code, ca_pub_key) v = v1 and v2 if v: msg = decrypt_multi_packet(encrypted_messages, self.key_pair) national_code = decrypt(encrypted_national_code, self.key_pair) national_code_binary = national_code national_code = str(national_code_binary)[2: -1] secret_key = self.symmetric_keys[national_code] key = RSA.importKey(msg) voter_pub_key = RSA.importKey(key.publickey().exportKey()) T = (sign(national_code_binary, self.key_pair)) T = encrypt_multi_packet(T, voter_pub_key) padding_character = "{" encrypted_T = [encrypt_message_binary((part_t), secret_key) for part_t in T] encrypted_i_code = encrypt_message(national_code, secret_key, padding_character) encrypted_pair_key = encrypt_message(str(key.exportKey()), secret_key, padding_character) sending_messages = [encrypted_i_code, encrypted_pair_key, encrypted_T] signed_encrypted_T = [sign(encrypted_part_T, self.key_pair) for encrypted_part_T in encrypted_T] signed_encrypted_i_code = sign(encrypted_i_code, self.key_pair) signed_encrypted_pair_key = sign(encrypted_pair_key, self.key_pair) signs = [signed_encrypted_i_code, signed_encrypted_pair_key, signed_encrypted_T] return (sending_messages, signs)
def response_to_authentication_request_part1(self, message, ca_pub_key): decrypted_message = str(decrypt(message=message, key_pair=self.key_pair)) encrypted_message = encrypt(decrypted_message, ca_pub_key) signature = sign(encrypted_message, self.key_pair) return [encrypted_message, signature]