Py学习记录
Python 常规学习
Python学习笔记(一)
Python学习笔记(二)
Python学习笔记(三)
Python学习笔记(四)
Python学习笔记(五)
Python学习笔记(六)
Python学习笔记(七)
Python习题(一)
Python习题(二)
Python习题(三)
Python习题(四)
Python习题(五)
Python常见Bug
Python编程环境
Python-依赖安装(三方库)
Python-VS Code
pip-换源
py 程序转 exe
Python-打开选择文件对话框
Python 项目
Python-密码学
Python-与佛伦禅
Python-喵语翻译
Python-翻译服务器
Python-邮件发送
Python-自动签到
Python-自动签到(Post请求)
Python-自动签到(模拟操作)
Python-图片添加二维码
Python-数据可视化
Python-端口扫描器
Python-未测试项目
Python-虚拟环境
Python-临时环境
Python-venv虚拟环境
Python-Conda
Python-OpenCV
OpenCV-人脸识别
Python-PyTorch
本文档使用 MrDoc 发布
-
+
首页
Python-密码学
参观密码学比赛: - [西工大第三届“探索·解密”趣味密码比赛-个人WriteUp](https://blog.csdn.net/qq_34427004/article/details/127832981) - [混入西工大,打CTF上大分!](https://mp.weixin.qq.com/s/oGf660AaxTQS_10qUoUaow) 相关在线工具: --- 密码学:https://wtool.com.cn/ciphers (工具集) 在线加密:https://ctf.bugku.com/tools (工具集) 与熊论道/熊曰加密:http://hi.pcmoe.net/index.html ⭐与佛论DES的禅:http://dafeigy.gitee.io/buddesha/# ⭐与佛论禅重制版V2:https://tools.takuron.top/talk-with-buddha/ ⭐喵语翻译:https://www.miao-lang.com/ 百家姓暗号:http://anhao.tlrkl.top/ 兽音译者:https://roar.dreagonmon.top/ text_blind_watermark(文字加水印):https://www.guofei.site/pictures_for_blog/app/text_watermark/v1.html 开源项目: --- 喵语翻译:https://github.com/miao-lang/miao-lang 、https://gitee.com/jin-xuening/miao-lang 与佛论DES的禅:https://gitee.com/dafeigy/buddesha 与佛论禅重制版V2:https://github.com/takuron/talk-with-buddha 与佛论禅_Python:https://gitee.com/Chao_bei/YuFoLunChan text_blind_watermark(文字加水印):https://github.com/guofei9987/text_blind_watermark blind_watermark(图片加水印):https://github.com/guofei9987/blind_watermark ## 与佛伦禅-Python 简易版 在 [与佛论禅_Python](https://gitee.com/Chao_bei/YuFoLunChan) 原有基础上进行改动而来 [Python-与佛伦禅](/doc/582/),这个是自制Python服务器版,提供了web和api两种方式 ```python # 导入所需的模块 from Crypto.Cipher import AES from random import choice # 阶段和补充密钥库 from Crypto.Util.Padding import pad, unpad # 设置初始化向量 IV = b'zwzwzwzwzwzwzwzw' # 定义一个汉字列表作为TudouCode的字符集 TUDOU = [ '滅', '苦', '婆', '娑', '耶', '陀', '跋', '多', '漫', '都', '殿', '悉', '夜', '爍', '帝', '吉', '利', '阿', '無', '南', '那', '怛', '喝', '羯', '勝', '摩', '伽', '謹', '波', '者', '穆', '僧', '室', '藝', '尼', '瑟', '地', '彌', '菩', '提', '蘇', '醯', '盧', '呼', '舍', '佛', '參', '沙', '伊', '隸', '麼', '遮', '闍', '度', '蒙', '孕', '薩', '夷', '迦', '他', '姪', '豆', '特', '逝', '朋', '輸', '楞', '栗', '寫', '數', '曳', '諦', '羅', '曰', '咒', '即', '密', '若', '般', '故', '不', '實', '真', '訶', '切', '一', '除', '能', '等', '是', '上', '明', '大', '神', '知', '三', '藐', '耨', '得', '依', '諸', '世', '槃', '涅', '竟', '究', '想', '夢', '倒', '顛', '離', '遠', '怖', '恐', '有', '礙', '心', '所', '以', '亦', '智', '道', '。', '集', '盡', '死', '老', '至' ] # 定义一个标记字符集,用于处理特殊字符(每次执行不同就是这个造成的,超过指定长度从这里抽一个作为标记符,可以随意添加但要唯一) BYTEMARK = ['冥', '奢', '梵', '呐', '俱', '哆', '怯', '諳', '罰', '侄', '缽', '皤'] def Encrypt(plaintext, key): """ 加密函数:将明文转换为TudouCode形式 """ # 将密钥进行填充或截断以满足AES要求的长度 key = key[:32].ljust(32, b'\x00') # 1. 将明文编码为UTF-16小端序 data = plaintext.encode('utf-16le') # 2. 添加填充(PKCS7) pads = (- len(data)) % 16 data = data + bytes(pads * [pads]) # 3. 使用AES-256-CBC进行加密 cryptor = AES.new(key, AES.MODE_CBC, IV) result = cryptor.encrypt(data) # 4. 编码并添加头部 return '佛曰:' + ''.join([TUDOU[i] if i < 128 else choice(BYTEMARK) + TUDOU[i-128] for i in result]) def Decrypt(ciphertext, key): """ 解密函数:将TudouCode形式的密文解密为明文 """ # 将密钥进行填充或截断以满足AES要求的长度 key = key[:32].ljust(32, b'\x00') # 1. 去除头部并解码 if ciphertext.startswith('佛曰:'): ciphertext = ciphertext[3:] data = b'' i = 0 while i < len(ciphertext): if ciphertext[i] in BYTEMARK: i = i + 1 data = data + bytes([TUDOU.index(ciphertext[i]) + 128]) else: data = data + bytes([TUDOU.index(ciphertext[i])]) i = i + 1 # 2. 使用AES-256-CBC进行解密 cryptor = AES.new(key, AES.MODE_CBC, IV) result = cryptor.decrypt(data) # 3. 去除填充(PKCS7) flag = result[-1] if flag < 16 and result[-flag] == flag: result = result[:-flag] # 4. 使用UTF-16小端序解码明文 return result.decode('utf-16le') else: return '' # 测试示例 # 自定义加密密钥 key = "Welcome to Zen Buddhism" print(Encrypt('你好世界HelloWord',key.encode())) print(Decrypt('佛曰:隸諳數迦缽恐諳能者侄藐侄婆哆數波皤呼諳道哆曳悉奢漫提闍特奢輸俱心他是冥數缽至俱死室缽室俱三冥道三俱滅吉',key.encode())) ``` ## 喵语翻译-Python 简易版 在 [喵语翻译](https://github.com/miao-lang/miao-lang) 原有基础上进行改动而来 ```Python import re import base64 import random # 随机函数库 # 定义基础64编码字符集 b64 = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/=' # 定义编码字符集,第一项为分隔字符,第二项为编码字符集 # 没有是有自动生成的,主要是可用的零宽度字符太少了,使用自动生成的会造成位数多的包含位数少相同内容,而造成匹配时出现错误匹配问题 # codes = ['\u200b','\u200c\u200d\u200e\u200f\u2029\ufeff'] # 已测试过Windows下零宽度字符 # codes = ['\u200b', '\u200c\u200d'] # 项目默认使用的零宽度字符 # 定义编码表 table = [] # 编码表预设为空 # 手动设定的编码表,使用\u200b\u200c\u200d这三个字符,\u200b作为分隔字符,\u200c\u200d作为01使用二进制方式标示出0~64,解决自动生成容易有长字符包含短字符内容问题 Defaulttable = ['\u200c\u200c\u200c\u200c\u200c\u200c\u200c','\u200b\u200c\u200c\u200c\u200c\u200c\u200c','\u200b\u200c\u200c\u200c\u200c\u200c\u200d','\u200b\u200c\u200c\u200c\u200c\u200d\u200c','\u200b\u200c\u200c\u200c\u200c\u200d\u200d','\u200b\u200c\u200c\u200c\u200d\u200c\u200c','\u200b\u200c\u200c\u200c\u200d\u200c\u200d','\u200b\u200c\u200c\u200c\u200d\u200d\u200c','\u200b\u200c\u200c\u200c\u200d\u200d\u200d','\u200b\u200c\u200c\u200d\u200c\u200c\u200c','\u200b\u200c\u200c\u200d\u200c\u200c\u200d','\u200b\u200c\u200c\u200d\u200c\u200d\u200c','\u200b\u200c\u200c\u200d\u200c\u200d\u200d','\u200b\u200c\u200c\u200d\u200d\u200c\u200c','\u200b\u200c\u200c\u200d\u200d\u200c\u200d','\u200b\u200c\u200c\u200d\u200d\u200d\u200c','\u200b\u200c\u200c\u200d\u200d\u200d\u200d','\u200b\u200c\u200d\u200c\u200c\u200c\u200c','\u200b\u200c\u200d\u200c\u200c\u200c\u200d','\u200b\u200c\u200d\u200c\u200c\u200d\u200c','\u200b\u200c\u200d\u200c\u200c\u200d\u200d','\u200b\u200c\u200d\u200c\u200d\u200c\u200c','\u200b\u200c\u200d\u200c\u200d\u200c\u200d','\u200b\u200c\u200d\u200c\u200d\u200d\u200c','\u200b\u200c\u200d\u200c\u200d\u200d\u200d','\u200b\u200c\u200d\u200d\u200c\u200c\u200c','\u200b\u200c\u200d\u200d\u200c\u200c\u200d','\u200b\u200c\u200d\u200d\u200c\u200d\u200c','\u200b\u200c\u200d\u200d\u200c\u200d\u200d','\u200b\u200c\u200d\u200d\u200d\u200c\u200c','\u200b\u200c\u200d\u200d\u200d\u200c\u200d','\u200b\u200c\u200d\u200d\u200d\u200d\u200c','\u200b\u200c\u200d\u200d\u200d\u200d\u200d','\u200b\u200d\u200c\u200c\u200c\u200c\u200c','\u200b\u200d\u200c\u200c\u200c\u200c\u200d','\u200b\u200d\u200c\u200c\u200c\u200d\u200c','\u200b\u200d\u200c\u200c\u200c\u200d\u200d','\u200b\u200d\u200c\u200c\u200d\u200c\u200c','\u200b\u200d\u200c\u200c\u200d\u200c\u200d','\u200b\u200d\u200c\u200c\u200d\u200d\u200c','\u200b\u200d\u200c\u200c\u200d\u200d\u200d','\u200b\u200d\u200c\u200d\u200c\u200c\u200c','\u200b\u200d\u200c\u200d\u200c\u200c\u200d','\u200b\u200d\u200c\u200d\u200c\u200d\u200c','\u200b\u200d\u200c\u200d\u200c\u200d\u200d','\u200b\u200d\u200c\u200d\u200d\u200c\u200c','\u200b\u200d\u200c\u200d\u200d\u200c\u200d','\u200b\u200d\u200c\u200d\u200d\u200d\u200c','\u200b\u200d\u200c\u200d\u200d\u200d\u200d','\u200b\u200d\u200d\u200c\u200c\u200c\u200c','\u200b\u200d\u200d\u200c\u200c\u200c\u200d','\u200b\u200d\u200d\u200c\u200c\u200d\u200c','\u200b\u200d\u200d\u200c\u200c\u200d\u200d','\u200b\u200d\u200d\u200c\u200d\u200c\u200c','\u200b\u200d\u200d\u200c\u200d\u200c\u200d','\u200b\u200d\u200d\u200c\u200d\u200d\u200c','\u200b\u200d\u200d\u200c\u200d\u200d\u200d','\u200b\u200d\u200d\u200d\u200c\u200c\u200c','\u200b\u200d\u200d\u200d\u200c\u200c\u200d','\u200b\u200d\u200d\u200d\u200c\u200d\u200c','\u200b\u200d\u200d\u200d\u200c\u200d\u200d','\u200b\u200d\u200d\u200d\u200d\u200c\u200c','\u200b\u200d\u200d\u200d\u200d\u200c\u200d','\u200b\u200d\u200d\u200d\u200d\u200d\u200c','\u200b\u200d\u200d\u200d\u200d\u200d\u200d'] # 生成编码表 # 注意,如果字符集数量中可用字符数量太少会造成长字符包含短字符内容问题,这种的建议手动设置可用二进制方式 def make_table(): sep = codes[0] # 获取编码分隔符 chars = codes[1] # 获取编码字符集 char_count = len(b64) # 获取基础64编码字符集的长度 code_len = len(chars) # 获取编码字符集的长度 # 循环直到编码表长度达到要求 while len(table) < char_count: table_len = len(table) # 遍历编码字符集 for i in range(code_len): c = chars[i] # 如果字符不在编码表中,添加字符 if c not in table: if len(table) >= char_count: break table.append(c) # 生成组合编码 for j in range(table_len): if len(table) >= char_count: break t = "".join([c, table[j]]) # 如果组合编码不在编码表中,添加组合编码 if t not in table: table.append(t) # 在每个编码前添加分隔符 for i in range(len(table)): table[i] = sep + table[i] # 将普通文本转换为喵语言 def human2miao(t, options=None): # 将文本进行Base64编码 t = base64.b64encode(t.encode('utf-8')).decode('utf-8') arr = [] # 遍历编码后的文本 for c in t: # 查找字符在基础64编码字符集中的索引 n = b64.index(c) # 使用编码表进行替换 arr.append(table[n]) # 生成最终的喵语言文本 data = "".join(arr) return add_punctuations(data, options) # 将喵语言转换为普通文本 def miao2human(t): # 判断是否是喵语言 if not is_miao(t): return "错误,非本星球语音" # 清理喵语言中的无关字符 t = clean(t) # 从编码表中查找并替换对应的基础64编码字符 for idx in range(len(table)-1, -1, -1): reg = table[idx] t = t.replace(reg, b64[idx]) # 将文本进行Base64解码 t = base64.b64decode(t.encode('utf-8')).decode('utf-8') return t # 添加标点符号,添加喵 def add_punctuations(t, options=None): # 获取调用符号和是否使用半角符号的选项 calls = options.get('calls', '喵') if options is not None else '喵' # 判断是否是使用半角字符(True半角/False全角) halfwidth_symbol = options.get('halfwidthSymbol', False) if options is not None else False a = list(t) idx = 0 # 遍历文本中的字符 while idx < len(a): c = ord(a[idx]) delta = (c % 60) + 1 idx += delta # 如果索引超出范围,结束循环 if idx >= len(a): break # 在指定位置添加调用符号 a[idx] += calls mod = idx % 32 # 根据索引位置添加标点符号 if mod in [0, 1, 2, 3]: a[idx] += ',' if halfwidth_symbol else ',' elif mod == 7: a[idx] += '.' if halfwidth_symbol else '。' elif mod == 8: a[idx] += '?' if halfwidth_symbol else '?' elif mod == 9: a[idx] += '!' if halfwidth_symbol else '!' elif mod == 10: a[idx] += '~' if halfwidth_symbol else '~' idx += 1 # 添加调用符号和结束符号 t = calls + ''.join(a) + calls + ('.' if halfwidth_symbol else '。') return t # 清理字符串中的无关字符 def clean(t): return re.sub(r'[^\u200b\u200c\u200d]', '', t) # 判断一个字符串是否是喵语言 def is_miao(t): if not t: return False return len(clean(t)) > 0 # 随机排序数组,支持传入种子以确保可以还原 def shuffle_array(arr, seed): random.seed(seed) # 设置随机种子 shuffled_arr = random.sample(arr, len(arr)) # 创建一个新的打乱数组 return shuffled_arr # 返回打乱后的数组 # 调用函数生成编码表,如果使用的是自定义编码表那么无需生成 # make_table() # 示例用法 # 打乱数组,增加一层加密 seed = "zwzw" # 设置随机种子 table = shuffle_array(Defaulttable,seed) # 打乱后的输出存入 table # 将普通文本转换为喵语言 text = "你好呀!!" miao_text = human2miao(text) # miao_text = human2miao(text,{"calls": "咕","halfwidthSymbol": "true"}) # calls替换喵,halfwidthSymbol设定半角字符 print(miao_text) # 输出: '喵喵喵喵!喵。' # 将喵语言转换为普通文本 decoded_text = miao2human(miao_text) print(decoded_text) # 输出: 'Hello, Miao!' ```
造物者W
2023年8月23日 13:37
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
分享
链接
类型
密码
更新密码