-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrambled.py
92 lines (71 loc) · 2.07 KB
/
scrambled.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
from itertools import permutations
def step1(message):
message = message[1:] + message[:1]
message = message[0::2] + message[1::2]
message = message[1:] + message[:1]
return message
def unstep1(original):
original = original[len(original)-1:]+ original[:len(original)-1]
half = len(original)//2
first_half = original[:half]
second_half = original[half:]
n = ""
for i in range(half):
n += first_half[i] + second_half[i]
original = n
original = original[len(original)-1:]+ original[:len(original)-1]
return original
def step2(message, key):
res = ""
for j in range(0, len(message), W):
for k in range(W):
res += message[j:j+W][key[k]]
return res
def unstep2(message, key):
res = ""
for j in range(0, len(message), 7):
empty = [0, 0, 0, 0, 0, 0, 0]
for k in range(7):
empty[key[k]] = message[j:j+7][k]
res += ''.join(empty)
return res
def scramble(message, key):
W = len(key)
while len(message) % (2*W):
message += "#"
for _ in range(128):
message = message[1:] + message[:1]
message = message[0::2] + message[1::2]
message = message[1:] + message[:1]
res = ""
for j in range(0, len(message), W):
for k in range(W):
res += message[j:j+W][key[k]]
message = res
return message
'''
key = [1, 3, 6, 0, 2, 4, 5]
W = len(key)
message = "AcciderbolinaaAcciderbolinaaAcciderbolinaa"
print(scramble(message, key))
# Encryption
for _ in range(128):
message = step1(message)
message = step2(message, key)
print(message)
'''
encrypted = "l_4Tnb_3cnnbcg3r3slCCm4Id__gb4u}ct{0mr3sds"
key = list(range(7))
comb = [i for i in permutations(key)]
solutions = []
# Decryption
for c in comb:
message = encrypted
c = list(c)
for _ in range(128):
message = unstep2(message, c)
message = unstep1(message)
print(message)
if message[:4] == "CCIT":
solutions.append((message, c))
print(solutions)