设为首页 加入收藏

TOP

Python工具箱系列(十四)(一)
2023-07-25 21:24:59 】 浏览:59
Tags:Python 十四

上文介绍了命令行方式来对文件进行加解密操作。本文将继续在此基础上,实现一个快速简易的GUI界面方便操作,先上代码看效果。

import argparse
import configparser
import json
import os
import struct
import sys
from configparser import ConfigParser
from pathlib import Path

from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
from gooey import Gooey, GooeyParser
from matplotlib import widgets

defaultsize = 64*1024
# 密钥随便写,使用时只使用前16字节
key = 'stayhungrystayfoolish'
realkey = key[:16].encode('utf-8')


def encrypt_file(key, in_filename, out_filename=None, chunksize=defaultsize):
    """
    对文件进行加密

    Args:
        key (str): 16字节密钥
        in_filename (str): 待加密文件
        out_filename (str, optional): 加密后输出的文件
        chunksize (int, optional): 块大小,缺省64k
    """
    if not out_filename:
        out_filename = in_filename + '.enc'
    iv = os.urandom(16)
    encryptor = AES.new(key, AES.MODE_CBC, iv)
    filesize = os.path.getsize(in_filename)
    with open(in_filename, 'rb') as infile:
        with open(out_filename, 'wb') as outfile:
            outfile.write(struct.pack('<Q', filesize))
            outfile.write(iv)
            pos = 0
            while pos < filesize:
                chunk = infile.read(chunksize)
                pos += len(chunk)
                if pos == filesize:
                    chunk = pad(chunk, AES.block_size)
                outfile.write(encryptor.encrypt(chunk))


def decrypt_file(key, in_filename, out_filename=None, chunksize=defaultsize):
    """
    解密文件

    Args:
        key (str): 16字节密钥
        in_filename (str): 待解密文件
        out_filename (str, optional): 解密后输出的文件
        chunksize (int, optional): 块大小,缺省64K
    """
    if not out_filename:
        out_filename = in_filename + '.dec'
    with open(in_filename, 'rb') as infile:
        filesize = struct.unpack('<Q', infile.read(8))[0]
        iv = infile.read(16)
        encryptor = AES.new(key, AES.MODE_CBC, iv)
        with open(out_filename, 'wb') as outfile:
            encrypted_filesize = os.path.getsize(in_filename)
            pos = 8 + 16  # the filesize and IV.
            while pos < encrypted_filesize:
                chunk = infile.read(chunksize)
                pos += len(chunk)
                chunk = encryptor.decrypt(chunk)
                if pos == encrypted_filesize:
                    chunk = unpad(chunk, AES.block_size)
                outfile.write(chunk)


@Gooey(language='chinese')
def parse():

    parser = GooeyParser(description=u'aes handler')
    parser.add_argument("-d", "--decry", action="store_true",
                        help="解密模式")
    parser.add_argument("-e", "--encry", action="store_true",
                        help="加密模式")
    parser.add_argument("-i", "--input", type=str,
                        help="要处理的文件", widget='FileChooser')
    parser.add_argument("-o", "--output", type=str,
                        help="要输出的文件", widget='FileSaver')

    args = parser.parse_args()
    print(args)
    # 判断参数输入情况,如果没有参数,则显示帮助。
    if len(sys.argv) == 1:
        parser.print_help()
        return

    # 解密模式,获得输入与输出文件后,调用算法解密
    if args.decry:
        inputfilename = Path(args.input)
        if inputfilename.exists():
            decrypt_file(realkey, in_filename=args.input,
                         out_filename=args.output)
        else:
            print(f'{args.input}不存在')

    # 加密模式,获得输入与输出文件后,调用算法加密
    if args.encry:
        inputfilename = Path(args.input)
        if inputfilename.exists():
            encrypt_file(realkey, in_filename=args.input,
                         out_filename=args.output)
        else:
            print(f'{args.input}不存在')


parse()

可以看出,基本上代码不用太改,直接就可以升级为GUI应用,确实非常方便,运行效果如下图所示。

要知道,python的GUI实在是令人感到一言难尽,不仅需要配置的东西很多,而且还经常出现各类错误,根本无法与微软的.Net相比,

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇python中的公共操作和推导式 下一篇同步与异步 multiprocessing 进程..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目