为了防止自己上传的文件可以被直接打开,又不想用什么专门的加密工具,一个按字节反码的小工具,防止手机端查看。
先上效果图
from os import path
from windnd import hook_dropfiles #文件拖拽使用,需安装windnd
from tkinter import Tk
from tkinter.ttk import Progressbar
from tkinter import Label
from tkinter import mainloop
from tkinter.scrolledtext import ScrolledText
from datetime import datetime
blocksize=4096 #默认块大小按照4kb加载
def FileBitComplement(filename):
if path.isfile(filename):
FileSize = path.getsize(filename)
t_name = path.splitext(filename)
filename_new = t_name[0] + '_new' + t_name[1]
file=open(filename,'rb')
file_new=open(filename_new,'wb')
print("文件大小%d"%FileSize)
progressbar1['maximum'] = FileSize
value = 0
while True:
buffer = file.read(blocksize)
buffer = bytearray(buffer)
size = min(blocksize, len(buffer))
for i in range(size):
buffer[i] = (~buffer[i]) & 0xff
value = value+i
progressbar1['value'] = value
top.update()
file_new.write(buffer)
if len(buffer) < blocksize:
break
file.close()
file_new.close()
return filename_new
#FileBitComplement('test_naw.zip')
def Dragged_file(files):
for file in files:
filefullname = str(file, encoding = "gbk") #windows默认编码为gbk,win10不知道是不是改成utf-8了
if path.isfile(filefullname):
print(filefullname)
FileSize = path.getsize(filefullname)
now_time = datetime.now().strftime('%F %T')
contents.config(state='normal')
contents.insert('end', '转换开始 %s \"%s\" 大小%d字节\n'%(now_time,filefullname,FileSize))
contents.config(state='disabled')
filename_new = FileBitComplement(filefullname)
now_time = datetime.now().strftime('%F %T')
filename_new = path.split(filename_new)[1]
contents.config(state='normal')
contents.insert('end', '转换完成 %s \"%s\" -> \"%s\"\n\n'%(now_time,filefullname,filename_new))
contents.config(state='disabled')
else:
print(filefullname+'跳过目录')
contents.config(state='normal')
contents.insert('end', '全部完成\n')
contents.config(state='disabled')
return
def RenewProgressbValue(value,maximum):
progressbar1['maximum'] = maximum
progressbar1['value'] = value
top.update()
#return
top = Tk()
top.title("文件反码转换(每个字节取反)")
top.geometry("640x390")
top.iconbitmap(".") #当前目录的图标
#拖放文件函数
hook_dropfiles(top, func=Dragged_file)
'''
ConvButton = tkinter.Button()
ConvButton.config(text='转换文件',command=RenewProgressbValue)
ConvButton.pack(side='top',padx=(1,1), pady=(1,0), fill='x')
'''
label=Label(top,text="把要转换的文件拖动到窗体中",fg='red',font=('bold'),borderwidth=2,relief="ridge")
label.pack(side='top',padx=(1,1), fill='x')
contents = ScrolledText()
contents.config(state='disabled')
contents.pack(side='top',fill='both', expand=True)
progressbar1 = Progressbar(top, orient='horizontal', length=200, mode='determinate')
progressbar1.pack(side='bottom',padx=(1,1), pady=(0,1), fill='both')
progressbar1['maximum'] = 100
progressbar1['value'] = 0
mainloop()