diff --git a/.gitignore b/.gitignore index 9b65602..89a11af 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ cr/ .venv/ +*.pdf \ No newline at end of file diff --git a/lib/__pycache__/arithmetics.cpython-313.pyc b/lib/__pycache__/arithmetics.cpython-313.pyc index 006ef0f..c97630e 100644 Binary files a/lib/__pycache__/arithmetics.cpython-313.pyc and b/lib/__pycache__/arithmetics.cpython-313.pyc differ diff --git a/lib/__pycache__/rsa.cpython-313.pyc b/lib/__pycache__/rsa.cpython-313.pyc index 3ecd8ed..dcbd953 100644 Binary files a/lib/__pycache__/rsa.cpython-313.pyc and b/lib/__pycache__/rsa.cpython-313.pyc differ diff --git a/lib/arithmetics.py b/lib/arithmetics.py index 0ac78de..c561889 100644 --- a/lib/arithmetics.py +++ b/lib/arithmetics.py @@ -10,8 +10,10 @@ def modpow(a: int, b: int, m: int) -> int: return result def gcd(a: int, b: int) -> int: + # if b = 0 return a if b == 0: return a + # return gcd(b, a % b) because gcd(a, b) = gcd(b, rem(a, b)) return gcd(b, a % b) def mod_inverse(A: int, M: int) -> int: @@ -21,18 +23,13 @@ def mod_inverse(A: int, M: int) -> int: if (M == 1): return 0 while (A > 1): - # q is quotient q = A // M t = M - # m is remainder now, process - # same as Euclid's algo M = A % M A = t t = y - # Update x and y y = x - q * y x = t - # Make x positive if (x < 0): x = x + m0 return x diff --git a/lib/rsa.py b/lib/rsa.py index 0ae2a7c..e146730 100644 --- a/lib/rsa.py +++ b/lib/rsa.py @@ -1,8 +1,5 @@ # Python RSA implementation -import random -import sys - import lib.arithmetics as arithm import lib.miller_rabin as miller @@ -16,15 +13,21 @@ def get_p_and_q(n: int) -> (int, int): return (p, q) def get_keys(l: int) -> int: + # Get p and q p, q = get_p_and_q(l // 2) + # Compute n and phy(n) n = p * q phy_n = (p - 1) * (q - 1) + # Chose e = 65537 e = 65537 + # Ensure e fits with the others numbers while arithm.gcd(e, phy_n) != 1: p, q = get_p_and_q(l // 2) n = p * q phy_n = (p - 1) * (q - 1) + # Compute d d = arithm.mod_inverse(e, phy_n) + # Return e, d and n return (e, d, n) def encrypt(m: int, e: int, n: int) -> int: diff --git a/main.py b/main.py index 4b8be63..552d479 100644 --- a/main.py +++ b/main.py @@ -9,6 +9,7 @@ LEN = BITS // 5 def encode(text): sum = 0 for i in range(len(text)): + # Codage de la chaƮne en base 27 sum += ALPH[text[i]] * 27 ** i return sum