CTF Warmup
题目
This machine will output an encrypted text of a maximum of 8 characters. Your task is to decrypt the code below:
qtgxuxci,1913
It is known that the mechanism of the encryption is like a Caesar cipher; but each characters is shifted by a different amount. The amount that is being shifted is referred to the first 50000 decimal numbers of Pi
一、分析
题目给出了提示是使用Caesar加密和偏移量是选取小数点后50000位中的序列作为位移量,最大支持输入8个英文字符,后面的五位数字与位移序列有关
通过输入8个a得到序列iiijbegfh,可得到偏移量是88914657,通过代码找出pi的小数点后5万位中是否有这个序列
import mpmath
mpmath.mp.dps = 50000
target = str(mpmath.pi)[1:50002]
number = "88914657"
position = target.find(number)
key = int(position) - 20317
print(key)
结果得到key为13356 表示加密结果中的数字加上13356得到位移开始的位置
二、解密
输入任意字母进行验证abcdefgh
得到bhcdjgih,8866,偏移量是16005120,再次运行刚才的脚本,得到结果13356
由此找到加密的机制,编写代码解密
import mpmath
def decrypt(encrypted_text,num):
mpmath.mp.dps = 50000
pi = str(mpmath.pi)[1:]
key = 13356
init = num + key
shifted_num = pi[init:init+8]
offsets = [int(digit) for digit in shifted_num]
decrypted_text = ""
for i, char in enumerate(encrypted_text):
offset = offsets[i % len(offsets)]
decrypted_char = chr((ord(char) - ord('a') - offset) % 26 + ord('a'))
decrypted_text += decrypted_char
return decrypted_text
encrypted_text = input("请输入加密文本: ")
enp, num = encrypted_text.split(',')
num = int(num)
original_text = decrypt(enp, num)
print("解密后的文本: ", original_text)