-
Notifications
You must be signed in to change notification settings - Fork 2
/
problem.hard.py
56 lines (48 loc) · 1.86 KB
/
problem.hard.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
# Python Version: 3.5.2
# -*- coding: utf-8 -*-
from Crypto.Util.number import *
from gmpy2 import *
from flag import FLAG2 as FLAG
from random import *
from signal import *
def main():
signal(SIGALRM, lambda: None)
alarm(60)
print('[*] 🎉 WELCOME TO HAREKAZE-CTF LOTTERY CHALLENGE 🎉')
print('[*] to ensure fairness, we use a public-key 🔑 cryptosystem 🔒')
win = 0
for round_ in range(1, 10000):
print()
print('[*] ROUND %d' % round_)
# generate params
p = [ getPrime(1024) for _ in range(3) ]
q = [ getPrime(1024) for _ in range(3) ]
n = [ p[i] * q[i] for i in range(3) ]
e = 65537
d = [ invert(e, (p[i] - 1) * (q[i] - 1)) for i in range(3) ]
# m = [ bytes_to_long(['WIN 💎', 'LOSE 💩'][bool(i)].encode()) for i in range(3) ]
m = [ bytes_to_long(['WIN 💎', 'LOSE 💩'][bool(i)].encode() + randrange(256 ** 200 - 1).to_bytes(200, 'big')) for i in range(3) ]
c = [ pow(m[i], e, n[i]) for i in range(3) ]
f = sorted(range(3), key=lambda x: c[x])
# receive the choice
for x, i in enumerate(f):
print(' 🔒 %d. (%#x, %d, %#x)' % (x + 1, n[i], e, c[i]))
print('[*] select:')
k = f[int(input('>>> ')) - 1]
# check the result
result = long_to_bytes(pow(c[k], d[k], n[k]))
print('[*] result:', result)
if 'WIN 💎'.encode() in result:
print('[+] you win 🎉')
win += 1
else:
print('[!] you lose')
# send the witnesses
print('[*] here are the keys. please ensure that there is no cheating')
for i in range(3):
print(' 🔑 %d. %d' % (i + 1, d[i]))
if round_ >= 30 and win / round_ >= 0.9:
print('[+] you got the flag 🏁:', FLAG)
break
if __name__ == '__main__':
main()