Skip to content
Snippets Groups Projects
Commit cfc3971e authored by dario.genga's avatar dario.genga
Browse files

Add comments to methods

Also removed the unused pgcd method.
parent efa7ed6a
No related branches found
No related tags found
No related merge requests found
......@@ -34,22 +34,31 @@ def get_bezout_coefficients(a, b):
def modular_inverse(a, n):
"""Compute the modular inverse of a number a modulo n.
Args:
a (int): The number to reverse.
n (int): The modulo.
Returns:
int: The reversed number.
"""
coefficients = get_bezout_coefficients(a, n)
if a * coefficients[0] % n == 1:
return coefficients[0] % n
return None
def pgcd(a, b):
if b > a:
temp = a
a = b
b = temp
return get_bezout_coefficients(a, b)[0]
def get_q_p(n):
"""Get the value of q and p from n by bruteforce.
Args:
n (int): One of the two value of the public key.
def get_q_p(n):
Returns:
tuple: The two value that represent n.
"""
p = 2
while True:
if n % p == 0:
......@@ -59,6 +68,16 @@ def get_q_p(n):
def modular_pow(base, exponent, modulus):
"""Compute the modular power.
Args:
base (int): The value of a part of the message to be decoded.
exponent (int): The private key.
modulus (int): One of the two value of the public key.
Returns:
int: The int value of the value, ready to be decoded to utf-8.
"""
if modulus == 1:
return 0
result = 1
......@@ -71,75 +90,80 @@ def modular_pow(base, exponent, modulus):
return result
e = 5249
n = 1653973759
messages = (
1511395078,
260436590,
1630654276,
1190458520,
790492067,
515550941,
297140366,
755589582,
647075331,
1191707844,
901889430,
660956124,
1500654109,
984322720,
1275630738,
1244853107,
1445928913,
1312523810,
265093060,
933013993,
1375592761,
195866064,
534502441,
928270408,
166404031,
621272622,
1304987439,
905393335,
55120151,
772595721,
506609577,
1172751778,
162439707,
233959833,
1468937795,
1358701120,
901889430,
495995733,
1524090698,
1043509086,
934992314,
1545639379,
1061595897,
1348452679,
1135067876,
905393335,
621272622,
55120151,
233959833,
1220119699,
708711266,
517797467,
195866064,
1579814353,
412378626,
498875436,
445485200,
7656659
)
p, q = get_q_p(n)
d = modular_inverse(e, (p - 1) * (q - 1))
result = ""
for x in messages:
tmp = modular_pow(x, d, n)
mBytes = tmp.to_bytes(tmp.bit_length(), byteorder="little")
result += mBytes.decode("utf-8")
print(result)
def main():
e = 5249
n = 1653973759
messages = (
1511395078,
260436590,
1630654276,
1190458520,
790492067,
515550941,
297140366,
755589582,
647075331,
1191707844,
901889430,
660956124,
1500654109,
984322720,
1275630738,
1244853107,
1445928913,
1312523810,
265093060,
933013993,
1375592761,
195866064,
534502441,
928270408,
166404031,
621272622,
1304987439,
905393335,
55120151,
772595721,
506609577,
1172751778,
162439707,
233959833,
1468937795,
1358701120,
901889430,
495995733,
1524090698,
1043509086,
934992314,
1545639379,
1061595897,
1348452679,
1135067876,
905393335,
621272622,
55120151,
233959833,
1220119699,
708711266,
517797467,
195866064,
1579814353,
412378626,
498875436,
445485200,
7656659
)
p, q = get_q_p(n)
d = modular_inverse(e, (p - 1) * (q - 1))
result = ""
for x in messages:
tmp = modular_pow(x, d, n)
mBytes = tmp.to_bytes(tmp.bit_length(), byteorder="little")
result += mBytes.decode("utf-8")
print(result)
if __name__ == "__main__":
main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment