# [SWPUCTF 2021 新生赛]ez_caesar # [SWPUCTF 2021 新生赛]ez_caesar 附件: ```python import base64 def caesar(plaintext): str_list = list(plaintext) i = 0 while i < len(plaintext): if not str_list[i].isalpha(): str_list[i] = str_list[i] else: a = "A" if str_list[i].isupper() else "a" str_list[i] = chr((ord(str_list[i]) - ord(a) + 5) % 26 + ord(a) or 5) i = i + 1 return ''.join(str_list) flag = "*************************" str = caesar(flag) print(str) #str="U1hYSFlLe2R0em1mYWpwc3RiaGZqeGZ3fQ==" ``` - `str_list = list(plaintext)`:将 flag 字符串转化为字符列表 > 字符串是不可变的,而列表是可变的,所以通过将字符串转换为列表,可以逐个修改其中的字符 - `if not str_list[i].isalpha()`:如果当前字符不是字母 - `isalpha()`:字符串方法,用于检查字符串是否只包含字母,在这里是检查当前字符是否为字母 - `a = "A" if str_list[i].isupper() else "a"`:三元运算,如果当前字符为大写字母则变量 a 为 "A" ,反制则为 "a" - `isupper()`:字符串方法,检查字符串中的所有字符是否都是大写字母,在这里是检查当前字符是否为大写字母 - `chr((ord(str_list[i]) - ord(a) + 5) % 26 + ord(a) or 5)`:进行了凯撒密码的替换操作 - `ord(str_list[i])`:返回字符 str_list[i] 的 ASCII 值 - `(ord(str_list[i]) - ord(a) + 5)`:计算字符相对于起始点 a 的偏移量,并加上5 - `% 26`:确保偏移量在 0 到 25 之间 - `+ ord(a)`:将结果映射回字母的 ASCII 范围,以便得到替换后的字符 - `or 5`:确保结果至少是字符 E,以防字符不是字母。这样的处理是为了避免空白字符和标点符号的错误处理 - `join(str_list)`:将字符列表转换回字符串 由此可见这是一个凯撒加密的脚本,将 `+5` 改成 `-5` 即为逆向脚本 U1hYSFlLe2R0em1mYWpwc3RiaGZqeGZ3fQ== 看上去是 base64 的编码,在逆向前先解码为 SXXHYK{dtzmfajpstbhfjxfw} EXP: ```python import base64 def caesar_decrypt(ciphertext): str_list = list(ciphertext) i = 0 while i < len(ciphertext): if not str_list[i].isalpha(): str_list[i] = str_list[i] else: a = "A" if str_list[i].isupper() else "a" str_list[i] = chr((ord(str_list[i]) - ord(a) - 5) % 26 + ord(a) or 5) i = i + 1 return ''.join(str_list) str = "SXXHYK{dtzmfajpstbhfjxfw}" flag = caesar_decrypt(str) print(flag) ```