鱼C论坛

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

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

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

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

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

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

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

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

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


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


    def Caesar(self, Offset:int, Uppercase=True, Lowercase=False, Number=False, Self_String=None) -> dict:
        """
        Offset
            设置字符偏移的位数
        Uppercase
            添加大写字母
        Lowercase
            添加小写字母
        Number
            添加数字
        Self_String
            使用自己的字符串
        
        Note:
            1. 启用 Self_String 后,将忽略大写、小写和数字的 bool 类型,并且会直接使用 Self_String 作为值生成字典,因此此时忽略的三个数据集将无效
            2. offset 参数不能超过字典中要生成的字符串数,否则会自爆
            3. 不能同时设置大写、小写和数字而不设置Self_String,即没有字符串要生成,否则会自爆。
        """
        _Upper = self.Uppercase
        _Lower = self.Lowercase
        _Numbers = self.Numbers
        _Offset = int(Offset)
        _StringList = []
        _Dictionary = {}
        _Source = Self_String if bool(Self_String) else (_Upper if bool(Uppercase) else "") + (_Lower if bool(Lowercase) else "") + (_Numbers if bool(Number) else "")
        _ErrorMessage = "The offset data exceeds the limit" if _Offset > len(_Source) else "The offset data exceeds the limit" if _Source is None else False
        if bool(_ErrorMessage):
            raise TechnologyError(_ErrorMessage)
        for each in _Source:
            _StringList.append(each)
        for each in _StringList:
            _Dictionary[each] = _StringList[_StringList.index(each) + _Offset] if _StringList.index(each) + _Offset < len(_StringList) else _StringList[_StringList.index(each) - len(_StringList) + _Offset]
        
        return _Dictionary

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

        模式文档
            Strict: 将所有明文字符包含在字典中,如果不这样做,它们就会自爆。
            Ignore: 忽略并丢弃字典中未包含的所有字符。
            Retain: 保留未定义的字符(未定义的字符会自动添加到字典中)
        """
        # 错误检查
        if bool(Add_Ignore):
            for each in self.Ignore:
                Dictionary[each] = each
        _Undefined = super().Export_Undefined(Text, Dictionary) # 导出未定义的字符
        _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 # 导出错误信息
        if bool(_ErrorMessage):
            raise TechnologyError(_ErrorMessage)
        _ModeType = self.Mode.index(Mode) # 将Mode字符转换为整数
        _Password = ""
        if _ModeType == 2 and bool(_Undefined):
            for each in _Undefined:
                Dictionary[each] = each # Retain模式添加未定义的字符
        _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 # 加密
        for each in _PasswordList:
            _Password += each

        return _Password

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-1-11 23:02:55 | 显示全部楼层
一行流用得有亿点多。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-24 23:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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