曲路密码(Bend crypto)

曲路密码(Bend crypto)

  • 加密对象: 所有字符
  • 原理
    • 该密码和栅栏密码类似,是一种移位密码

    • 秘钥就是行列数,但我感觉只需要列数就好了(可能是我理解有问题),使用行列数构成一个表格,将明文依次填入,例如:行列为6列3行, 明文为: “flag{y0u_are_p1g@}”

      flag{y
      0u_are
      _p1g@}
    • 然后从最后一个字符像如下方式串起来即构成了密文:}ey{r@gaga_1pulf0_
      在这里插入图片描述

  • 代码:
    # write by 2021/8/4
    # 曲路密码
    import re
    
    
    def encrypt_bend(string, col, row=10):
        ciphertext = ""
        temp = []
        for i in range(col):
            temp.append([])
        for index, i in enumerate(string):
            temp[index % col].append(i)
        re_temp = list(reversed(temp))
        for index, i in enumerate(re_temp):
            if index % 2 == 0:
                i = list(reversed(i))
            ciphertext += "".join(i)
        return ciphertext
    
    
    def decrypt_bend(string, col, row=10):
        plaintext = ""
        length = len(string)
        min_row = length // col       # 最小的行数
        min_num = col - length % col  # 最小行数的列数
        # 分组
        temp = []
        index = 0
        for i in range(col):
            if i < min_num:
                temp.append(string[index:index+min_row])
                index += min_row
            else:
                temp.append(string[index:index+min_row+1])
                index += min_row + 1
        print(temp)
        # 改回列顺序
        for index, i in enumerate(temp):
            if index % 2 == 0:
                # print(re.findall(".{1}", temp[index]))
                temp[index] = "".join(list(reversed(re.findall(".{1}", temp[index]))))
        temp.reverse()
        for i in range(length):
            plaintext += temp[i % col][i // col]
        return plaintext
    
    
    if __name__ == '__main__':
        col_ = 7
        row_ = 5
        ciphertext_ = encrypt_bend("i will beat you this day", col_, row_)
        plaintext_ = decrypt_bend(ciphertext_, col_, row_)
        print(f"{plaintext_} : {ciphertext_}")
    
    
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值