python之栅栏密码(任意栅栏,无密钥爆破)

        初学Python,想通过练习密码的加解密来逐渐掌握python的基本使用,下面是代码

'''
栅栏密码
可以根据num分栏
'''
import math

def encryption(plaintext,num):
    tmp_dict = {}
    #初始化字典
    for i in range(num):
        tmp_dict[i] = []

    #根据num 进行分组赋值(核心代码)
    for i in range(len(plaintext)):
        tmp_dict[i%num].append(plaintext[i])

    #从tmp_dict遍历出密文然后进行拼接
    ciphtertext = []
    for j in tmp_dict.values():
        j = ''.join(j)
        ciphtertext.append(j)
    ciphtertext = ''.join(ciphtertext)
    print(f'密文为: {ciphtertext}')

def decryption(ciphertext,num):
    plaintext = []
    t = int(len(ciphertext)/num)
    r = len(ciphertext) % num
    b = list(range(1,r+1))
    c = len(range(num)) - len(b)
    #用0补充b,使其长度和num一样
    for i in range(c):
        b.append(0)
    # print(r,b)
    if r != 0:
        for i,j in zip(range(num),b):
            a = 1
            #余数最多只有9
            if j == 1:
                plaintext.append(ciphertext[0 + t * i:a + t + t * i])
            elif j >= 2:
                plaintext.append(ciphertext[0 + (j-1) * a + t * i:j * a + t + t * i])
            else:
                # print(f'b,j,r分别为{b,j,r}')
                j = b[j+r-2]
                plaintext.append(ciphertext[0 + j*a + 1 + t * i:j*a + 1 + t + t * i])
    else:
        t = math.ceil(len(ciphertext)/num)
        for i in range(num):
            plaintext.append(ciphertext[0 + t * i: t + t * i])

    print(f'栅栏为{num}时,明文为: ')
    for i in range(math.ceil(len(ciphertext)/num)):
        for j in range(len(plaintext)):
            try:
                # print(plaintext)
                print(plaintext[j][i],end='')
            except:
                pass
    print()


if __name__ == '__main__':
    enter = int(input('加密输入0\n解密输入1\n无密钥暴力破解输入2\n'))
    # enter = 2

    #加密
    if enter == 0:
        plaintext = input('请输入明文: ').lower()
        num = int(input('总共将明文分为为num栏,请输入num: '))
        print(f'明文共{len(plaintext)}位')
        print('-----------------------------')
        # plaintext = 'stay hungry stay foolishstay hungry stay foolish'
        # num = 4
        if len(plaintext) % num != 0:
            plaintext = plaintext + ' '
        encryption(plaintext,num)
    #解密
    if enter == 1:
        ciphertext = input('请输入密文: ')
        num = int(input('请输入栅栏数: '))
        # ciphertext = 'qhaiuinazgiiys uhsu'
        # num = 7
        print(f'栅栏数为{num},密文长度为{len(ciphertext)}')
        decryption(ciphertext,num)
    #不知道栅栏数时进行解密,初始设定为256,可以自己调

    if enter == 2:
        # ciphertext = 'qhaiuinazgiiys uhsu'
        num = 2
        ciphertext = input('请输入密文: ')
        decryption(ciphertext, num)
        while num < 257:
            if num <= len(ciphertext):
                print(f'栅栏数为{num},密文长度为{len(ciphertext)}')
                decryption(ciphertext,num)
                num = num + 1
            else:
                print(f'\n目前{len(ciphertext)}位,栅栏在{num}时被停止')
                break
        # print('--------破解完毕--------')
  1. 可以把while < 257 改成其他栅栏数方便进行更多的遍历
  2. 解密的时候不会自动去空格,所以输入的时候不要多输或少输,会影响结果
栅栏密码是一种简单的加密方式,它将明文按照一定规律排列,然后读取特定的位置上的字符,形成密文。解密时需要按照相同的规律排列密文,然后读取特定位置上的字符,即可恢复明文。 下面是一个简单的 Python 实现: ```python def decrypt_fence(ciphertext, rails): # 根据密文长度和栅栏数计算每个栅栏的长度 rail_len = [0] * rails full_rails = len(ciphertext) // (rails*2-2) for i in range(rails): if i == 0 or i == rails - 1: rail_len[i] = full_rails else: rail_len[i] = full_rails * 2 remainder = len(ciphertext) % (rails*2-2) for i in range(remainder): if i < rails - 1: rail_len[i] += 1 else: rail_len[2*rails-i-2] += 1 # 根据每个栅栏的长度将密文拆分成多个子串 rails_text = [] start = 0 for i in range(rails): end = start + rail_len[i] rails_text.append(ciphertext[start:end]) start = end # 根据栅栏规律将子串合并成明文 plaintext = '' for i in range(full_rails+1): for j in range(rails): if i == 0 or i == full_rails: if j < len(rails_text) and len(rails_text[j]) > i: plaintext += rails_text[j][i] else: if j < len(rails_text) and len(rails_text[j]) > i: plaintext += rails_text[j][i] if j < len(rails_text) and len(rails_text[j]) > (2*full_rails-i): plaintext += rails_text[j][2*full_rails-i] return plaintext ``` 使用方法: ```python ciphertext = 'sae_ecnroirnptmsrtof_' rails = 3 plaintext = decrypt_fence(ciphertext, rails) print(plaintext) # 输出:"secret_message_to_fbi" ``` 其中,`ciphertext` 是密文,`rails` 是栅栏数。运行结果为明文。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值