Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- """
- ## ⚡加强版凯撒密码加密/解密器⚡ 3.2.1
- By: SystemFileB
- 凯撒密码,但是可以自定义字符集来提高安全性
- 此文件使用MIT(Expat)许可证分发,记得注明原出处
- ## 如何使用?
- 1. 在当前目录创建一个input.txt,输入被加密/将要加密的文本
- 2. 执行脚本,根据提示输入相关的内容
- 3. 在当前目录下的output.txt即为加密/解密结果
- """
- import os
- from getpass import getpass
- cs = [
- # 数字
- '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
- # 大写字母
- 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
- 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
- # 小写字母
- 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
- 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
- # GBK半角符号(包含大多数可显示半角符号)
- '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/',
- ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`',
- '{', '|', '}', '~', ' ', "\u3000", '\n', '\t', # 空格和全角空格(GBK编码)
- # 中文常用半角符号(GBK编码)
- '·', '…',
- '¥', '¢', '£', '§', '※', '★', '☆', '○', '●', '◎', '◇', '◆',
- '□', '■', '△', '▲', '▽', '▼', '→', '←', '↑', '↓', '〓',
- # 较少见但可显示的半角符号
- ' ̄', 'ˊ', 'ˋ', 'ˇ', '々', '〆', '〇', '─', '━', '│', '┃', '┄', '┅',
- ]
- l=len(cs) # 字符集长度
- print(f"字符集长度:{l}\n")
- if not os.path.exists("input.txt"):
- with open("input.txt","w",encoding="utf-8") as f:
- f.write("删除我并重新输入新内容")
- f.close()
- print("你还没有新建input.txt,已为你创建了一个\n请你删除这个文件里的所有内容,随后放入你需要加密的内容\n注意,input.txt编码必须为UTF-8!\n")
- getpass("按下回车键退出...")
- exit()
- m=input("加密还是解密 (e=加密, d=解密):")=="e"
- xz=False
- base=False
- bx=input("其他加密方式 (b=base85,x=xz+base85,空=都不使用):")
- if bx.find("b")!=-1:
- from base64 import b85encode,b85decode
- base=True
- if bx.find("x")!=-1:
- from lzma import compress,decompress,FORMAT_XZ
- from base64 import b85encode,b85decode
- xz=True
- try:
- p=int(input("凯撒偏移量:"))
- except ValueError:
- print("输入数字!")
- exit(1)
- def ks(s):
- out=""
- type="加" if m else "解"
- if not m:
- s=s[s.find("/")+1:s.rfind("\")]
- s=s.replace("_\n","\n")
- else:
- out="/"
- prog=0
- progt=len(s)
- for c in s:
- finded=False
- for i in range(l):
- if c==cs[i]:
- finded=True
- pos=i
- if finded:
- if m:
- out+=cs[(pos+p)%l]
- else:
- out+=cs[(pos-p)%l]
- else:
- out+=c # 字符不在字符集中,保持原样
- print("\n未找到:",c)
- prog+=1
- text=f"{prog}/{progt}"
- print(f"\x1b[{len(text)+14}D凯撒{type}密进度:{text}",end="")
- if m:
- out+="\"
- out=out.replace("\n","_\n")
- print()
- return out
- with open("input.txt","r",encoding="utf-8") as f:
- s=f.read()
- f.close()
- if m:
- if xz:
- tmp=b""
- print("LZMA2 压缩...")
- tmp=compress(s.encode("utf-8"),format=FORMAT_XZ)
- print("Base85 编码...")
- s=b85encode(tmp).decode("utf-8")
- elif base:
- print("Base85 编码...")
- s=b85encode(s.encode("utf-8")).decode("utf-8")
- out=ks(s)
- else:
- out=ks(s)
- if base:
- print("Base85 解码...")
- out=b85decode(out.encode("utf-8")).decode("utf-8")
- elif xz:
- tmp=b""
- print("Base85 解码...")
- tmp=b85decode(out.encode("utf-8"))
- print("LZMA2 解压...")
- out=decompress(tmp).decode("utf-8")
- with open("output.txt","w",encoding="utf-8") as f:
- f.write(out)
- print("结果已保存到:./output.txt")
Add Comment
Please, Sign In to add comment