forked from akalin/cryptopals-python3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge35.py
59 lines (44 loc) · 1.6 KB
/
challenge35.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
from Crypto.Random import random
import challenge11
import socket
import sys
import challenge34_util
import util
host = sys.argv[1]
port = int(sys.argv[2])
message = sys.argv[3]
p = 0xffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca237327ffffffffffffffff
g = 2
a = random.randint(0, p)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((host, port))
util = challenge34_util.Util(sock)
print('C: writing p...')
util.writenum(p)
print('C: writing g...')
util.writenum(g)
print('C: reading p...')
negotiatedP = util.readnum()
print('C: reading g...')
negotiatedG = util.readnum()
A = pow(negotiatedG, a, negotiatedP)
print('C: writing A...')
util.writenum(A)
print('C: reading B...')
B = util.readnum()
s = pow(B, a, negotiatedP)
key = util.derivekey(s)
iv = util.randbytes(16)
encryptedMessage = util.encrypt(key, iv, message)
print('C: writing encrypted message...')
util.writebytes(encryptedMessage)
print('C: writing iv...')
util.writebytes(iv)
print('C: reading encrypted message...')
encryptedMessage2 = util.readbytes()
message2 = util.decrypt(key, iv, encryptedMessage2)
if message2 != message:
raise Exception(message2 + ' != ' + message)
finally:
sock.close()