Repository navigation
Expand file tree
/
Copy pathrepeatingoffense.py
More file actions
245 lines (155 loc) · 6.67 KB
/
Copy pathrepeatingoffense.py
File metadata and controls
245 lines (155 loc) · 6.67 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
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
#!/usr/bin/env python3
#
# Polymero
#
# Imports
from Crypto.Util.number import getPrime, inverse, GCD
from secrets import randbelow
import os, hashlib
# Local imports
with open('flag.txt','rb') as f:
FLAG = f.read().decode().strip()
f.close()
HDR = r"""|
| _______ _______ _______ _______ __ ___________ __ _____ ___ _______
| /" \ /" "| | __ "\ /" "| /""\(" _ ")|" \ (\" \|" \ /" _ "|
| |: |(: ______) (. |__) :)(: ______) / \)__/ \\__/ || | |.\\ \ |(: ( \___)
| |_____/ ) \/ | |: ____/ \/ | /' /\ \ \\_ / |: | |: \. \\ | \/ \
| // / // ___)_ (| / // ___)_ // __' \ |. | |. | |. \ \. | // \ ___
| |: __ \ (: "| /|__/ \ (: "| / / \\ \\: | /\ |\| \ \ |(: _( _|
| |__| \___) \_______)(_______) \_______)(___/ \___)\__| (__\_|_)\___|\____\) \_______)
| ______ _______ _______ _______ _____ ___ ________ _______
| / " \ /" "| /" "| /" "|(\" \|" \ /" )/" "|
| // ____ \(: ______)(: ______)(: ______)|.\\ \ |(: \___/(: ______)
| / / ) :)\/ | \/ | \/ | |: \. \\ | \___ \ \/ |
| (: (____/ // // ___) // ___) // ___)_ |. \ \. | __/ \\ // ___)_
| \ / (: ( (: ( (: "|| \ \ | /" \ :)(: "|
| \"_____/ \__/ \__/ \_______) \___|\____\)(_______/ \_______)
|"""
print(HDR)
class RSA_then_Paillier:
""" Layered Cipher of RSA : Zn -> Zn then Paillier : Zn -> Zn2. """
def __init__(self, domain: tuple):
# Class parameters
P, Q = domain
self.HISTORY = []
# RSA public key
self.E = 0x10001
self.N = P * Q
# RSA private key
F = (P - 1) * (Q - 1)
self.D = inverse(self.E, F)
# Paillier public key
self.G = randbelow(self.N * self.N)
# Paillier private key
self.L = F // GCD(P - 1, Q - 1)
self.U = inverse((pow(self.G, self.L, self.N * self.N) - 1) // self.N, self.N)
def encrypt(self, msg: int) -> int:
# RSA
cip_rsa = pow(msg, self.E, self.N)
# Paillier
g_m = pow(self.G, cip_rsa, self.N * self.N)
r_n = pow(randbelow(self.N), self.N, self.N * self.N)
cip = (g_m * r_n) % (self.N * self.N)
# Update HISTORY
self.HISTORY += [hashlib.sha256(cip.to_bytes(256, 'big')).hexdigest()]
return cip
def decrypt(self, cip: int) -> int:
# Check HISTORY
if hashlib.sha256(cip.to_bytes(256, 'big')).hexdigest() in self.HISTORY:
return -1
# Paillier
cip_rsa = ((pow(cip, self.L, self.N * self.N) - 1) // self.N * self.U) % self.N
# RSA
return pow(cip_rsa, self.D, self.N)
class Paillier_then_RSA:
""" Layered Cipher of Paillier : Zn -> Zn2 then RSA : Zn2 -> Zn2. """
def __init__(self, domain: tuple):
# Class parameters
P, Q = domain
self.HISTORY = []
# RSA public key
self.E = 0x10001
self.N = P * Q
# RSA private key
F = (P - 1) * (Q - 1)
self.D = inverse(self.E, F * self.N)
# Paillier public key
self.G = randbelow(self.N * self.N)
# Paillier private key
self.L = F // GCD(P - 1, Q - 1)
self.U = inverse((pow(self.G, self.L, self.N * self.N) - 1) // self.N, self.N)
def encrypt(self, msg: int) -> int:
# Paillier
g_m = pow(self.G, msg, self.N * self.N)
r_n = pow(randbelow(self.N), self.N, self.N * self.N)
cip_pai = (g_m * r_n) % (self.N * self.N)
# RSA
cip = pow(cip_pai, self.E, self.N * self.N)
# Update HISTORY
self.HISTORY += [hashlib.sha256(cip.to_bytes(256, 'big')).hexdigest()]
return cip
def decrypt(self, cip: int) -> int:
# Check HISTORY
if hashlib.sha256(cip.to_bytes(256, 'big')).hexdigest() in self.HISTORY:
return -1
# RSA
cip_pai = pow(cip, self.D, self.N * self.N)
# Paillier
return ((pow(cip_pai, self.L, self.N * self.N) - 1) // self.N * self.U) % self.N
DOMAIN = [getPrime(512) for _ in range(2)]
STAGE_1, STAGE_2 = True, False
RTP = RSA_then_Paillier(DOMAIN)
print("|\n| STAGE 1 :: RSA-then-Paillier\n|\n| N: {}\n| G: {}".format(RTP.N, RTP.G))
RTP_PWD = int.from_bytes(os.urandom(32).hex().encode(), 'big')
print("|\n| RTP(Password): {}".format(RTP.encrypt(RTP_PWD)))
while STAGE_1:
try:
print("|\n|\n| MENU:\n| [E]ncrypt\n| [D]ecrypt\n| [S]ubmit Password")
choice = input("|\n| >>> ").lower()
if choice == 'e':
user_input = input("|\n| MSG(int): ")
print("|\n| CIP ->", RTP.encrypt(int(user_input)))
elif choice == 'd':
user_input = input("|\n| CIP(int): ")
print("|\n| MSG ->", RTP.decrypt(int(user_input)))
elif choice == 's':
user_input = input("|\n| PWD(int): ")
if user_input == str(RTP_PWD):
print("|\n|\n| Correct ~ On to Stage 2!\n|")
STAGE_2 = True
break
else:
print("|\n| ERROR -- Unknown command.")
except (KeyboardInterrupt, EOFError):
print("\n|\n|\n| Cya ~\n|")
break
except:
print("|\n| ERROR -- Something went wrong.")
if STAGE_2:
PTR = Paillier_then_RSA(DOMAIN)
print("|\n| STAGE 2 :: Paillier-then-RSA\n| N: {}\n| G: {}\n|".format(PTR.N, PTR.G))
PTR_PWD = int.from_bytes(os.urandom(32).hex().encode(), 'big')
print("|\n| PTR(Password): {}".format(PTR.encrypt(PTR_PWD)))
while STAGE_2:
try:
print("|\n|\n| MENU:\n| [E]ncrypt\n| [D]ecrypt\n| [S]ubmit Password")
choice = input("|\n| >>> ").lower()
if choice == 'e':
user_input = input("|\n| MSG(int): ")
print("|\n| CIP ->", PTR.encrypt(int(user_input)))
elif choice == 'd':
user_input = input("|\n| CIP(int): ")
print("|\n| MSG ->", PTR.decrypt(int(user_input)))
elif choice == 's':
user_input = input("|\n| PWD(int): ")
if user_input == str(PTR_PWD):
print("|\n|\n| Correct ~ Here's your flag: {}\n|".format(FLAG))
break
else:
print("|\n| ERROR -- Unknown command.")
except (KeyboardInterrupt, EOFError):
print("\n|\n|\n| Cya ~\n|")
break
except:
print("|\n| ERROR -- Something went wrong.")