-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendecode.py
70 lines (65 loc) · 1.49 KB
/
endecode.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
alpha=[]
##生成字母表
bet=list(map(chr, range(97, 123)))
lists=[]
##生成数字表
for num in range(0,26):
alpha.append(num)
##十进制与二进制进行转换
for num in alpha:
##把列表传递出循环
newalpha=[]
zero=0
while zero<=25:
##删除二进制转换中的多余符号
ele=bin(int(alpha[zero])).replace('0b', '')
newalpha.append(ele)
zero+=1
##合并新列表--字母表和二进制表
alphabet=dict(zip(bet,newalpha))
##你可以在此自定义其他符号
alphabet[' ']='3'
alphabet[',']='4'
##字母表键值反转,并添加对特殊符号的处理
betalpha=dict(zip(alphabet.values(),alphabet.keys()))
betalpha[' ']=' '
betalpha['']=''
##加密函数
def encode(thing):
thing=str.lower(thing)
for elements in thing:
lists.append(elements)
length=len(lists)
zero=0
before=lists[zero]
##跟上面一样,传递出列表
final=[]
while zero<=length-1:
before=lists[zero]
after=alphabet[before]
final.append(after)
zero+=1
##移除无用符号
wait=str(final).replace(',','2')
wait=wait.replace('[','')
wait=wait.replace(']','')
wait=wait.replace("'",'')
wait=wait.replace(' ','')
##提高严谨度,增加首尾的2(单词间字母的间隔)
wait='2'+wait+'2'
return wait
##解密函数
def decode(code):
##数值变换
code=code.replace('3',' ')
code=code.split('2')
zero=0
##又又一次传递列表
listsafter=[]
for i in code:
##字典对应
i=betalpha[i]
listsafter.append(i)
##合并列表中的元素
str=''
return str.join(listsafter)