Example #1
0
    def load(self, key_path, password: str = None):

        self.path = key_path

        input_raw_bytes = read_file(self.path)
        input_bytes = input_raw_bytes

        self.md5 = hashlib.md5(input_bytes)

        if password is not None:
            illegal, msg = Key.is_password_illegal(password)
            if illegal:
                raise InvalidKeyException(msg)
            password = self.pad_key(password)
            blank_password = self.pad_key('')
            # 整个文件用空密码解密
            input_bytes = decrypt_data(blank_password, input_raw_bytes)

        buffer = BytesIO(input_bytes)

        with zipfile.ZipFile(buffer) as mem_zipfile:
            self.id = mem_zipfile.read('id').decode('utf-8')
            self.name = mem_zipfile.read('name').decode('utf-8')
            self._key = mem_zipfile.read('key')
            if password is not None:
                password_b = self.sha_passwd(password)
                if mem_zipfile.read('passwd').hex() != password_b.hex():
                    raise InvalidPasswordException('密码错误')
            if password is not None:
                self._key = decrypt_data(password, self._key)

        self._update_last_access()
        self._timeout = False
Example #2
0
def load_encrypt_image(path: str) -> QPixmap:

    try:
        raw_data = read_file(path)

        image_data = raw_data

        if is_encrypt_data(raw_data):
            image_data = decrypt_data(cache.get_cur_key().key, raw_data)

        pixmap = QPixmap()

        pixmap.loadFromData(image_data)
    except Exception as e:
        print(e)

    return pixmap, len(image_data)
Example #3
0
    def do_it(self):
        if not self.check_input():
            return
        pd = QProgressDialog(self)
        pd.setMinimumDuration(10)
        pd.setAutoClose(True)
        pd.setAutoReset(False)
        pd.setLabelText('正在处理')
        pd.setCancelButtonText('取消')
        pd.setRange(0, len(self.file_list))
        pd.setValue(0)
        pd.setWindowModality(Qt.WindowModal)
        pd.show()

        try:
            for index, file_path in enumerate(self.file_list):

                if pd.wasCanceled():
                    break

                pd.setLabelText('正在处理 {}/{}'.format(str(index + 1),
                                                    len(self.file_list)))

                if os.path.exists(file_path):

                    p, n = os.path.split(file_path)
                    file_content = read_file(file_path)

                    if self.before_process(file_content):
                        encrypt_content = self.processor(
                            self.key.key, file_content)
                        output_file = os.path.join(self.output_dir_path, n)
                        write_file(output_file, encrypt_content)

                pd.setValue(pd.value() + 1)

            if not pd.wasCanceled():
                QMessageBox.information(pd, '处理完成', self.success_msg)
            else:
                QMessageBox.information(pd, '已经终止', '用户取消')
        except Exception as e:
            pass
            QMessageBox.critical(pd, '处理失败', str(e))
        pd.close()
        self.close()
Example #4
0
def open_encrypt_files_dialog(key: Key,
                              progress_title='正在处理',
                              caption_files='选择需要加密的文件',
                              caption_output_dir='选择输出目录',
                              file_filter='All Files (*);;Text Files (*.txt)',
                              parent=None):
    file_list, _ = QFileDialog.getOpenFileNames(parent,
                                                caption=caption_files,
                                                filter=file_filter)

    if len(file_list) == 0:
        return

    dir_path = QFileDialog.getExistingDirectory(None, caption_output_dir,
                                                "C:/")

    if dir_path == '' or not os.path.exists(dir_path):
        return

    pd = QProgressDialog(parent)
    pd.setMinimumDuration(1000)
    pd.setAutoClose(True)
    pd.setAutoReset(False)
    pd.setLabelText('正在处理')
    pd.setCancelButtonText('取消')
    pd.setRange(0, len(file_list))
    pd.setValue(0)
    pd.setWindowModality(Qt.WindowModal)
    pd.show()

    try:
        for index, file_path in enumerate(file_list):

            if pd.wasCanceled():
                break

            pd.setLabelText('正在处理 {}/{}'.format(str(index + 1),
                                                len(file_list)))

            if os.path.exists(file_path):

                p, n = os.path.split(file_path)
                file_content = read_file(file_path)

                if not is_encrypt_data(file_content):
                    encrypt_content = encrypt_data(key.key, file_content)
                    output_file = os.path.join(dir_path, n)
                    write_file(output_file, encrypt_content)

            pd.setValue(pd.value() + 1)

        if not pd.wasCanceled():
            QMessageBox.information(pd, '处理完成', '加密完成')
        else:
            QMessageBox.information(pd, '已经终止', '用户取消')

    except Exception as e:
        pass
        QMessageBox.critical(pd, '处理失败', str(e))

    pd.close()
Example #5
0
 def _load_md5(self):
     key_md5_file_path = self.get_md5_file_path()
     if not os.path.exists(key_md5_file_path):
         return None
     return read_file(key_md5_file_path)