You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Alice and Bob publicly agree to use a modulus p and base g, where p is prime and g is a primitive root modulo p
Alice chooses a secret integer a
Alice calculates her public key A = g^a % p and sends to Bob
Bob chooses a secret integer b
Bob calculates his public key B = g^b % p and sends to Alice
Alice computes s = B^a % p
Bob computes s = A^b % p
Alice and Bob encrypt/decrypt the message by XORing with s
#!/usr/bin/env python3importsysimportstringimportbase64frompwnimport*fromCrypto.Random.randomimportgetrandbitsfromCrypto.Util.strxorimportstrxordefshow(name, value, *, b64=True):
log.info(f"{name}: {value}")
defshow_hex(name, value):
show(name, hex(value))
# Using pwntools to run the challenge processrun=process(b"/challenge/run")
# Receive the agreed upon modulus p from Alicerun.recvuntil(b"p: ")
pstr=run.recvline().strip();
p=int(pstr, 16)
show_hex("p", p)
# Receive the agreed upon base g from Alicerun.recvuntil(b"g: ")
gstr=run.recvline().strip();
g=int(gstr, 16)
show_hex("g", g)
# Receive Alice's public key Arun.recvuntil(b"A: ")
Astr=run.recvline().strip();
A=int(Astr, 16)
show_hex("A", A)
# Bob chooses a secret key bb=getrandbits(2048)
# Bob calculates his public key B = g^b % pB=pow(g, b, p)
show_hex("B", b)
# Bob sends his public key B to Alicerun.recvuntil(b"B: ")
run.sendline(hex(B))
# Alice encrypts the data using Bob's public# key B and her private key a, then sends the# resulting encrypted text base64 encodedrun.recvuntil(b"secret ciphertext (b64): ")
secretb64=run.recvline().strip();
secret=base64.b64decode(secretb64)
# Bob computes his s and uses it to decrypt the messages=pow(A, b, p)
key=s.to_bytes(256, "little")
plaintext=strxor(secret, key[:len(secret)])
log.info(plaintext)