Analysis of fence encryption and decryption script algorithm with arbitrary number of columns

Source: Internet
Author: User

Script reference: Https://github.com/erfze/CTF_tools/tree/master/Rail_Fence_encipherment

1. String equal length division

def cut_string (string,length):    textarr=re.findall ('. { '+str (length) +'}', String)    Textarr.append (String[len (Textarr) *Length:])    return Textarr

Using the FindAll in the RE module to split the string by the specified length, you can refer to Bole online This article on the RE module (http://python.jobbole.com/88729/), I think it is very detailed, clear.

The first parameter of FindAll is the pattern (Pattern matching object), and the following parameter represents a regular expression constructed with the specified split length.

' . {'+str (length) +'}'

2. Encryption

defrail_fence_encryption (group_char_number,string): Str_len=len (string) ts_list=cut_string (String,group_char_number)#Temporary storageResult_str="'     forIinchRange (group_char_number): forJinchRange (len (ts_list)):Try: Result_str=result_str+Ts_list[j][i]except:                Pass    return 'Encryption Result:'+result_str

Group_char_number represents the number of characters in each column, and uses the Cut_string function to split the string, even if the last column has less than group_char_number of characters.

Like what:

Rail_fence_encryption (3,'1234567890')

The result is:

['123','456','789', ' 0 ']

The try-except is to prevent the index value from exceeding the length of the last column.

3. Decryption

defrail_fence_decryption (group_number,string): Group_char_number=int (Math.floor (len (String)/group_number)) Add_char_number=len (String)%Group_number Ts_list1=cut_string (string[:((group_char_number+1) *add_char_number)],group_char_number+1) Ts_list2=cut_string (string[(group_char_number+1) *add_char_number):],group_char_number) ts_list=ts_list1+Ts_list2 result_str="'     forIinchRange (group_char_number+1):         forJinchRange (len (ts_list)):Try: Result_str=result_str+Ts_list[j][i]except:                Pass    return 'decryption Result:'+result_str

Decryption is a bit more complicated, first of all group_number represents the number of columns, and group_char_number for each column "normal" (later I will say "abnormal" case) should have the number of characters per column.

Add_char_number represents the last column in the first few columns should be the number of characters per column +1, the following a chestnut on the point of glance.

Rail_fence_decryption (4, ' 1234567890 ')

String length is 10, and the number of columns is 4, can not just split the string, then the "normal" number of characters per column should be int (10/4), but also the remaining 10%4=2 characters, the two characters are the first two columns per column character length of +1.

The results are as follows:

['123','456','a ', '  - ']

If you feel a bit difficult to understand, you can give some of the chestnut offspring into the function of the simulation run will understand.

Analysis of fence encryption and decryption script algorithm with arbitrary number of columns

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.