1. Introduction to the fence password

The so-called fence password is to encrypt the plaintext into a group of N, and then the first word of each group connected to form a paragraph of irregular words. However, there is an unspoken rule in the fence code itself, which is that there are usually not too many letters to make up the fence. — Baidu Baike

For example, divide the plaintext string “Hello World” into two groups, that is, the barrier (key) is 2.

Helloworld 2 1 group: helloworld take the first letter combination: hlool take the second letter combination: elwrd finally get fence encryption: hloolelwrdCopy the code

Note: When len(strings)%n! When = 0, the missing bits are filled with @. Len (strins) indicates the length of the plaintext string, and n indicates the number of fences (keys).

2. Encrypt the fence password

Fence_Passwd_encode. Py:

#! /usr/bin/python
# Env: python3
# Author: afei_0and1
# -*- coding: utf8 -*-

def fence_Passwd_encode() :
    string = input('Please enter the plaintext to be encrypted:')
    num = int(input('Please enter fence number (key) :'))
    res = ' '
    for i in range(int(num)):
        Loop over the output
        for j in range(int(string.__len__()/num + 0.5)) :#print(j)
            try:
                res += string[j*num+i]
            except:
                pass
    print(res)
            
if(__name__ == '__main__'):
    fence_Passwd_encode()
Copy the code

3. Fence decryption (blasting)

Fence_Passwd_burst. Py:

#! /usr/bin/python
# Env: python3
# Author: afei_0and1
# -*- coding: utf8 -*-

import sys

def fence_Passwd_burst() :
    if (len(sys.argv) < 2) :print("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --")
        print("")
        print("Useg: python %s <fence_encryp_string>" % sys.argv[0])
        print("eg: python fence_Passwd_burst.py 'hloolelwrd'")
        print("")
        print("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --")
        return
    secret = sys.argv[1]
    
    # fence password decryption, loop through how many columns
    res = [step for step in range(2.len(secret)) if len(secret)%step == 0]
    for step in res:
        flag = ' '
        # To obtain decryption results, use the step column to traverse to obtain decrypted flag
        for i in range(step):
            flag += secret[i::step]
        print('Column %s: Decryption result: %s'% (str(step), flag))

if(__name__ == '__main__'):
    fence_Passwd_burst()

Copy the code

4.W fence password encryption

The W barrier cipher is a variant of the barrier cipher. The plaintext is arranged in W shape and the letters in each line are connected to form the ciphertext. The number of lines is the barrier number, that is, the key. Decryption is also based on the “W” pattern, each column of letters joined together to form the plaintext. The key of a W-fence cipher is not only a factor of the ciphertext length, but can be any integer smaller than the ciphertext length greater than 1.

Fence_W -type – encode. Py:

#! /usr/bin/python
# Env: python3
# Author: afei_0and1
# -*- coding: utf8 -*-

import sys

def generaget_W() :
    if (len(sys.argv) < 3) :print("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --")
        print("")
        print("Useg: python %s <string> <col_num>" % sys.argv[0])
        print("eg: python fence_W-Type_encode.py helloworld 3")
        print("")
        print("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --")
        return
    string = sys.argv[1]
    num = sys.argv[2]
    
    arr = [[The '*'] *len(string) for i in range(int(num))]
    # number of rows, originally defined as 0
    row = 0
    upflag = False
    # Press "W" to draw string on the matrix
    for column in range(len(string)):
        arr[row][column] = string[column]
        if row == int(num)-1:
            upflag = True
        if row == 0:
            upflag = False
        if upflag:
            # on the border
            row -= 1
        else:
            # under the boundary
            row += 1
    # Print the W pattern
    for i in arr:
        print(i)
    
    # Store the encrypted result
    res = []
    for row in range(int(num)):
        for column in range(len(string)):
            ifarr[row][column] ! =The '*':
                res.append(arr[row][column])
    print(' '.join(res))
    
if(__name__ == '__main__'):
    generaget_W()

Copy the code

Original address: blog.csdn.net/qq_41490561…