-
Notifications
You must be signed in to change notification settings - Fork 1
/
The RailFence Cipher.py
103 lines (89 loc) · 3.37 KB
/
The RailFence Cipher.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import string
import math
function = input("Input e to encrypt or d to decrypt:")
function = function.upper()
if function == 'E':
encrypt_message = ""
message = input("Please input your message:")
message_length = len(message)
fence_number = int(input("Please input your fence number:"))
# 创建全局空列表
for i in range(fence_number):
s = 'encrypt_message_list' + str(i)
vars()[s] = []
# 将字符串的字符加入列表
for i in range(message_length):
remainder = i % fence_number
s = 'encrypt_message_list' + str(remainder)
vars()[s].append(message[i])
# 打印加密信息
for i in range(fence_number):
s = 'encrypt_message_list' + str(i)
print((vars()[s]))
encrypt_message = encrypt_message + "".join(vars()[s])
print(encrypt_message)
elif function == 'D':
decrypt_message = ""
decrypt_message_list_size = 0
encrypt_message = input("Please input your encrypt_message:")
encrypt_message_length = len(encrypt_message)
fence_number = int(input("Please input your fence number:"))
j = 0
h = 0
m = 0
# 创建全局空列表
for i in range(fence_number):
s = 'decrypt_message_list' + str(i)
vars()[s] = []
# 基本列表大小,即字符串大小除以栅栏数目取整
basic_list_size = int(encrypt_message_length / fence_number)
# 字符串大小除以栅栏数目取余,后面根据余数确定每个列表最终大小
remainder1 = encrypt_message_length % fence_number
remainder2 = encrypt_message_length % fence_number
# 将字符串的字符加入列表
for i in range(encrypt_message_length):
s = 'decrypt_message_list' + str(j)
if m == 0 and remainder1 == 0:
if i == int(i / (basic_list_size-1)) * basic_list_size - 1:
vars()[s].append(encrypt_message[i])
j += 1
else:
vars()[s].append(encrypt_message[i])
elif m != 0 and remainder1 == 0:
if i == int(i / (basic_list_size - 1)) * basic_list_size - 1 + m:
(vars()[s]).append(encrypt_message[i])
j += 1
else:
vars()[s].append(encrypt_message[i])
elif remainder1 != 0:
if i == int(i / (basic_list_size - 1)) * basic_list_size + h and i != 0:
vars()[s].append(encrypt_message[i])
h += 1
j += 1
remainder1 -= 1
m += 1
else:
vars()[s].append(encrypt_message[i])
# 打印解密信息
for i in range(fence_number):
s = 'decrypt_message_list' + str(i)
print((vars()[s]))
if remainder2 == 0:
for q in range(basic_list_size):
for i in range(fence_number):
s = 'decrypt_message_list' + str(i)
decrypt_message = decrypt_message + (vars()[s])[0]
del (vars()[s])[0]
print(decrypt_message)
else:
for q in range(basic_list_size + 1):
for i in range(fence_number):
s = 'decrypt_message_list' + str(i)
if len((vars()[s])) > 0:
decrypt_message = decrypt_message + (vars()[s])[0]
del (vars()[s])[0]
else:
pass
print(decrypt_message)
else:
print("Input Error!")