def backup(self): try: selected_index = self.treeViewStorage.selectedIndexes()[0] selected_path = self.model_storage.filePath(selected_index).split( '/')[::-1] storage = '' for i, dir in enumerate(selected_path): if dir == "Backups": storage = str(selected_path[i - 1]) dialog = QInputDialog() dialog.setModal(True) key, ok = dialog.getText(self, "Key", "Enter key for <b>%s</b> storage" % storage, mode=QLineEdit.Password) if ok: #try: key = str(key) with open(self.current_dir + storage + sep + "init", 'rb') as f: hash_from_storage = f.read(64) cipter_text = f.read() encryptor = AES.new(key + self.padding[len(key):]) plain = encryptor.decrypt(cipter_text) if hash_from_storage == SHA256.new(plain).hexdigest(): self.password = key self.encrypt_and_save_to(storage) else: QMessageBox.about(self, "Error", "Incorrect password!") #except UnicodeError: # QMessageBox.about(self, "Unicode error", "Incorrect password!") except IndexError: if self.selected_storage: with open( self.current_dir + self.selected_storage + sep + "init", 'rb') as f: hash_from_storage = f.read(64) cipter_text = f.read() encryptor = AES.new(self.password + self.padding[len(self.password):]) plain = encryptor.decrypt(cipter_text) if hash_from_storage == SHA256.new(plain).hexdigest(): self.encrypt_and_save_to(self.selected_storage) else: QMessageBox.about(self, "Error", "Incorrect password! sel") else: QMessageBox.about( self, "Error", "Select the storage before creating backup!") self.tabWidgetRoot.setCurrentWidget(self.tab_storage)