鱼C论坛

 找回密码
 立即注册
查看: 2466|回复: 1

[技术交流] 基于密码字典的加密工具及字典生成(凯撒密码)

[复制链接]
发表于 2023-1-11 23:02:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
从自己的项目中扒出来的代码(

加密功能和字典生成已完成,解密功能嘛。。。。在路上。。。。

原函数文档用英文写的,这里懒得再写,所有直接机翻,可能会有亿些别扭

  1. class TechnologyError(Exception):
  2.     pass

  3. class Password:
  4.     def Set_Config(self) -> None:
  5.         self.Mode = ["Strict", "Ignore", "Retain"]
  6.         self.Ignore = [" ", "\n", "(", ")", ",", ".", "_", ":", "-"]
  7.     def Set_Str(self) -> None:
  8.         self.Uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  9.         self.Lowercase = "abcdefghijklmnopqrstuvwxyz"
  10.         self.Numbers = "1234567890"
  11.     def Export_Undefined(self, Text:str, Dictionary:dict) -> list:
  12.         return [Undefined for Undefined in Text if Undefined not in Dictionary]


  13. class Dictionary(Password):
  14.     def __init__(self) -> None:
  15.         super().Set_Str()


  16.     def Caesar(self, Offset:int, Uppercase=True, Lowercase=False, Number=False, Self_String=None) -> dict:
  17.         """
  18.         Offset
  19.             设置字符偏移的位数
  20.         Uppercase
  21.             添加大写字母
  22.         Lowercase
  23.             添加小写字母
  24.         Number
  25.             添加数字
  26.         Self_String
  27.             使用自己的字符串
  28.         
  29.         Note:
  30.             1. 启用 Self_String 后,将忽略大写、小写和数字的 bool 类型,并且会直接使用 Self_String 作为值生成字典,因此此时忽略的三个数据集将无效
  31.             2. offset 参数不能超过字典中要生成的字符串数,否则会自爆
  32.             3. 不能同时设置大写、小写和数字而不设置Self_String,即没有字符串要生成,否则会自爆。
  33.         """
  34.         _Upper = self.Uppercase
  35.         _Lower = self.Lowercase
  36.         _Numbers = self.Numbers
  37.         _Offset = int(Offset)
  38.         _StringList = []
  39.         _Dictionary = {}
  40.         _Source = Self_String if bool(Self_String) else (_Upper if bool(Uppercase) else "") + (_Lower if bool(Lowercase) else "") + (_Numbers if bool(Number) else "")
  41.         _ErrorMessage = "The offset data exceeds the limit" if _Offset > len(_Source) else "The offset data exceeds the limit" if _Source is None else False
  42.         if bool(_ErrorMessage):
  43.             raise TechnologyError(_ErrorMessage)
  44.         for each in _Source:
  45.             _StringList.append(each)
  46.         for each in _StringList:
  47.             _Dictionary[each] = _StringList[_StringList.index(each) + _Offset] if _StringList.index(each) + _Offset < len(_StringList) else _StringList[_StringList.index(each) - len(_StringList) + _Offset]
  48.         
  49.         return _Dictionary

  50. class Encrypt(Password):
  51.     def __init__(self) -> None:
  52.         super().Set_Config()
  53.    

  54.     def Caesar(self, Text:str, Dictionary:dict, Mode="Strict", Add_Ignore=True) -> str:
  55.         """
  56.         Text
  57.             需要加密的文本
  58.         Dictionary
  59.             加密所需的字典(可以使用字典对象生成)
  60.         Mode
  61.             加密模式(默认为严格),可以在对象的 Mode 属性中查看所有模式。
  62.         Add_Ignore
  63.             在字典中添加需要忽略的字符,这些字符将被保留(空格默认禁用并启用),可以修改 Ignore 的值来更改它们

  64.         模式文档
  65.             Strict: 将所有明文字符包含在字典中,如果不这样做,它们就会自爆。
  66.             Ignore: 忽略并丢弃字典中未包含的所有字符。
  67.             Retain: 保留未定义的字符(未定义的字符会自动添加到字典中)
  68.         """
  69.         # 错误检查
  70.         if bool(Add_Ignore):
  71.             for each in self.Ignore:
  72.                 Dictionary[each] = each
  73.         _Undefined = super().Export_Undefined(Text, Dictionary) # 导出未定义的字符
  74.         _ErrorMessage = "The plaintext entered is empty" if not bool(str(Text)) else "The current pattern does not exist" if Mode not in self.Mode else "The value of the dictionary must be of type dict" if type(Dictionary) is not dict else "Contains characters that are not defined in the dictionary {}{}".format("\n", str(_Undefined)) if bool(_Undefined) and self.Mode.index(Mode) == 0 else None # 导出错误信息
  75.         if bool(_ErrorMessage):
  76.             raise TechnologyError(_ErrorMessage)
  77.         _ModeType = self.Mode.index(Mode) # 将Mode字符转换为整数
  78.         _Password = ""
  79.         if _ModeType == 2 and bool(_Undefined):
  80.             for each in _Undefined:
  81.                 Dictionary[each] = each # Retain模式添加未定义的字符
  82.         _PasswordList = [Dictionary[each] for each in Text] if _ModeType == 0 else [Dictionary[each] for each in Text if each in Dictionary] if _ModeType == 1 else [Dictionary[each] for each in Text] if _ModeType == 2 else None # 加密
  83.         for each in _PasswordList:
  84.             _Password += each

  85.         return _Password
复制代码

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-1-11 23:02:55 | 显示全部楼层
一行流用得有亿点多。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-25 04:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表