def Create_New_Format(self, path, UID_List, rbac, second_UID_List = None, second_rbac = None): """receives the file path, a list of uids allowed to do what the rbac specifies, and a rbac attaches the header and uses Encryption's function to encrypt the data""" users_rbac = [] if self.user_uid not in UID_List: UID_List.append(str(self.user_uid)) file_path_list = path.split(".") old_extension = file_path_list[1] new_path = file_path_list[0] + ".cb" if second_rbac is None and second_UID_List is None: optional_header_flag = 0 else: optional_header_flag = 1 file_header = FileHeaderStruct(MAGIC_NUMBER, int(FILE_TYPE_CODE_DICTIONARY[old_extension]), int(rbac), int(optional_header_flag), len(UID_List)) new_file = open(new_path, "wb") new_file.write(file_header) fileid = int(self.get_key_or_id()) new_file.write(struct.pack('i', fileid)) for uid in UID_List: new_file.write(struct.pack('i', int(uid))) if optional_header_flag == 1: addition_to_file_header = OptionalHeaderStructAdditions(second_rbac, len(second_UID_List)) new_file.write(addition_to_file_header) for uid in second_UID_List: new_file.write(struct.pack('i', int(uid))) users_rbac = [(rbac, UID_List), (second_rbac, second_UID_List)] else: users_rbac = [(rbac, UID_List)] aux_obj = AUXGenerator() aux = aux_obj.hash_generate(users_rbac) original_key = self.get_key_or_id() + self.get_key_or_id() MyCrypto.encrypt_file(original_key, aux, path) temp_obj = open("temp.txt", "rb") encrypted_content = temp_obj.read() temp_obj.close() new_file.write(encrypted_content) new_file.close() os.chmod(path, stat.S_IWRITE) os.remove(path) os.remove("temp.txt") file_data = (fileid, original_key) return file_data
def Strip_File(self, path, original_key): try: encrypted_file = open(path, "rb") file_header = FileHeaderStruct() encrypted_file.readinto(file_header) dict_1 = {} for ext, index in FILE_TYPE_CODE_DICTIONARY.items(): dict_1[index] = ext file_path_list = path.split(".") new_path = file_path_list[0] + "." + dict_1[file_header.fileTypeCode] file_id = struct.unpack('<L', encrypted_file.read(4)) print file_id[0] UID_List = [] for i in xrange(file_header.lenUIDS): uid_s = encrypted_file.read(4) UID_List.append(struct.unpack('<L', uid_s)[0]) first_rbac_users = (file_header.rBac, UID_List) if file_header.optionalHeaderFlag: file_optional_header = OptionalHeaderStructAdditions() encrypted_file.readinto(file_optional_header) second_UID_List = [] for i in xrange(file_optional_header.secondLenUIDS): uid_s = encrypted_file.read(4) second_UID_List.append(struct.unpack('<L', uid_s)[0]) second_rbac_users = (file_optional_header.secondRbac, second_UID_List) try: users_rbac = [first_rbac_users, second_rbac_users] """users_rbac = [(1, [12345678,23456789]), (0, [23544445, 87342914])]""" except: users_rbac = [first_rbac_users] encrypted_file_content = encrypted_file.read() encrypted_file.close() temp_obj = open("temp2.txt", "wb") temp_obj.write(encrypted_file_content) temp_obj.close() aux_obj = AUXGenerator() aux = aux_obj.hash_generate(users_rbac) validated = MyCrypto.validate(self.user_uid, users_rbac) if validated: MyCrypto.decrypt_file(original_key, aux, "temp2.txt") temp_obj = open("temp.txt", "rb") decrypted_content = temp_obj.read() temp_obj.close() new_file = open(new_path, "wb") new_file.write(decrypted_content) new_file.close() os.remove(path) os.remove("temp2.txt") os.remove("temp.txt") if first_rbac_users[0] == 1: os.chmod(new_path, stat.S_IREAD) return "File unlocked, user can only read the file" return "File unlocked" else: return "The specified user is not allowed to open the file " except IOError: "Could not strip the file" except IOError: "Could not strip the file"