超级简单加解密工具

方案:读取文件头,提取特定长度进行加密,加密后加这一部分写入源文件,解密可逆。

#! /usr/bin/env python
#coding=utf-8
#Edit:Sandy
#时间:2019年1月27日13:57:04
#功能:文件的加密和解密

import os
from LogUtility import Log,CreateLoggerFile

def readheader(path):
    f = open(path,"rb")
    Log("读取源文件")
    for now in f:
        if len(now)>= 20:
            Log("返回文件头字符串")
            return now[0:18]
            break
        elif len(now)>= 3:
            #print("返回少量文件头字符")
            Log("返回少量文件头字符")
            return  now[0:(len(now)-2)]
            break
        else:
            Log("文件内容太少")
            return "FFFFFFFFFF"
            break

#加密
def encrypt(path,message,key1):

    f  = open(path,"rb+")
    Log("打开需要加密的文件")
    Log("使用内置密码,错位循环加密")

    kl = len(key1)
    result1= []
    if "\r\n" in message:
        ml = len(message)-2
        key = ml // kl * key1 + key1[:ml % kl]

        for i in range(len(message)-2):
            newByte = chr(ord(message[i]) ^ ord(key[i]))
            result1.append(newByte)
        result1.append("\r\n")

    else:
        ml = len(message)
        key = ml // kl * key1 + key1[:ml % kl]
        for i in range(len(message)):
            newByte = chr(ord(message[i]) ^ ord(key[i]))
            result1.append(newByte)
    result2 = "".join(result1)
    #print result2

    f.write(result2)
    Log("将密文合入文件")

    f.close()
    Log("关闭文件")

    print("文件加密完毕^_^")
    Log("文件加密完毕^_^")

def decrypt(path, message,key1):

    f = open(path, "rb+")
    Log("打开需要解密的文件")

    Log("使用内置密码,错位循环解密")

    kl = len(key1)

    result1= []

    if "\r\n" in message:
        ml = len(message)-2
        key = ml // kl * key1 + key1[:ml % kl]
        for i in range(len(message)-2):
            newByte = chr(ord(message[i]) ^ ord(key[i]))
            result1.append(newByte)
        result1.append("\r\n")
    else:
        ml = len(message)
        key = ml // kl * key1 + key1[:ml % kl]
        for i in range(len(message)):
            newByte = chr(ord(message[i]) ^ ord(key[i]))
            result1.append(newByte)

    result2 = "".join(result1)
    #print "解密后的字符串"
    #print result2
    f.write(result2)
    Log("解除密文")

    f.close()
    Log("关闭文件")

    #print("文件解密完毕^_^")
    Log("文件解密完毕^_^")

  

日志部分代码:

import logging

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)


def CreateLoggerFile(filename):
    try:
        fulllogname = filename
        fh = logging.FileHandler(fulllogname)
        fh.setLevel(logging.DEBUG)
        formatter = logging.Formatter('%(asctime)s [line:%(lineno)d] %(message)s')
        fh.setFormatter(formatter)
        logger.addHandler(fh)
    except Exception as err:
        logger.debug("Error when creating log file, error message: {}".format(str(err)))


def Log(message):
    logger.debug(message)

  

因为作为工具来使用,我们做一个简单的GUI,用Tkinter实现

#! /usr/bin/env python
#coding=utf-8
#Edit:Sandy
#date:2019/1/28

import os
import Tkinter
from Tkinter import *
import tkMessageBox as messagebox
import tkFileDialog,Tkconstants
import ttk
from LogUtility import Log,CreateLoggerFile
from Lock import encrypt,decrypt,readheader

def commodata(jiamiflag):

    if jiamiflag:

        logadd = jiamilog_t.get('0.0', END).split()[0]
        Log("LOG地址获取完成")
        CreateLoggerFile(logadd)
        Log("创建日志文件")

        flag = 0
        try:
            filename0 = jiamifname_t.get('0.0',END).split("\n")[0]
            Log("从GUI上拿到完整路径+文件名")
        except Exception as e:
            Log("filename获取出错或直接点击了加密按钮,程序自动跳转到选择文件")
            Log(str(e))
            flag = 1

        if flag == 1:
            Log("调用浏览app")
            filename1 = selectfilejiami()

        else:
            filename1 = filename0

        Log("成功获取文件绝对路径+文件名")
        return filename1

    else:
        logadd = jiemilog_t.get('0.0', END).split()[0]
        Log("LOG地址获取完成")
        CreateLoggerFile(logadd)
        Log("创建日志文件")

        flag = 0
        try:
            filename0 = jiemifname_t.get('0.0', END).split("\n")[0]
            Log("从GUI上拿到完整路径+文件名")
        except Exception as e:
            Log("filename获取出错或直接点击了解密按钮,程序自动跳转到选择文件")
            Log(str(e))
            flag = 1

        if flag == 1:
            Log("调用浏览app")
            filename1 = selectfilejiemi()
        else:
            filename1 = filename0

        Log("成功获取文件绝对路径+文加名")
        #print filename1
        return filename1


def qidongjiamiapp():

    jiamiflag = 1
    Log("文件名获取中")
    jiamifilename =commodata(jiamiflag)
    if jiamifilename !=1:

        message = readheader(jiamifilename)
        #print message
        if message == "FFFFFFFFFF":
            messagebox.showwarning("警告","文件内容太少或者这不是一个正确文件")
        elif(message is None):
            messagebox.showwarning("警告", "空文件")
        else:
            Log("获取message完成")
            Log("调用加密app")
            encrypt(jiamifilename,message,"zhengjing1128")
            Log("加密完成")
            messagebox.showinfo("提示","加密成功")
    else:

        #messagebox.showwarning("提示","请选择正确的文件")
        pass

def qidongjiemiapp():
    
    jiamiflag = 0
    Log("文件名获取中")
    jiemifilename = commodata(jiamiflag)
    if jiemifilename != 1:

        message = readheader(jiemifilename)
        if message == "FFFFFFFFFF":
            messagebox.showwarning("警告","文件内容太少或者这不是一个正确文件")
        elif (message is None):
            messagebox.showwarning("警告", "空文件")
        else:
            Log("获取message完成")
            Log("调用加密app")
            decrypt(jiemifilename,message,"zhengjing1128")
            Log("解密完成")
            messagebox.showinfo("提示","解密成功")
    else:
        #messagebox.showwarning("提示","请选择正确的文件")
        pass

def selectfilejiami():

    Log("获取选择的文件完整路径和名称")
    s = tkFileDialog.askopenfilename()
    Log(s)

    if s == "":
        messagebox.showerror("Error","未选择文件")
        return 1
    else:
        jiemifname_t.delete('1.0', 'end')
        jiamifname_t.delete('1.0','end')
        jiamifname_t.insert(Tkinter.END, s)
        jiamifname_t.see(END)
        Log("已将加密文件路径输出到GUI")
        return s

def selectfilejiemi():

    Log("获取选择的文件完整路径和名称")
    s = tkFileDialog.askopenfilename()
    Log(s)

    if s == "":
        messagebox.showerror("Error","未选择文件")
        return 1
    else:

        jiamifname_t.delete('1.0', 'end')
        jiemifname_t.delete('1.0','end')
        jiemifname_t.insert(Tkinter.END, s)
        jiemifname_t.see(END)
        Log("已将解密文件路径输出到GUI")
        return s

if __name__ == "__main__":

    try:
        root = Tkinter.Tk()
        root.title("加解密工具")
        root.geometry("800x450")
        print "debug1"
        root.resizable(width=False, height=False)
        Tkinter.Label(root, text='加密区域', fg='blue', font=("黑体", 20, "bold")).grid(row=0, column=2)
        Tkinter.Label(root, text='*Version: v0.1-20190128*', fg='blue', font=("黑体", 10)).grid(row=11, column=4)
        Tkinter.Label(root, text='*Design : Sandy Zheng*', fg='blue', font=("黑体", 10)).grid(row=12, column=4)

        Tkinter.Label(root,text='日志地址:',font=("黑体", 10)).grid(row=1,column=1)
        jiamilog_t=Tkinter.Text(root,height=3,width=60)
        jiamilog_t.grid(row=1,column=2)
        jiamilog_t.insert('0.0','E:/jiami-Log.txt')

        Tkinter.Label(root, text='加密文件完整路径:',font=("黑体", 10)).grid(row=2, column=1)
        jiamifname_t = Tkinter.Text(root, height=3, width=60)
        jiamifname_t.grid(row=2, column=2)

        jiamiinputfile =Tkinter.Button(root, text='浏览',activeforeground = "red",fg='blue',font=("黑体", 12, "bold"), height =2,command= selectfilejiami)
        jiamiinputfile.grid(row = 2,column = 3)

        btn_jiami=Tkinter.Button(root,text='开始加密',activeforeground = "red",fg='black',font=("黑体", 12, "bold"),height = 2,command=qidongjiamiapp)
        btn_jiami.grid(row=5,column=3)


        Tkinter.Label(root,text =  "").grid(row=6,column=1)
        #Tkinter.Label(root, text="").grid(row=7, column=1)
        Tkinter.Label(root, text='解密区域', fg='blue', font=("黑体", 20, "bold")).grid(row=7, column=2)


        Tkinter.Label(root,text='日志地址:',font=("黑体", 10)).grid(row=8,column=1)

        jiemilog_t=Tkinter.Text(root,height=3,width=60)
        jiemilog_t.grid(row=8,column=2)
        jiemilog_t.insert('0.0','E:/jiemi-Log.txt')

        Tkinter.Label(root, text='解密文件完整路径:',font=("黑体", 10)).grid(row=9, column=1)

        jiemifname_t = Tkinter.Text(root, height=3, width=60)
        jiemifname_t.grid(row=9, column=2)

        jiemiinputfile =Tkinter.Button(root, text='浏览',activeforeground = "red",fg='blue',font=("黑体", 12, "bold"), height =2,command= selectfilejiemi)
        jiemiinputfile.grid(row = 9,column = 3)


        btn_jiemi = Tkinter.Button(root, text='开始解密', activeforeground="red", fg='black', font=("黑体", 12, "bold"),
                                  height=2, command=qidongjiemiapp)
        btn_jiemi.grid(row=10, column=3)

        root.mainloop()
    except Exception as e:
        pass

  

完成后,我们可以用py2exe模块直接生成exe可执行文件,做好后界面如下图:

 

posted @ 2019-03-06 21:01  Sandy1128  阅读(1932)  评论(0编辑  收藏  举报