CTF
  • Introduction
  • IDF 实验室
    • 牛刀小试
      • 被改错的密码
      • ASCII 码而已
      • 聪明的小羊
      • 摩斯密码
      • 啥?
    • CRYPTO 百密一疏
      • 笨笨的小猪
      • 凯撒加密
      • 孔子的学费
      • 特殊的日子
      • 伟人的名字
    • WEB天罗地网
      • COOKIE欺骗
      • 不难不易的js加密
      • 超简单的js题
      • 古老的邮件编码
      • 简单的js解密
      • 你关注最新的漏洞吗
      • 一种编码而已
    • STEGA万里寻踪
      • 图片里的秘密
      • 上帝也哭泣
      • 红与黑
    • PPC初探乾坤
      • 简单编程-字符统计
      • Fuck your brain
      • 谁是卧底
    • REVERSE倒行逆施
      • 简单的PE文件逆向
      • 简单的ELF逆向
      • python ByteCode
    • MISC包罗万象
      • 图片里的英语
      • 抓到一只苍蝇
  • 实验吧
    • WEB
      • 登陆一下好吗??
      • 注入
      • 简单的sql注入
      • Forms
Powered by GitBook
On this page
  • 题目描述
  • write up

Was this helpful?

  1. IDF 实验室
  2. 牛刀小试

聪明的小羊

题目描述

一只小羊跳过了栅栏,两只小样跳过了栅栏,一坨小羊跳过了栅栏...

tn c0afsiwal kes,hwit1r g,npt ttessfu}ua u hmqik e {m, n huiouosarwCniibecesnren.

write up

栅栏密码, 题目说得很清楚了, 自己用 Python 写个脚本解决, 栅栏数为 5, 解密后得到明文:

the anwser is wctf{C01umnar},if u is a big new,u can help us think more question,tks.

from itertools import zip_longest

__author__ = '__L1n__w@tch'


class RailFence:
    """
    栅栏密码, 初始化需要一个栏数, 内含加密操作与解密操作, 以及内置使用的分组操作
    """

    def __init__(self, number):
        self.num = number  # 规定几个一组

    def encrypt(self, text_decrypted):
        """
        栅栏密码的加密操作
        :param text_decrypted: "WoShiZhongWenBuShiYingWen"
        :return: 栅栏数为 5 时的加密结果, "WZWSnohehgSoniWhnBYeiguin"
        """
        if len(text_decrypted) % self.num != 0:
            raise RuntimeError("待加密的长度需要是栏数的倍数")
        text_encrypted = str()
        groups = RailFence.__divide_group(text_decrypted, self.num)

        for order in range(self.num):
            for each_group in groups:
                text_encrypted += each_group[order]

        return text_encrypted

    def decrypt(self, text_encrypted):
        """
        栅栏密码的解密操作
        :param text_encrypted: "WZWSnohehgSoniWhnBYeiguin"
        :return: 栅栏数为 5 时的解密结果, "WoShiZhongWenBuShiYingWen"
        """
        if len(text_encrypted) % self.num != 0:
            raise RuntimeError("待解密的密文应该是栅栏数的倍数")
        text_decrypted = str()
        groups = RailFence.__divide_group(text_encrypted, len(text_encrypted) // self.num)

        for order in range(len(text_encrypted) // self.num):
            for each_group in groups:
                text_decrypted += each_group[order]

        return text_decrypted

    @staticmethod
    def __divide_group(text, size):
        """
        对字符串进行分组操作
        :param text: "abcdefghi"
        :param size: 3
        :return: ["abc", "def", "ghi"]
        """
        args = [iter(text)] * size
        blocks = list()
        for block in zip_longest(*args):
            blocks.append("".join(block))

        return blocks


if __name__ == "__main__":
    num = 5
    rail_fence = RailFence(num)
    cipher_text = "tn c0afsiwal kes,hwit1r  g,npt  ttessfu}ua u  hmqik e {m,  n huiouosarwCniibecesnren."
    plaintext = rail_fence.decrypt(cipher_text)
    print("plaintext = {0}\n{num}-cipher_text = {1}".format(plaintext, cipher_text, num=num))
PreviousASCII 码而已Next摩斯密码

Last updated 5 years ago

Was this helpful?