月牙博客 专注于“胡说八道”。

基于Python3的加密程序分享

发布时间:2022-12-02 18:16:00  类别:life

运行环境:Python3.0以上

初始创建文件:rawFile 和 outPath 作为输入文件,输出文件。

import base64, sys, os
import hashlib

def isFile(File):
    return os.path.isfile(File)

def read_file(file_path, encodingl):
    content=None
    try:
        with open(file_path,"r",encoding=encodingl) as file:
            content = file.read()
        file.close()
    except Exception as err:
        print(err)
    return content

def save_file(file_path,content, encodingl):
    try:
        with open(file_path, "w",encoding=encodingl) as file:
            file.write(content)
        file.close()
    except Exception as err:
        print(err)

def getMD5ID(Num):
    content = str(Num*3.1415).encode("utf-8")
    md5hash = hashlib.md5(content)
    md5 = md5hash.hexdigest()
    return md5.upper()

def ChangeBackNum(strl, Num):
    md5 = getMD5ID(Num)
    len_md5 = len(md5)
    lln = list(strl)
    changeNum = Num%10
    if changeNum == 0:
        changeNum = 10
    first = len(lln)-1-changeNum
    for i in range(first, -1, -1):
        temp = lln[i]
        lln[i] = lln[i+changeNum]
        lln[i+changeNum] = temp
    chars = "".join(lln)
    charss = chars.replace(md5[0:len_md5//2],"").replace(md5[len_md5//2:],"")
    return charss

def ChangeNum(strl, Num):
    md5 = getMD5ID(Num)
    len_md5 = len(md5)
    len_strl = len(strl)
    lln = list( strl[0:len_strl//2]+md5[0:len_md5//2]+strl[len_strl//2:]+md5[len_md5//2:] )
    changeNum = Num%10 
    if changeNum == 0:
        changeNum = 10
    length = len(lln)
    for i in range(0,length,1):
        if i+changeNum > (length-1):
            break
        temp = lln[i]
        lln[i] = lln[i+changeNum]
        lln[i+changeNum] = temp
    chars = "".join(lln)
    return chars

def main1(stringl,program_version):#加密
    try:
        sec_key =int( input("密钥:") )
    except:
        return
    t = base64.b64encode(stringl.encode("utf-8"))
    outstring = str(t).split("'")
    return "program_version:{}\nsecurity_key:{}\n{}".format(program_version, sec_key, ChangeNum(outstring[1], sec_key) )

def main2(stringl):#解密
    tt = None
    try:
        sec_key =int( input("密钥:") )
    except:
        return
    t = ChangeBackNum(stringl, sec_key)
    t1 = bytes(t, encoding="utf-8")
    try:
        tt = base64.b64decode(t1).decode("utf-8")
    except Exception as err:
        print(err)
    return tt

if __name__ == "__main__":
    program_version = "2.0"

    rawFile = "rawr.txt"
    #rawFile = "q.txt"
    outPath = "temp/security.txt"

    print("1:加密,2:解密")
    id = input("input:")

    if id == "1":
        pass
    if id == "2":
       temp_file = rawFile
       rawFile = outPath
       outPath = temp_file

    dic_TF = {"y":True, "n":False}
    isWay = input("是否通过命令行输入输出:y/n ")
    if dic_TF[isWay]:
        stringl = input("内容:")
    else:
        if isFile(rawFile) == False:
            print("{}不存在,请先创建".format(rawFile))
            sys.exit(1)
        stringl = read_file(rawFile, "utf-8")
    if stringl == None:
        sys.exit(1)

    if "1" == id:
        res = main1(stringl, program_version)
        if dic_TF[isWay]:
            print(res)
        else:
            save_file(outPath,res, "utf-8")
    elif "2" == id:
        res = main2(stringl)
        if dic_TF[isWay]:
            print(res)
        else:
            save_file(outPath, res,"utf-8")
    else:
        pass

Contact Info.

留言: 123guestbook