-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
371 lines (329 loc) · 11.6 KB
/
main.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
"""
:author NicolasMahn
This system decrypts and encrypts tickets or other int arrays.
For example 1 can be encrypted to a byte code like this:
gAAAAABigrWsV-vesiIvrgI0CiY-X0uDE-Ot-SkpXla8DQ9D0q2SqEhEotXi-BDe-fW1R2ZwP3cvVVWpocchg2m-Ku3PcuGlrYQC2TjX8rTvIGSNOCDufuI=
which can be decrypted with a key like this one:
msV-x2CarhEaAM5EaDPZpwupUNm6kO8Wu2SY_rpWZKQ=
"""
import json
import os
import subprocess
from cryptography.fernet import Fernet
from reportlab.lib.pagesizes import landscape, A4
from reportlab.lib.units import mm
import privat
from reportlab.pdfgen import canvas
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase import pdfmetrics
from PyPDF2 import PdfFileWriter, PdfFileReader
from io import BytesIO
import qrcode
from os.path import exists
# This int is not uploaded to GitHub, for obvious reasons.
# If you need it please contact me.
RANDOM_INT = privat.numb
decryptedNumbs = set()
W = '\033[0m' # white (normal)
R = '\033[31m' # red
G = '\033[32m' # green
O = '\033[33m' # orange
B = '\033[34m' # blue
P = '\033[35m' # purple
def makeTickets(result):
"""
This methode creates a pdf of tickets
"""
makeFront(result)
makeBack(result)
def makeBack(result):
"""
This method makes the back of the pdf tickets
"""
buffer = BytesIO()
pdfWriter = canvas.Canvas(buffer)
pdfWriter.setTitle("Ententickets Rückseite")
pdfWriter.setPageSize(landscape(A4))
i = 1000000
max = 0
keys = result.keys()
for key in keys:
if i > key:
i = key
if max < key:
max = key
p = 0
while i <= max:
if i <= max:
pdfWriter.drawCentredString(255 * mm, 140 * mm, f"{i:04d}")
qrCode = qrcode.make(result.get(i))
qrCode.save(f'QR_Code{i}.png')
pdfWriter.drawImage(f'QR_Code{i}.png', 230 * mm, 144 * mm, 50 * mm, 50 * mm)
os.remove(f"QR_Code{i}.png")
i += 1
if i <= max:
pdfWriter.drawCentredString(165 * mm, 140 * mm, f"{i:04d}")
qrCode = qrcode.make(result.get(i))
qrCode.save(f'QR_Code{i}.png')
pdfWriter.drawImage(f'QR_Code{i}.png', 140 * mm, 144 * mm, 50 * mm, 50 * mm)
os.remove(f"QR_Code{i}.png")
i += 1
if i <= max:
pdfWriter.drawCentredString(75 * mm, 140 * mm, f"{i:04d}")
qrCode = qrcode.make(result.get(i))
qrCode.save(f'QR_Code{i}.png')
pdfWriter.drawImage(f'QR_Code{i}.png', 50 * mm, 144 * mm, 50 * mm, 50 * mm)
os.remove(f"QR_Code{i}.png")
i += 1
if i <= max:
pdfWriter.drawCentredString(255 * mm, 80 * mm, f"{i:04d}")
qrCode = qrcode.make(result.get(i))
qrCode.save(f'QR_Code{i}.png')
pdfWriter.drawImage(f'QR_Code{i}.png', 230 * mm, 84 * mm, 50 * mm, 50 * mm)
os.remove(f"QR_Code{i}.png")
i += 1
if i <= max:
pdfWriter.drawCentredString(165 * mm, 80 * mm, f"{i:04d}")
qrCode = qrcode.make(result.get(i))
qrCode.save(f'QR_Code{i}.png')
pdfWriter.drawImage(f'QR_Code{i}.png', 140 * mm, 84 * mm, 50 * mm, 50 * mm)
os.remove(f"QR_Code{i}.png")
i += 1
if i <= max:
pdfWriter.drawCentredString(75 * mm, 80 * mm, f"{i:04d}")
qrCode = qrcode.make(result.get(i))
qrCode.save(f'QR_Code{i}.png')
pdfWriter.drawImage(f'QR_Code{i}.png', 50 * mm, 84 * mm, 50 * mm, 50 * mm)
os.remove(f"QR_Code{i}.png")
i += 1
if i <= max:
pdfWriter.drawCentredString(255 * mm, 20 * mm, f"{i:04d}")
qrCode = qrcode.make(result.get(i))
qrCode.save(f'QR_Code{i}.png')
pdfWriter.drawImage(f'QR_Code{i}.png', 230 * mm, 24 * mm, 50 * mm, 50 * mm)
os.remove(f"QR_Code{i}.png")
i += 1
if i <= max:
pdfWriter.drawCentredString(165 * mm, 20 * mm, f"{i:04d}")
qrCode = qrcode.make(result.get(i))
qrCode.save(f'QR_Code{i}.png')
pdfWriter.drawImage(f'QR_Code{i}.png', 140 * mm, 24 * mm, 50 * mm, 50 * mm)
os.remove(f"QR_Code{i}.png")
i += 1
if i <= max:
pdfWriter.drawCentredString(75 * mm, 20 * mm, f"{i:04d}")
qrCode = qrcode.make(result.get(i))
qrCode.save(f'QR_Code{i}.png')
pdfWriter.drawImage(f'QR_Code{i}.png', 50 * mm, 24 * mm, 50 * mm, 50 * mm)
os.remove(f"QR_Code{i}.png")
pdfWriter.showPage()
p += 1
i += 1
pdfWriter.save()
buffer.seek(0)
pdfNew = PdfFileReader(buffer)
tmp = buffer.getvalue()
open('tmp.pdf', 'wb').write(tmp)
background = PdfFileReader(open("Ententickets_Rueckseite_Vorlage.pdf", "rb"))
pdf = PdfFileWriter()
i = 0
while p > i:
page = pdfNew.getPage(i)
page.mergePage(background.getPage(0))
pdf.addPage(page)
i += 1
outputStream = open("output/Ententickets_Rueckseite.pdf", "wb")
pdf.write(outputStream)
outputStream.close()
os.remove("tmp.pdf")
def makeFront(result):
"""
This method makes the front of the pdf tickets
"""
buffer = BytesIO()
pdfWriter = canvas.Canvas(buffer)
pdfWriter.setTitle("Ententickets Vorderseite")
pdfWriter.setPageSize(landscape(A4))
i = 1000000
max = 0
keys = result.keys()
for key in keys:
if i > key:
i = key
if max < key:
max = key
p = 0
while i <= max:
pdfmetrics.registerFont(TTFont('an', 'Arial Narrow.ttf'))
pdfWriter.setFont("an", 11)
pdfWriter.setFillColorRGB(0.4375, 0.4414, 0.4492)
pdfWriter.drawCentredString(50 * mm, 165.2 * mm, f"{i:04d}")
i += 1
pdfWriter.drawCentredString(140 * mm, 165.2 * mm, f"{i:04d}")
i += 1
pdfWriter.drawCentredString(230 * mm, 165.2 * mm, f"{i:04d}")
i += 1
pdfWriter.drawCentredString(50 * mm, 105.2 * mm, f"{i:04d}")
i += 1
pdfWriter.drawCentredString(140 * mm, 105.2 * mm, f"{i:04d}")
i += 1
pdfWriter.drawCentredString(230 * mm, 105.2 * mm, f"{i:04d}")
i += 1
pdfWriter.drawCentredString(50 * mm, 45.2 * mm, f"{i:04d}")
i += 1
pdfWriter.drawCentredString(140 * mm, 45.2 * mm, f"{i:04d}")
i += 1
pdfWriter.drawCentredString(230 * mm, 45.2 * mm, f"{i:04d}")
pdfWriter.showPage()
p += 1
i += 1
pdfWriter.save()
buffer.seek(0)
pdfNew = PdfFileReader(buffer)
tmp = buffer.getvalue()
open('tmp.pdf', 'wb').write(tmp)
background = PdfFileReader(open("Ententickets_Vorderseite_Vorlage.pdf", "rb"))
pdf = PdfFileWriter()
i = 0
while p > i:
page = pdfNew.getPage(i)
page.mergePage(background.getPage(0))
pdf.addPage(page)
i += 1
outputStream = open("output/Ententickets_Vorderseite.pdf", "wb")
pdf.write(outputStream)
outputStream.close()
os.remove("tmp.pdf")
def encrypt():
"""
This methode encrypts int arrays
"""
save = False
print("Would you like to save the encoded Numbers and the Key (Y/n)?")
print("This is highly recommended!")
answer = input()
if answer.lower()[:1] != 'n':
save = True
print("From what natural Number to what natural Number would you like to encrypt?")
first = input("Starting Number (include): ")
last = input("Last Number (include): ")
print(f"Ok, creating encrypted Integers from [{first},{last}]")
if first.isnumeric() and last.isnumeric():
first = int(first)
last = int(last)
if last >= first > 0:
result = dict()
numbs = [*range(first, last + 1, 1)]
print("Would you like to use a pre defined key (not recommended) (y/N)")
answer = input()
if answer.lower()[:1] == 'y':
try:
key = input("Key: ")
if key.lower()[:1] == 'p':
key = privat.key
except Exception:
print("Dude, Like an accepted key, not this bs")
else:
key = Fernet.generate_key()
print(key)
fernet = Fernet(key)
key = str(key)
for numb in numbs:
encNumb = fernet.encrypt(str(RANDOM_INT * numb).encode())
encNumb = str(encNumb)
print(f"{numb} has been encrypted to {encNumb} with the Key: {key}")
result[numb] = encNumb
if save:
with open("output/Tickets.json", 'w') as f:
json.dump(result, f, indent=3)
f.close()
with open("output/Key.txt", 'w') as f:
f.write(key)
f.close()
print("\nThe Result and Key has been saved to Tickets.json and Key.txt")
# print("You'll find them in: %ProgramData%/Dencrypt/output")
print("\nShould I directly create tickets with corresponding numbers and QR codes (Y/n)?")
answer = input()
if answer.lower()[:1] != 'n':
makeTickets(result)
subprocess.Popen(f'explorer "output"')
else:
print("Dude, you had one Job. I think you should go back to primary school!")
else:
print("Dude, WTF do you know what numbers are?")
menu()
def saveNumb(numb):
"""
This method saves the decrypted numbs to a result json so when they are scanned again, the system knows it
"""
global decryptedNumbs
decryptedNumbs.add(numb)
if exists(f"results.json"):
file = open(f"results.json")
results = json.load(file)
file.close()
else:
results = list()
results.append(numb)
if len(results) > 0:
with open(f"results.json", "w") as outfile:
outfile.write(json.dumps(results))
def decrypt():
"""
This methode decrypts encNumbers with the help of a key
"""
global decryptedNumbs
if exists(f"results.json"):
file = open(f"results.json")
results = json.load(file)
file.close()
for r in results:
decryptedNumbs.add(r)
sameKey = True
print("Please enter the Key")
key = input()
if key.lower()[:1] == 'p':
key = privat.key
try:
fernet = Fernet(key)
except Exception:
print("Dude, this key isn't valid")
sameKey = False
while sameKey:
print("Please enter the encrypted Number or type stop to stop")
encNumb = input()
if encNumb.lower()[:4] == 'stop':
break
if len(encNumb) == 123:
encNumb = encNumb[2:122]
try:
numb = int(int(fernet.decrypt(bytes(encNumb, encoding="UTF-8"))) / RANDOM_INT)
# print("Would you like to continue with the same Key (Y/n)?")
if numb in decryptedNumbs:
print(f"{O}{numb} is a Duplicate, this Ticket was already scanned{W}")
else:
print(f"{G}The decrypted Number is {numb}{W}")
saveNumb(numb)
except Exception:
print(f"{R}The encrypted Number isn't valid!{W}")
menu()
def menu():
"""
This methode handles the menu
"""
print("\n")
answer = input("Do you want to decrypt, encrypt or exit (D/e/x)?\n")
if answer.lower()[:1] == 'e':
encrypt()
elif answer.lower()[:1] == 'x':
pass
else:
decrypt()
def main():
"""
this method initialises the program, and then opens the menu
"""
print("This System encrypts and decrypts Tickets.")
menu()
if __name__ == '__main__':
main()