-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMAS.py
399 lines (352 loc) · 11.9 KB
/
MAS.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import random
from itertools import chain
# random.seed(9)
class MAS:
# Attributes
__A = []
__B = []
__Code = {}
__X = {}
__S = 0b0 # Mask bit string
__k = 0 # unambiguous degree of Language set
__m = 0 # bit length of S
__word_bit = 0 # bit length of code element
__Padding_word = '' # word for pading
# Constructor
def __init__(self, A, B, Code, X, S, k, PaddingWord):
self.__A = A
self.__B = B
self.__Code = Code
self.__X = X
self.__S = S
self.__k = k
self.__m = len(self.__S)
self.__word_bit = len(list(self.__Code.values())[0])
self.__Padding_word = self.__B.index(PaddingWord)
print("MAS cryptosystem initialization successful!")
print("Author: Thanh HoangVan")
print("Github: thanhhoangvan")
print("+-----------------------------------------+")
# Methods
def __Msg2Index(self, Msg, Dictionary) -> list:
"""
Convert index of word to message by dictionary list
---
Parameters:
- Dictionary: List of all word
- Msg: Message
---
Return:
- List of index word in message
---
Example
- Dictionary: ['c', 'a1', 'a2', 'a3', 'b1', 'b2', 'b3', 'b4']
- Message: "a1a2a3b1"
- Return: [1, 2, 3, 4]
"""
if len(Msg) == 0:
raise Exception("Message is empty!")
for i in Dictionary:
Msg = Msg.replace(i, str(Dictionary.index(i)))
return [int(i) for i in Msg]
def __Index2Msg(self, WordIndex, Dictionary) -> str:
"""
Convert index of word to message by dictionary list
---
Parameters:
- WordIndex: List of index word
- Dictionary: List of all word
---
Return:
- Message string
---
Example
- Dictionary: ['c', 'a1', 'a2', 'a3', 'b1', 'b2', 'b3', 'b4']
- WordIndex: [1, 2, 3, 4]
- Return: "a1a2a3b1"
"""
Msg = ""
for i in WordIndex:
Msg = Msg + Dictionary[i]
return Msg
def __PlainText2Index(self, Msg='') -> list:
"""
Split the words in the Message into a list of index this word in A
---
Parameter:
- Msg: a message(string)
---
Retrun:
- List of words
---
Example:
>>> A = ['u1', 'u2', 'u3']
>>> __Msg2Index("u1u3u2u3") -> [0, 2, 1, 2]
"""
if len(Msg) == 0:
raise Exception("Message is empty!")
for i in self.__A:
Msg = Msg.replace(i, str(self.__A.index(i)))
return [int(i) for i in Msg]
def __EncodedMsg2Index(self, EMsg='') -> list:
"""
"""
if len(msg) == 0:
raise Exception("Encoded message is empty!")
for i in self.__B:
EMsg = EMsg.replace(i, str(self.__B.index(i)))
return [int(i) for i in EMsg] # split index number to list
def __EncodedMsg2Binary(self, EMsg='') -> bytes:
"""
Convert encoded message to binary result
---
Parameter:
- EMsg: String of encoded message(String)
---
Result:
- bytes string of result
---
Example
- Encoded message: 'ca1b1a2'
with {'c': b'110', 'a1':b'001', 'a2':b'010', 'b1':b'100'}
- Result: b'110001100010'
"""
BinaryResult = b''
if len(msg) == 0:
raise Exception("Encoded message is empty!")
word_index = self.__EncodedMsg2Index(EMsg)
EMsg = [self.__B[i] for i in word_index] # split index number to list
for i in EMsg:
temp = self.__Code[i]
BinaryResult = BinaryResult + temp
return BinaryResult
def __NormalizeLanguage(self) -> list:
"""
Convet all word in X to list of index word
---
Example
---
- X1 = {'c'}
- X2 = {'ca1', 'a1b1'}
- X3 = {'b1a2', 'ca1a3b1'}
- X4 = {'a2b2'}
- X5 = {'b2a3', 'a3'}
- X = [X1, X2, X3, X4, X5]
=> Result: X = [[[0]], [[0, 1], [1, 4]], [[0, 1, 3, 4], [4, 2]], [[2, 5]], [[3], [5, 3]]]
"""
Normlized = []
for i in self.__X:
temp = []
for word in i:
temp.append(self.__Msg2Index(word, self.__B))
Normlized.append(temp)
return Normlized
def __PADDING(self, EMsg) -> list:
"""
Hàm đệm bit vào chuỗi
"""
while len(EMsg)*self.__word_bit < self.__m:
EMsg.append(self.__Padding_word)
return EMsg
def __UNPADDING(self, EMsg) -> list:
"""
"""
while self.__Padding_word in EMsg:
EMsg.remove(self.__Padding_word)
return EMsg
def __EXTRACT(self, Encoded_Word) -> list:
"""
The function parses a list of letters and splits them into words
---
Parameter:
- Encoded_Word: List of all word in Encoded Message
---
Result:
- List of corresponding plaintext words
---
Example:
---
- Encoded_Word
[[0, 0, 1, 3, 4, 3],
[4, 2, 2, 5, 5, 3],
[0, 1, 0, 7, 7, 7],
[4, 2, 5, 3, 7, 7]]
- Result:
[[0], [0, 1, 3, 4], [3]]
[[4, 2], [2, 5], [5, 3]]
[[0, 1], [0]]
[[4, 2], [5, 3]]
"""
Language = list(chain.from_iterable(self.__NormalizeLanguage())) # List of all word in language [[0], [0, 1], [1, 4], [0, 1, 3, 4], [4, 2], [2, 5], [5, 3], [3]]
TMP = []
Solution = []
W = []
def FIND(msg, len_msg, k):
nonlocal W, TMP, Solution
for i in Language:
if len(W) + len(i) <= len_msg:
W = W + i
Solution = Solution + [i]
else:
continue
if (len(W) == len_msg) and (W == msg):
TMP = TMP + [list(Solution)]
else:
FIND(msg, len_msg, k+1)
W = W[:-len(i)]
Solution.pop()
for i in Encoded_Word:
ListChars = self.__UNPADDING(i)
n = len(ListChars)
W = []
Solution = []
FIND(i, n, 1)
return TMP
def MASKING(self, EMsg=b'') -> bytes:
"""
Hàm thêm lớp mặt nạ vào bit string đầu ra
"""
n = len(EMsg)
m = len(self.__S)
k = n//m
Mask = self.__S*k + self.__S[:(n-m*k)]
return Mask
def __e_g(self, k, Language) -> int:
"""
Encode function
---
Parameter:
- k: index of Language
- Language: List of Language
---
Return
---
- A random element corresponding to the index k
"""
return random.choice(Language[k])
def __d_g(self) -> list:
"""
"""
pass
def __Encode(self, Msg="") -> bytes:
"""
Encryption function
---
Parameters
---
- Msg: Messages to be encrypted
Return
---
- BitResult: The binary code of the encrypted message
Example
---
"""
# Check null message exception
if len(Msg) == 0:
raise Exception("Input message is empty!")
Language = self.__NormalizeLanguage() # [[[0]], [[1, 4], [0, 1]], [[0, 1, 3, 4], [4, 2]], [[2, 5]], [[3], [5, 3]]]
wordIndex = self.__PlainText2Index(Msg) # [0, 2, 4, 2, 3, 4, 1, 0, 2, 4]
# initialization
W = [] # list of all encode word in encoded msg
i, j = 0, 0 # i_current msg word, j_current block bit of encode word(encoded msg)
n = len(wordIndex) # number of word in message
# main cryptosystem
while True:
#============================================================
if (i >= n):
break # break first while loop
count = 0
temp = [] # Current encoded word
while True:
if (count >= self.__k) or (len(temp)*self.__word_bit == self.__m): # Kiểm tra điều kiện độ dài từ W_j < m
if len(temp)*self.__word_bit < self.__m:
temp = self.__PADDING(temp)
# Masking
# if j == 1:
# pass
# else:
# pass
W.append(temp)
j = j + 1
break
#============================================================
try:
newEncodedWord = self.__e_g(wordIndex[i], Language)
if len(temp + newEncodedWord)*self.__word_bit <= self.__m:
temp = temp + newEncodedWord
count = count + 1
i = i + 1
else:
temp = self.__PADDING(temp)
except:
if len(temp)*self.__word_bit < self.__m:
temp = self.__PADDING(temp)
# Masking
# if j == 1:
# pass
# else:
# pass
W.append(temp)
print('-'*20)
j = j + 1
break
# if len(temp + newEncodedWord)*self.__word_bit <= self.__m:
# temp = temp + newEncodedWord
# count = count + 1
# i = i + 1
# else:
# temp = self.__PADDING(temp)
#============================================================
print(count, self.__k)
print(temp, '\n')
print('='*20)
return W
def Decode(self, EMsg=[]) -> str:
"""
Decryption function
---
Parameters
---
Return
---
Example
---
"""
# Check null message exception
if len(EMsg) == 0:
raise Exception("Input message is empty!")
Msg = []
i, j = 0, 0
n_word = len(EMsg) # number of word in encoded message
while True:
# Unmasking
if j == 0:
# unmasking
pass
else:
# unmasking
pass
tmp = self.__EXTRACT(EMsg)
return tmp
if __name__ == '__main__':
# Example Initialization
A = ['u1', 'u2', 'u3', 'u4', 'u5']
B = ['c', 'a1', 'a2', 'a3', 'b1', 'b2', 'b3', 'b4']
Code = {'c': b'110', 'a1':b'001', 'a2':b'010', 'a3':b'011', 'b1':b'100', 'b2':b'101', 'b3':b'000', 'b4':b'111'}
# Language
X1 = {'c'}
X2 = {'ca1', 'a1b1'}
X3 = {'b1a2', 'ca1a3b1'}
X4 = {'a2b2'}
X5 = {'b2a3', 'a3'}
X = [X1, X2, X3, X4, X5]
paddingWord = 'b4'
k = 3
# Message
msg = 'u1u3u5u3u4u5u2u1u3u5'
emsg = [[0, 0, 1, 3, 4, 3], [4, 2, 2, 5, 5, 3], [0, 1, 0, 7, 7, 7], [4, 2, 5, 3, 7, 7]]
# Mask
S = b'101000110110101100'
#=======================================
Cryptosystem = MAS(A, B, Code, X, S, k, paddingWord)
print(Cryptosystem.Decode(emsg))