What’s Rail-Fence

传统型的栅栏密码就是将明文分为n组,然后从每组里一个个拿出来,一直拿到没有,如果已经空了(长度和栏数取模不为零)那么就跳过下一组。

Python实现

写了一个小时。。。没打过ACM实在没得思维,慢慢写慢慢调,主要是解密的有点点难,相对网上一些代码我写的可能复杂,如果之后学习了算法可能会好一些吧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import math


def rfencode(str, l):
s = ''
group = math.ceil(len(str) / l) # 组数
for i in range(0, l):
for j in range(0, group):
ind = j * l + i
if ind < len(str):
s += str[ind]
return s


def rfdecode(str, l):
s = ''
oldgroup = math.ceil(len(str) / l)
group = math.ceil(len(str) / oldgroup) # 组数
plus = len(str) % group
length = math.ceil(len(str) / group)
arr = [""] * group
i = 0
j = 0
while j < group:
if plus:
arr[j] = str[i: i + length]
i += length
plus -= 1
else:
arr[j] = str[i: i + length - 1]
i += (length - 1)
j += 1
i = 0
j = 0
while (i + j * l) < len(str):
if i == group:
j += 1
i = 0
continue
s += arr[i % group][j % length]
i += 1
return s




if __name__ == '__main__':
pT = 'hellome'
print(rfencode(pT, 3))
sT = 'hleeolm'
print(rfdecode(sT, 3))

非传统W型

暂时用工具跑着加密方法如图

image-20200319225317346