-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathchallenge46.py
More file actions
38 lines (34 loc) · 1.06 KB
/
challenge46.py
File metadata and controls
38 lines (34 loc) · 1.06 KB
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
import base64
import challenge39
pub, priv = challenge39.genKey(1024)
def parityOracle(c):
p = challenge39.decryptnum(priv, c)
return p % 2
def deducePlaintext(ciphertext, pub, parityOracle):
(e, n) = pub
low = 0
high = 1
denom = 1
c = challenge39.bytestonum(ciphertext)
k = pow(2, e, n)
for _ in range(n.bit_length()):
c = (c * k) % n
p = parityOracle(c)
d = high - low
low *= 2
high *= 2
denom *= 2
if p == 0:
high -= d
else:
low += d
hightext = challenge39.numtobytes(n * high // denom)
print(hightext)
return hightext
if __name__ == '__main__':
encodedPlaintext = b'VGhhdCdzIHdoeSBJIGZvdW5kIHlvdSBkb24ndCBwbGF5IGFyb3VuZCB3aXRoIHRoZSBGdW5reSBDb2xkIE1lZGluYQ=='
plaintext = base64.b64decode(encodedPlaintext)
ciphertext = challenge39.encryptbytes(pub, plaintext)
plaintext2 = deducePlaintext(ciphertext, pub, parityOracle)
if plaintext2 != plaintext:
raise Exception(b'Invalid plaintext ' + plaintext2)