W型加密围栏的解密,栅栏,密码

发表时间:2020-09-14

题目描述:被小鱼一连将了两军,你心里更加不服气了。两个人一起继续往前走, 一路上杂耍卖艺的很多,但是你俩毫无兴趣,直直的就冲着下一个谜题的地方去了。 到了一看,这个谜面看起来就已经有点像答案了样子了,旁边还画着一张画,是一副农家小院的 图画,上面画着一个农妇在栅栏里面喂5只小鸡,你嘿嘿一笑对着小鱼说这次可是我先找到答案了
ccehgyaefnpeoobe{lcirg}epriec_ora_g

1

# -*- coding: utf-8 -*-

'''
helloworldgoodmorningxxxx 5
h       l       r       x
 e     r d     o n     x
  l   o   g   m   i   x
   l w     o d     n x
    o       o       g
hlrnerdonilogmiqlwodnxoog

7
5 1
3 3
1 5
7


helloworldgoodmorningxxxx 4
h     o     o     i     x
 e   w r   o d   n n   x
  l o   l g   m r   g x
   l     d     o     x
hooixewrodnnxlolgmrgxldox

5
3 1
1 3
5
'''

def enc(plain, num):
    matrix = [([0] * len(plain)) for i in range(num)]

    # 获取i的取值序列
    i_s = []
    for a in range(num):
        i_s.append(a)
    for a in range(num - 2, 0, -1):
        i_s.append(a)
    i_s_len = len(i_s)

    # 按规则写入
    i = 0
    for c in plain:
        matrix[i_s[i % i_s_len]][i] = c
        i += 1

    # 排除空值,从头到尾取出
    encrypted = ''
    for i in range(num):
        for j in range(len(plain)):
            if matrix[i][j]:
                encrypted += matrix[i][j]

    # 临时输出
#    for i in range(num):
#        for j in range(len(plain)):
#            print (matrix[i][j], ' ')
#        print()
    
    return encrypted


def dec(encrypted, num):
    matrix = [([0] * len(encrypted)) for i in range(num)]
    cur = 0
    for i in range(num):  # 按行来填
        # 生成每行空格个数的取值序列
        if i == 0:  # 第1行和最后一行,只需要一个取值就好了
            pair = [(num-(i+1))*2-1]
        elif i == num-1:
            pair = [i*2-1]
        else:
            pair = [(num-(i+1))*2-1, i*2-1]
        
        # 按规则填入
        pair_i = 0
        j = i
        while True:
            if cur < len(encrypted):
                matrix[i][j] = encrypted[cur]
            cur += 1
            j += pair[pair_i % len(pair)]+1  # 这里要加1,直接加间隔是不够的
            pair_i += 1
            if j >= len(encrypted):
                break

    # 临时输出
#    for i in range(num):
#        for j in range(len(encrypted)):
#            print (matrix[i][j], ' ')
#        print()

    # 获取i的取值序列
    i_s = []
    for a in range(num):
        i_s.append(a)
    for a in range(num - 2, 0, -1):
        i_s.append(a)
    i_s_len = len(i_s)
    # 按规则取出
    decrypted = ''
    for j in range(len(encrypted)):
        decrypted += matrix[i_s[j % i_s_len]][j]
    return decrypted




encrypted = 'ccehgyaefnpeoobe{lcirg}epriec_ora_g'
num = 5
for i in range(2,len(encrypted)):
    print('分为'+str(i)+'栏时,解密结果为:'+dec(encrypted, i)  )
'''
hooixewrodnnxlolgmrgxldox
helloworldgoodmorningxxxx
cyberpeace{railfence_cipher_gogogo}
'''

文章来源互联网,如有侵权,请联系管理员删除。邮箱:417803890@qq.com / QQ:417803890

微配音

Python Free

邮箱:417803890@qq.com
QQ:417803890

皖ICP备19001818号
© 2019 copyright www.pythonf.cn - All rights reserved

微信扫一扫关注公众号:

联系方式

Python Free