Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ashutosh1206
GitHub Repository: ashutosh1206/crypton
Path: blob/master/Elgamal-Encryption/Challenges/Megalal/megalal.py
1402 views
1
#!/usr/bin/env python2
2
3
import binascii
4
import base64
5
import random
6
7
from secrets import g, p, x, h, flag
8
9
def xgcd(b, n):
10
x0, x1, y0, y1 = 1, 0, 0, 1
11
while n != 0:
12
q, b, n = b // n, n, b % n
13
x0, x1 = x1, x0 - q * x1
14
y0, y1 = y1, y0 - q * y1
15
return b, x0, y0
16
17
def modinv(b, n):
18
g, x, _ = xgcd(b, n)
19
if g == 1:
20
return x % n
21
22
def enc(m):
23
M = int(binascii.hexlify(m), 16)
24
assert len(bin(M)) < len(bin(p)), 'm too long'
25
26
y = random.SystemRandom().randint(0, p-1)
27
c1 = pow(g, y, p)
28
c2 = (M * pow(h, y, p) ) % p
29
30
return c1, c2
31
32
def dec(c1, c2):
33
s = pow(c1, x, p)
34
M = (c2 * modinv(s, p)) % p
35
36
return str(binascii.unhexlify('{:x}'.format(M)))
37
38
def login():
39
c = raw_input('Please input your access token: ').split('_')
40
c1 = int(c[0], 16)
41
c2 = int(c[1], 16)
42
user, role = dec(c1, c2).split('#')
43
print('\nWelcome {}!'.format(user))
44
print('Your role is \'{}\'.\n'.format(role))
45
if role == 'overlord':
46
print('Here\'s your flag: {}'.format(flag))
47
print('That\'s all, nothing else happening here.')
48
49
def register():
50
username = raw_input('Your username: ')
51
role = raw_input('Your role: ')
52
53
if role == 'overlord':
54
print('nope, you\'re not the overlord...')
55
return
56
c = enc('%s#%s' % (username, role))
57
token = '{:x}_{:x}'.format(c[0], c[1])
58
59
print('Here is your access token:\n{}'.format(token))
60
61
if __name__ == '__main__':
62
print('What do you want to do?')
63
print('[1] Login')
64
print('[2] Register')
65
choice = raw_input('> ')
66
try:
67
if choice == '1':
68
login()
69
elif choice == '2':
70
register()
71
except:
72
print('something went wrong...')
73
74