CTF warmup

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)

在这里插入图片描述

三、结果

得到flag: hkbulove

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值