-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcipher.py
More file actions
185 lines (171 loc) · 5.33 KB
/
cipher.py
File metadata and controls
185 lines (171 loc) · 5.33 KB
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
import string
import sys
def main():
input_text:str = str(sys.argv[1]) if len(sys.argv) > 1 else "Seems like the code is working"
print_steps:bool = bool(sys.argv[2]) if len(sys.argv) > 2 else False
encrypted:str = charstring_zlib(bin_to_chars(make_fake_binary(split_text(input_text.lower())), "X", "Y"))
print("Input: ", input_text)
print("Encrypted: ", encrypted)
print("Decrypted: ", decrypt(encrypted))
def split_text(text:string) -> list:
text_list:list = []
for i in range(text.__len__()):
text_list.append(text[i])
return text_list
def make_fake_binary(text_list:list) -> list:
alphabet = string.ascii_lowercase + " "
mapping = {
letter: format(i, "05b")
for i, letter in enumerate(alphabet)}
binary_list:list = []
for l in text_list:
binary_list.append(mapping[l])
return binary_list
def bin_to_chars(bin_list:list, c1:string, c2:string) -> string:
combined = "".join(bin_list)
last_letter:string = "0"
count:int = 0
result:string = ""
for i in combined:
if (i == last_letter):
count += 1
else:
char = c1 if last_letter == "0" else c2
count_str = "" if count == 1 else str(count)
result += count_str + char
count = 1
last_letter = i
char = c1 if last_letter == "0" else c2
count_str = "" if count == 1 else str(count)
result += count_str + char
return result
def charstring_zlib(text:string) -> string:
mapping = {
"XY": "A",
"YX": "B",
"YXY": "C",
"XYX": "D",
"3Y": "E",
"2Y": "F",
"2X": "G",
"3X": "H",
"4X": "I",
"5X": "J",
}
result:string = ""
latest_pattern:string = ""
for i in range(text.__len__()):
latest_pattern += text[i]
if latest_pattern in mapping.keys():
result += mapping[latest_pattern]
latest_pattern = ""
else:
for n in range(latest_pattern.__len__() - 2):
p = latest_pattern[n+1:]
if p in mapping.keys():
result += latest_pattern.removesuffix(p)
result += mapping[p]
latest_pattern = ""
return result
# def decrypt(encrypted: str) -> str:
# mapping = {
# "XY": "A",
# "YX": "B",
# "YXY": "C",
# "XYX": "D",
# "3Y": "E",
# "2Y": "F",
# "2X": "G",
# "3X": "H",
# "4X": "I",
# "5X": "J",
# }
# decrypted:str = ""
# for i in range(len(encrypted)):
# ch = encrypted[i]
# if ch in mapping.values():
# decrypted += list(mapping.keys())[list(mapping.values()).index(ch)]
# else:
# decrypted += ch
# encrypted = decrypted
# decrypted = ""
# while len(encrypted) > 0:
# # if encrypted[0].isdigit():
# # count = int(encrypted[0])
# # char = encrypted[1]
# # decrypted += char * count
# # encrypted = encrypted[2:]
# if encrypted[0].isdigit():
# j = 0
# while j < len(encrypted) and encrypted[j].isdigit():
# j += 1
# count = int(encrypted[:j])
# char = encrypted[j]
# decrypted += char * count
# encrypted = encrypted[j+1:]
# else:
# decrypted += encrypted[0]
# encrypted = encrypted[1:]
# result = ""
# for ch in decrypted:
# if ch == "X":
# result += "0"
# else:
# result += "1"
# decrypted = result
# alphabet = string.ascii_lowercase + " "
# mapping = {format(i, "05b"): letter for i, letter in enumerate(alphabet)}
# chunks = [decrypted[i:i+5] for i in range(0, len(decrypted), 5)]
# return "".join(mapping.get(chunk, "?") for chunk in chunks)
def decrypt(encrypted: str) -> str:
mapping = {
"XY": "A",
"YX": "B",
"YXY": "C",
"XYX": "D",
"3Y": "E",
"2Y": "F",
"2X": "G",
"3X": "H",
"4X": "I",
"5X": "J",
}
# 1) Undo charstring_zlib (A..J -> XY / YX / etc.)
decrypted = ""
for ch in encrypted:
if ch in mapping.values():
decrypted += list(mapping.keys())[list(mapping.values()).index(ch)]
else:
decrypted += ch
# 2) RLE decode with multi-digit counts
encrypted = decrypted
decrypted = ""
while len(encrypted) > 0:
if encrypted[0].isdigit():
j = 0
while j < len(encrypted) and encrypted[j].isdigit():
j += 1
count = int(encrypted[:j])
char = encrypted[j]
decrypted += char * count
encrypted = encrypted[j+1:]
else:
decrypted += encrypted[0]
encrypted = encrypted[1:]
# 3) Map X/Y -> 0/1
result = ""
for ch in decrypted:
if ch == "X":
result += "0"
else:
result += "1"
decrypted = result
# 4) Trim to full 5-bit chunks only
usable_len = (len(decrypted) // 5) * 5
decrypted = decrypted[:usable_len]
# 5) 5-bit -> letters
alphabet = string.ascii_lowercase + " "
mapping = {format(i, "05b"): letter for i, letter in enumerate(alphabet)}
chunks = [decrypted[i:i+5] for i in range(0, len(decrypted), 5)]
return "".join(mapping[chunk] for chunk in chunks)
main()