Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jvdsn
GitHub Repository: jvdsn/crypto-attacks
Path: blob/master/attacks/rsa/lsb_oracle.py
2589 views
1
from sage.all import ZZ
2
3
4
def attack(N, e, c, oracle):
5
"""
6
Recovers the plaintext from the ciphertext using the LSB oracle (parity oracle) attack.
7
:param N: the modulus
8
:param e: the public exponent
9
:param c: the encrypted message
10
:param oracle: a function which returns the last bit of a plaintext for a given ciphertext
11
:return: the plaintext
12
"""
13
left = ZZ(0)
14
right = ZZ(N)
15
while right - left > 1:
16
c = (c * pow(2, e, N)) % N
17
if oracle(c) == 0:
18
right = (right + left) / 2
19
else:
20
left = (right + left) / 2
21
22
return int(right)
23
24