diff --git a/algo.py b/algo.py index cf8e4440164b192dac2d16fa056920beb6f7a818..8e292942931104370a388648b0a45ba5ee5c856e 100644 --- a/algo.py +++ b/algo.py @@ -34,6 +34,19 @@ def bachet_bezout(a, b): return a, u[i-2], v[i-2] +def inverse_modulaire(a, n): + """Does the inverse modular of a by n, using Bachet-Bezout + + Args: + a (uint): the number + n (uint): the modulo + + Returns: + uint: the inverse modular + """ + + return bachet_bezout(a, n)[1] + def exponentiation_rapide(a, exp, n): """Does the quick explanation of a pow x modulo n @@ -108,4 +121,3 @@ def fermat_factorization(n): a += 1 return (a, b) - diff --git a/main.py b/main.py index e1e62c9a31f4c64f908a097284e9e049c0f8c232..67d26849b9055fb4f4bc656fbda3ec9ccb33b276 100644 --- a/main.py +++ b/main.py @@ -1,34 +1,44 @@ -import algo +from algo import * def main(): mu = { - 31726849986005826981, - 305966565717393601613, - 61497322861823383198, - 269645690420162032117, - 155457162093765938384, - 24931468152962121635, - 138444967690527010216, - 282789589899417404817, - 134251529920691060404, - 423054566352157178418, - 265453042944217161627, - 39119050384849643825 - } #encrypted message - - n = 4556490129 * pow(10, 11) + 89940178621 #first element public key - e = 5303 #second element public key + 416687707, + 420774592, + 1078076801, + 372477309, + 1915438026, + 306996859, + 1858268340, + 1934595642, + 444729462, + 1953792398, + 1118037789, + 1220832721, + 701508709, + 1976470330, + 1081245832, + 1480954262, + 921801986, + 1154526486, + 1974597168, + 812527863, + 1895548977, + 1274512749, + 712992858 + } #encrypted message + n = 1989929159 #first element public key + e = 2203 #second element public key length = length(mu) # --- private element --- - M = [] #decriypted message - msg = "" #message (string) - p, q = 0 #primes numbers - d = 0 #private key + M = [] #decriypted message + msg = "" #message (string) + p, q = 0 #primes numbers + d = 0 #private key #--- crack RSA --- - a,b = fermat_factorization(n) + a, b = fermat_factorization(n) p = a + b q = a - b @@ -38,7 +48,7 @@ def main(): fi = (p - 1) * (q - 1) d = inverse_modulaire(e, fi) - #// --- decode mu & initialise msg --- + # --- decode mu & initialise msg --- for i in range(length): M[i] = exponentiation_rapide(mu[i], d, n)