浏览器运行环境异常,请检查是否开启本站的JavaScript权限或下载最新版浏览器
简单文件加密工具

简单文件加密工具

代码来自GitHub,我对其进行了一定的优化和修改,可用于百度网盘防炸。

主程序代码:

import sys
from tkinter import *
from tkinter import filedialog
from tkinter import ttk
import tkinter.ttk
import threading
from binascii import a2b_hex
from binascii import b2a_hex
import tkinter.messagebox
import os
import shutil
import tkinter.scrolledtext
import multiprocessing
from multiprocessing import Queue
from multiprocessing import Process
import base64
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes

from i import img


def read(dir,address,length,f):
    f.seek(address, 0)
    result = f.read(length)
    result = b2a_hex(result)
    result = bytes.decode(result)
    return result
def write(write_bytes,dir,address,f):
    try:
        write_bytes = bytes().fromhex(write_bytes)
    except:
        print(write_bytes)
    f.seek(address, 0)
    result=f.write(write_bytes)
    return result
def realcode1(dir,mima,mima_len,flagmi):
    def jiamihanshu(mima, readdata, iv):
        secret = mima  # 由用户输入的16位或24位或32位长的初始密码字符串

        # 密钥处理,16的整数倍
        def add_to_16(text):
            while len(text) % 16 != 0:
                text += '\0'
            return (text)
        secret = add_to_16(secret)
        iv = bytes().fromhex(iv)
        cipher = AES.new(secret.encode('utf-8'), AES.MODE_CBC, iv)
        data = readdata  # 16beishu
        data = bytes().fromhex(data)
        try :
            encrypt_data = cipher.encrypt(data)  # 输入需要加密的字符串,注意字符串长度要是16的倍数。16,32,48..
            encrypt_data = bytes.decode(b2a_hex(encrypt_data))
            return encrypt_data
        except:
            return bytes.decode(b2a_hex(data))

    def jiemihanshu(mima, readdata, iv):
        secret = mima  # 由用户输入的16位或24位或32位长的初始密码字符串

        def add_to_16(text):
            while len(text) % 16 != 0:
                text += '\0'
            return (text)

        secret = add_to_16(secret)
        iv = bytes().fromhex(iv)  # 随机获取16位变量
        encrypt_data = bytes().fromhex(readdata)
        cipher = AES.new(secret.encode('utf-8'), AES.MODE_CBC, iv)
        try:
            decrypt_data = cipher.decrypt(encrypt_data)
            decrypt_data = bytes.decode(b2a_hex(decrypt_data))
            return decrypt_data
        except:
            return bytes.decode(b2a_hex(encrypt_data))

    f = open(dir, "rb+")
    file_stats = os.stat(dir)
    max = int(file_stats.st_size)
    i = 0
    while True:
        a = read(dir, i,32, f)
        if flagmi == 1:
            abc = jiamihanshu(mima,a,"20060815200608152006081520060815")
        if flagmi == 2:
            abc = jiemihanshu(mima,a,"20060815200608152006081520060815")
        write(abc,dir,i,f)
        i += 1024
        bar1.step(1024)
        root.update()
        if i+1024>=(min(max,10000000)):
            f.close()
            break
def coding(dir,mima,mima_len,flagmi):
    file_stats = os.stat(dir)
    max = int(file_stats.st_size)
    realcode1(dir,mima,mima_len,flagmi,)
def jiami(dir,save_dir,mima,mima_len):
    shutil.copyfile(dir,save_dir)
    coding(save_dir,mima,mima_len,1)
def jiemi(dir,save_dir,mima,mima_len):
    shutil.copyfile(dir, save_dir)
    coding(save_dir,mima,mima_len,2)
def function1():
    global file2,singleflag
    OpenFile = Tk()   #创建新窗口
    OpenFile.withdraw()
    file_path2 = filedialog.askopenfilenames(filetypes=[('加密文件(*.ghl)','.ghl')]) # 获取路径
    a3.configure(state='normal')
    a3.delete(0, 'end')
    a3.insert(INSERT, file_path2)
    a3.configure(state='disable')
    file2 = file_path2
    singleflag = 1
def function2():
    global file1,singleflag
    OpenFile = Tk()   #创建新窗口
    OpenFile.withdraw()
    file_path1 = filedialog.askopenfilenames() # 获取路径
    a2.configure(state='normal')
    a2.delete(0, 'end')
    a2.insert(INSERT, file_path1)
    a2.configure(state='disable')
    file1 = file_path1
    singleflag = 1
def functionjia():
    global file1,singleflag
    OpenFile = Tk()  # 创建新窗口
    OpenFile.withdraw()
    file_path1 = filedialog.askdirectory()
    file1 = ()
    for root1, dirs, files in os.walk(file_path1, topdown=False):
        for a in files:
            newroot = ''
            for evestr in root1:
                if evestr == '\\':
                    newroot+='/'
                else:
                    newroot+=evestr
            file1+=(newroot+'/'+a,)
    a2.configure(state='normal')
    a2.delete(0, 'end')
    a2.insert(INSERT, file_path1)
    a2.configure(state='disabled')
    singleflag = 0
def functionjie():
    global file2,singleflag
    OpenFile = Tk()  # 创建新窗口
    OpenFile.withdraw()
    file_path2 = filedialog.askdirectory()
    file2 = ()
    for root1, dirs, files in os.walk(file_path2, topdown=False):
        for a in files:
            newroot = ''
            for evestr in root1:
                if evestr == '\\':
                    newroot += '/'
                else:
                    newroot += evestr
            file2 += (newroot + '/'+a,)
    a3.configure(state='normal')
    a3.delete(0, 'end')
    a3.insert(INSERT, file_path2)
    a3.configure(state='disabled')
    singleflag = 0
def function3():#jiami
    global file1,log,singleflag
    if singleflag == 0:
        mima = a1.get()
        newmima = ''
        for i in mima:
            newmima = newmima+str(ord(i))
        mima = newmima
        mima_len = len(mima)
        cha = 1024
        save_dir = filedialog.askdirectory(initialdir=a2.get())
        print(save_dir)
        for everypath in file1:
            if save_dir!='':
                log.configure(state='normal')
                log.insert(INSERT, '正在加密'+save_dir+everypath[len(a2.get()):])
                log.configure(state='disabled')
                root.update()
                if not os.path.exists(save_dir+everypath[len(a2.get()):everypath.rfind('/')]):
                    os.makedirs(save_dir+everypath[len(a2.get()):everypath.rfind('/')])
                file_stats = os.stat(everypath)
                bar1['maximum'] = min(int(file_stats.st_size),10000000) + 1
                jiami(everypath,save_dir+everypath[len(a2.get()):]+'.ghl',mima,mima_len)
                bar1['value'] = 0
                log.configure(state='normal')
                log.insert(INSERT, '  完成!' + '\n')
                log.mark_set("insert", "end")
                log.see("insert")
                log.configure(state='disabled')
                root.update()
        log.configure(state='normal')
        log.insert(INSERT, '加密完成!所有文件均输出至' +save_dir+ '\n')
        log.configure(state='disabled')
    elif singleflag==1:
        mima = a1.get()
        newmima = ''
        for i in mima:
            newmima = newmima + str(ord(i))
        mima = newmima
        mima_len = len(mima)
        cha = 1024
        save_dir = filedialog.askdirectory(initialdir=file1[0][:file1[0].rfind('/')])
        for everypath in file1:
            if save_dir != '':
                log.configure(state='normal')
                log.insert(INSERT, '正在加密' + save_dir + everypath[everypath.rfind('/'):])
                log.configure(state='disabled')
                root.update()
                file_stats = os.stat(everypath)
                bar1['maximum'] = min(int(file_stats.st_size), 10000000) + 1
                jiami(everypath, save_dir + everypath[everypath.rfind('/'):] + '.ghl', mima, mima_len)
                bar1['value'] = 0
                log.configure(state='normal')
                log.insert(INSERT, '  完成!' + '\n')
                log.mark_set("insert", "end")
                log.see("insert")
                log.configure(state='disabled')
                root.update()
        log.configure(state='normal')
        log.insert(INSERT, '加密完成!所有文件均输出至' + save_dir + '\n')
        log.configure(state='disabled')
def function4():#jiemi
    global file2,log,singleflag
    if singleflag==0:
        mima = a1.get()
        newmima = ''
        for i in mima:
            newmima = newmima+str(ord(i))
        mima = newmima
        mima_len = len(mima)
        cha = 1024
        save_dir = filedialog.askdirectory(initialdir=a3.get())
        for everypath in file2:
            if save_dir!='':
                if everypath[everypath.rfind('.'):]=='.ghl':
                    log.configure(state='normal')
                    log.insert(INSERT, '正在解密' + save_dir + everypath[len(a3.get()):])
                    log.configure(state='disabled')
                    if not os.path.exists(save_dir + everypath[len(a3.get()):everypath.rfind('/')]):
                        os.makedirs(save_dir + everypath[len(a3.get()):everypath.rfind('/')])
                    file_stats = os.stat(everypath)
                    bar1['maximum'] = min(int(file_stats.st_size),10000000) + 1
                    jiemi(everypath,save_dir+everypath[len(a3.get()):everypath.rfind('.')],mima,mima_len)
                    log.configure(state='normal')
                    log.insert(INSERT, '  完成!' + '\n')
                    log.mark_set("insert", "end")
                    log.see("insert")
                    log.configure(state='disabled')
                    bar1['value'] = 0
        log.configure(state='normal')
        log.insert(INSERT, '解密完成!所有文件均输出至' + save_dir + '\n')
        log.configure(state='disabled')
    elif singleflag==1:
        mima = a1.get()
        newmima = ''
        for i in mima:
            newmima = newmima + str(ord(i))
        mima = newmima
        mima_len = len(mima)
        cha = 1024
        save_dir = filedialog.askdirectory(initialdir=file2[0][:file2[0].rfind('/')])
        for everypath in file2:
            if save_dir != '':
                log.configure(state='normal')
                log.insert(INSERT, '正在解密' + save_dir + everypath[everypath.rfind('/'):])
                log.configure(state='disabled')
                file_stats = os.stat(everypath)
                bar1['maximum'] = min(int(file_stats.st_size), 10000000) + 1
                jiemi(everypath, save_dir + everypath[everypath.rfind('/'):everypath.rfind('.')], mima, mima_len)
                log.configure(state='normal')
                log.insert(INSERT, '  完成!' + '\n')
                log.mark_set("insert", "end")
                log.see("insert")
                log.configure(state='disabled')
                bar1['value'] = 0
        log.configure(state='normal')
        log.insert(INSERT, '解密完成!所有文件均输出至' + save_dir + '\n')
        log.configure(state='disabled')
if __name__ == '__main__':

    picture = open("picture.ico", "wb+")#载入图标
    picture.write(base64.b64decode(img))
    picture.close()

    multiprocessing.freeze_support()
    singleflag = 2
    root = Tk()
    root.title('文件解密工具')
    root.iconbitmap('picture.ico')
    os.remove("picture.ico")
    root.minsize(560, 420)
    #root.maxsize(560, 420)
    root.protocol("WM_DELETE_WINDOW", lambda: sys.exit(0))
    L1 = Label(root, text="密码:",width=10,font=("Calibri 16"))
    a1 = Entry(root,width=20,font=("Calibri 16"))
    Lth = Label(root, text="使用进程数:",width=10)
    ath = Entry(root,width=20,font=("Calibri 16"))
    L2 = Label(root, text="加密:",width=10,font=("Calibri 16"))
    a2 = Entry(root,width=20,font=("Calibri 16"))
    b = Button(root,text='浏览文件',command=function2,width=10,font=("Calibri 16"))
    b1 = Button(root,text='浏览文件夹',command=functionjia,width=10,font=("Calibri 16"))
    c = Button(root,text='加密',command=function3,width=10,font=("Calibri 16"))
    L3 = Label(root, text="解密:",width=10,font=("Calibri 16"))
    a3 = Entry(root,width=20,font=("Calibri 16"))
    d = Button(root,text='浏览文件',command=function1,width=10,font=("Calibri 16"))
    d1 = Button(root,text='浏览文件夹',command=functionjie,width=10,font=("Calibri 16"))
    e = Button(root,text='解密',command=function4,width=10,font=("Calibri 16"))
    bar1 = tkinter.ttk.Progressbar(root,length = 550)
    log = tkinter.scrolledtext.ScrolledText(root, width=85, height=20)
    L1.grid(row=0,column=0)
    a1.grid(row=0,column=1)  # 将小部件放置到主窗口中
    a1.delete(0, "end")
    a1.insert(0, "ghacg.com")
    #
    b.grid(row=1,column=2)
    #
    b1.grid(row=1,column=3)
    #
    c.grid(row=1,column=4)
    d.grid(row=2,column=2)
    #
    d1.grid(row=2, column=3)
    e.grid(row=2,column=4)
    #
    L2.grid(row=1,column=0)
    L3.grid(row=2,column=0)
    #
    a2.grid(row=1,column=1)
    a3.grid(row=2,column=1)
    bar1.grid(row=3,column=0,columnspan=5)
    log.grid(row=4,column=0,columnspan=5)
    ath.insert(INSERT, 4)
    log.configure(state='disabled')
    a3.configure(state='disabled')
    a2.configure(state='disabled')
    root.mainloop()  # 进入消息循环

效果展示

全部文件(包括编译后的单个EXE)

网页链接(pan.baidu.com)?pwd=c22f

其中EXE文件可以独立运行,免安装,无需环境。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得UP主同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理: DMCA投诉/Report
赞 7
按踩
4
收藏
4条评论

正好需要,感谢~

2022-11-30

建议给macOS和Linux同时编译一份,或者至少开源让我能自己编译(

2022-12-23

没有必要对所有字节加密吧,加密前几个字节应该就可以了,类似rpgmv的加密方式

2022-12-23

小白一個,請問這個是怎麽樣用的呢

2023-1-9
我是有底线的
内容滑到底啦,去看看别的风景吧