W型栅栏密码


# coding:utf8

'''
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 = enc('helloworldgoodmorningxxxx', 11)
print encrypted
decrypted = dec(encrypted, 11)
print decrypted

encrypted = 'ccehgyaefnpeoobe{lcirg}epriec_ora_g'
num = 5
print dec(encrypted, num)

'''
hooixewrodnnxlolgmrgxldox
helloworldgoodmorningxxxx
cyberpeace{railfence_cipher_gogogo}
'''

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
W栅栏密码是一种简单的加密算法,它通过将明文按照一定规则排列成W,然后按照特定顺序读取密文来实现加密。解密W栅栏密码的过程与加密相反,需要按照相同的规则重新排列密文,然后按照特定顺序读取明文。 以下是一个使用C++实现W栅栏密码解密的示例代码: ```cpp #include <iostream> #include <string> std::string decryptWZhalan(const std::string& ciphertext, int numRows) { if (numRows <= 1) { return ciphertext; } int len = ciphertext.length(); std::string plaintext(len, ' '); int cycleLen = 2 * numRows - 2; int index = 0; for (int i = 0; i < numRows; i++) { for (int j = 0; j + i < len; j += cycleLen) { plaintext[index++] = ciphertext[j + i]; if (i != 0 && i != numRows - 1 && j + cycleLen - i < len) { plaintext[index++] = ciphertext[j + cycleLen - i]; } } } return plaintext; } int main() { std::string ciphertext = "WZHALNCPTOY"; int numRows = 4; std::string plaintext = decryptWZhalan(ciphertext, numRows); std::cout << "Plaintext: " << plaintext << std::endl; return 0; } ``` 在上述代码中,`decryptWZhalan`函数接受两个参数:密文和栅栏的行数。它首先判断行数是否小于等于1,如果是,则直接返回密文。然后,根据W栅栏的规则,按照特定顺序将密文中的字符重新排列成明文。最后,返回解密后的明文。 在示例代码中,我们使用了一个密文"WZHALNCPTOY"和4行的栅栏进行解密。运行代码后,将输出解密后的明文"WHYCATZLONP"。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值