-
Notifications
You must be signed in to change notification settings - Fork 4
/
sp_box.py
executable file
·54 lines (43 loc) · 1.51 KB
/
sp_box.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
from string import ascii_letters, digits
from random import SystemRandom
from math import ceil, log
from signal import signal, alarm, SIGALRM
#from secret import flag
flag='dctf{n88v=TaFJ=a=:Fy=VnnKF!ay=J:$7x;FvVt=}'
random = SystemRandom()
ALPHABET = ascii_letters + digits + "_!@#$%.'\"+:;<=}{"
shuffled = list(ALPHABET)
random.shuffle(shuffled)
S_box = {k : v for k, v in zip(ALPHABET, shuffled)}
def encrypt(message):
if len(message) % 2:
message += "_"
message = list(message)
print("Message:",message) ## added
rounds = int(2 * ceil(log(len(message), 2)))
print("Rounds:",rounds) ## added
for round in range(rounds):
message = [S_box[c] for c in message]
print(f"Round #{round}:",message) ## added
if round < (rounds-1):
message = [message[i] for i in range(len(message)) if i%2 == 1] + [message[i] for i in range(len(message)) if i%2 == 0]
return ''.join(message)
def play():
print("Here's the flag, please decrypt it for me:")
print(encrypt(flag))
for _ in range(150):
guess = input("> ").strip()
assert 0 < len(guess) <= 10000
if guess == flag:
print("Well done. The flag is:")
print(flag)
break
else:
print("That doesn't look right, it encrypts to this:")
print(encrypt(guess))
def timeout(a, b):
print("\nOut of time. Exiting...")
exit()
signal(SIGALRM, timeout)
alarm(5 * 60)
play()