上文介紹了命令列方式來對檔案進行加解密操作,本文將繼續在此基礎上,實作一個快速簡易的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相比,也無法與B/S架構的前端相比,通常情況下,本系列不再介紹GUI編程方面的知識,但是這個工具包非常簡潔明快,推薦使用,windows下使用以下命令完成相關環境準備,
pip install Gooey
ubuntu 18.04環境有些復雜,建議以下安裝步驟,
sudo apt-get install python-wxtools # method1 apt-get install pkg-config # method2 wget http://launchpadlibrarian.net/477064124/pkg-config_0.29.2-1ubuntu1_amd64.deb dpkg -i pkg-config_0.29.2-1ubuntu1_amd64.deb pkg-config --version apt-get install -y libgtk-3-dev # 檢驗與設定 pkg-config --cflags --libs gtk+-3.0 # 前面全是準備 pip install wxpython pip install matplotlib pip install gooey
安裝成功后,也可以獲得與windows下類似的界面,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/535985.html
標籤:其他
下一篇:資料庫平滑擴容方案剖析
