Binaes
1 # 仿AES对明文进行加密
2 class Encrypt(object):
3 def __init__(self, raw_string):
4 self.raw_string = raw_string
5 def str_to_bin(self,raw_string):
6 bin_list = []
7 if isinstance(raw_string,str):
8 for i in raw_string:
9 ten = ord(i)
10 bin_ = bin(ten)
11 bin_list.append(bin_)
12 return bin_list
13
14 def to_32_length(self,bin_list):
15 bin32 = []
16 if isinstance(bin_list,list):
17 for i in bin_list:
18 raw_length = len(i[2:])
19 repair_lenght = 32-raw_length
20 repair_bin = '0'*repair_lenght+i[2:]
21 bin32.append(repair_bin)
22 return bin32
23
24 def xor(self,bin32_list):
25 xor_bin32 = []
26 xorb = lambda x,y:''.join([str(int(x[i])^int(y[i])) for i in range(32)])
27 if isinstance(bin32_list,list):
28 if len(bin32_list)==1:
29 return bin32_list
30 else:
31 for n,i in enumerate(bin32_list):
32 if n==0:
33 xor_bin32.append(i)
34 else:
35 xor_bin32.append(xorb(bin32_list[n-1],bin32_list[n]))
36 return xor_bin32
37
38 def run(self):
39 bin_data = self.str_to_bin(self.raw_string)
40 bin_32 = self.to_32_length(bin_data)
41 xor_bin32 = self.xor(bin_32)
42 return ''.join(xor_bin32)
43
44 class Decrypt(object):
45 """解密"""
46 def __init__(self, enc_string):
47 self.enc_string = enc_string
48 def to_32_list(self,enc_bin_string):
49 length = len(enc_bin_string)
50 if length//32 < length/32:
51 print('数据长度不符合要求')
52 else:
53 enc_bin_list = [enc_bin_string[i*32:(i+1)*32] for i in range(length//32)]
54 return enc_bin_list
55
56 def unxor(self,xor_bin32_list):
57 unxor_list = []
58 unxorb = lambda x,y:''.join([str(int(x[i])^int(y[i])) for i in range(32)])
59 if isinstance(xor_bin32_list,list):
60 if len(xor_bin32_list)==1:
61 return xor_bin32_list
62 else:
63 for n,i in enumerate(xor_bin32_list):
64 if n==0:
65 unxor_list.append(i)
66 else:
67 unxor_list.append(unxorb(unxor_list[n-1],xor_bin32_list[n]))
68 return unxor_list
69
70 def str_to_bin(self,unxor_list):
71 bin_list = []
72 if isinstance(unxor_list,list):
73 for i in unxor_list:
74 bin_list.append('0b'+i.lstrip('0'))
75 return bin_list
76
77 def bin_to_ten(self,bin_list):
78 ten_list = []
79 if isinstance(bin_list,list):
80 for i in bin_list:
81 ten_list.append(int(i,2))
82 return ten_list
83
84 def ten_to_str(self,ten_list):
85 dec_string = []
86 if isinstance(ten_list,list):
87 for i in ten_list:
88 dec_string.append(chr(i))
89 return dec_string
90
91 def run(self):
92 enc_bin_list = self.to_32_list(self.enc_string)
93 unxor_list = self.unxor(enc_bin_list)
94 bin_list = self.str_to_bin(unxor_list)
95 ten_list = self.bin_to_ten(bin_list)
96 dec_string = self.ten_to_str(ten_list)
97 return ''.join(dec_string)
98
99 class Binaes(object):
100 """仿AES对明文进行加密"""
101 def encrypt(raw_string):
102 '''
103 encrypt功能:将明文字符串加密成二进制密文字符串
104 :raw_string 要加密的明文字符串
105 :return 返回加密后的密文字符串
106 '''
107 return Encrypt(raw_string).run()
108
109 def decrypt(enc_string):
110 '''
111 decrypt功能:将密文字符串解密成明文字符串
112 :enc_string 要解密的二进制密文字符串
113 :return 返回解密后的明文字符串
114 '''
115 return Decrypt(enc_string).run()
116
117
118 if __name__ == '__main__':
119 raw_string = '0123456789你好abc,建造犇\n'
120 result = Binaes.encrypt(raw_string)
121 print(result)
122 print(Binaes.decrypt(result))