SystemFileB

加强版凯撒加密 - Python

Feb 10th, 2026 (edited)
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.14 KB | None | 0 0
  1. #!/usr/bin/python3
  2. """
  3. ## ⚡加强版凯撒密码加密/解密器⚡ 3.2.1
  4. By: SystemFileB
  5.  
  6. 凯撒密码,但是可以自定义字符集来提高安全性
  7. 此文件使用MIT(Expat)许可证分发,记得注明原出处
  8.  
  9. ## 如何使用?
  10. 1. 在当前目录创建一个input.txt,输入被加密/将要加密的文本
  11. 2. 执行脚本,根据提示输入相关的内容
  12. 3. 在当前目录下的output.txt即为加密/解密结果
  13. """
  14. import os
  15. from getpass import getpass
  16.  
  17. cs = [
  18.     # 数字
  19.     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  20.     # 大写字母
  21.     'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
  22.     'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  23.     # 小写字母
  24.     'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  25.     'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  26.     # GBK半角符号(包含大多数可显示半角符号)
  27.     '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/',
  28.     ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`',
  29.     '{', '|', '}', '~', ' ', "\u3000", '\n', '\t',  # 空格和全角空格(GBK编码)
  30.     # 中文常用半角符号(GBK编码)
  31.     '·', '…',
  32.     '¥', '¢', '£', '§', '※', '★', '☆', '○', '●', '◎', '◇', '◆',
  33.     '□', '■', '△', '▲', '▽', '▼', '→', '←', '↑', '↓', '〓',
  34.     # 较少见但可显示的半角符号
  35.     ' ̄', 'ˊ', 'ˋ', 'ˇ', '々', '〆', '〇', '─', '━', '│', '┃', '┄', '┅',
  36. ]
  37.  
  38.  
  39. l=len(cs)               # 字符集长度
  40. print(f"字符集长度:{l}\n")
  41.  
  42. if not os.path.exists("input.txt"):
  43.     with open("input.txt","w",encoding="utf-8") as f:
  44.         f.write("删除我并重新输入新内容")
  45.         f.close()
  46.     print("你还没有新建input.txt,已为你创建了一个\n请你删除这个文件里的所有内容,随后放入你需要加密的内容\n注意,input.txt编码必须为UTF-8!\n")
  47.     getpass("按下回车键退出...")
  48.     exit()
  49.  
  50. m=input("加密还是解密 (e=加密, d=解密):")=="e"
  51.  
  52. xz=False
  53. base=False
  54. bx=input("其他加密方式 (b=base85,x=xz+base85,空=都不使用):")
  55. if bx.find("b")!=-1:
  56.     from base64 import b85encode,b85decode
  57.     base=True
  58. if bx.find("x")!=-1:
  59.     from lzma import compress,decompress,FORMAT_XZ
  60.     from base64 import b85encode,b85decode
  61.     xz=True
  62.  
  63. try:
  64.     p=int(input("凯撒偏移量:"))
  65. except ValueError:
  66.     print("输入数字!")
  67.     exit(1)
  68.  
  69. def ks(s):
  70.     out=""
  71.     type="加" if m else "解"
  72.     if not m:
  73.         s=s[s.find("/")+1:s.rfind("\")]
  74.         s=s.replace("_\n","\n")
  75.     else:
  76.         out="/"
  77.     prog=0
  78.     progt=len(s)
  79.     for c in s:
  80.         finded=False
  81.         for i in range(l):
  82.             if c==cs[i]:
  83.                 finded=True
  84.                 pos=i
  85.         if finded:
  86.             if m:
  87.                 out+=cs[(pos+p)%l]
  88.             else:
  89.                 out+=cs[(pos-p)%l]
  90.         else:
  91.             out+=c  # 字符不在字符集中,保持原样
  92.             print("\n未找到:",c)
  93.         prog+=1
  94.         text=f"{prog}/{progt}"
  95.         print(f"\x1b[{len(text)+14}D凯撒{type}密进度:{text}",end="")
  96.     if m:
  97.         out+="\"
  98.         out=out.replace("\n","_\n")
  99.     print()
  100.     return out
  101.  
  102. with open("input.txt","r",encoding="utf-8") as f:
  103.     s=f.read()
  104.     f.close()
  105. if m:
  106.     if xz:
  107.         tmp=b""
  108.         print("LZMA2 压缩...")
  109.         tmp=compress(s.encode("utf-8"),format=FORMAT_XZ)
  110.         print("Base85 编码...")
  111.         s=b85encode(tmp).decode("utf-8")
  112.  
  113.     elif base:
  114.         print("Base85 编码...")
  115.         s=b85encode(s.encode("utf-8")).decode("utf-8")
  116.     out=ks(s)
  117. else:
  118.     out=ks(s)
  119.     if base:
  120.         print("Base85 解码...")
  121.         out=b85decode(out.encode("utf-8")).decode("utf-8")
  122.     elif xz:
  123.         tmp=b""
  124.         print("Base85 解码...")
  125.         tmp=b85decode(out.encode("utf-8"))
  126.         print("LZMA2 解压...")
  127.         out=decompress(tmp).decode("utf-8")
  128.  
  129.  
  130. with open("output.txt","w",encoding="utf-8") as f:
  131.     f.write(out)
  132. print("结果已保存到:./output.txt")
  133.  
Add Comment
Please, Sign In to add comment