-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathS06C46.py
60 lines (45 loc) · 1.75 KB
/
S06C46.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
60
# Imports
import math
import base64
import decimal
from lib import *
def check_parity(ciphertext: int, rsa: object) -> int:
"""
Returns the last bit of the number.
"""
return rsa.decryptnum(ciphertext) & 1
def parity_attack(message: bytes, rsa: object) -> int:
"""
Parity attack on RSA
"""
(_, n) = rsa.pub
ciphertext = rsa.encryptnum(int.from_bytes(message, "big"))
# encrypt multiplier
multiplier = rsa.encryptnum(2)
# Initialize lower and upper bound.
# I need to use Decimal because it allows me to set the precision for the floating point
# numbers, which we will need when doing the binary search divisions.
lower_bound = decimal.Decimal(0)
upper_bound = decimal.Decimal(n)
# Compute the number of iterations that we have to do
num_iter = int(math.ceil(math.log(n, 2)))
# Set the precision of the floating point number to be enough
decimal.getcontext().prec = num_iter
for _ in range(num_iter):
ciphertext = (ciphertext * multiplier) % n
# checking parity
if check_parity(ciphertext, rsa) & 1:
lower_bound = (lower_bound + upper_bound) / 2
else:
upper_bound = (lower_bound + upper_bound) / 2
# Return the binary version of the upper_bound (converted from Decimal to int)
return int(upper_bound).to_bytes((int(upper_bound).bit_length() + 7) // 8, "big").decode("utf-8")
def main():
# Given
given_string = "VGhhdCdzIHdoeSBJIGZvdW5kIHlvdSBkb24ndCBwbGF5IGFyb3VuZCB3aXRoIHRoZSBGdW5reSBDb2xkIE1lZGluYQ=="
byte_string = base64.b64decode(given_string)
plaintext = parity_attack(byte_string, RSA(1024))
assert(plaintext == byte_string.decode("utf-8"))
return
if __name__ == "__main__":
main()