Skip to content
Snippets Groups Projects
Commit 779c07b2 authored by jonas.stirnema's avatar jonas.stirnema
Browse files

idk

parents aae1593d 073bbdb3
No related branches found
No related tags found
No related merge requests found
'''
Description : Calcule le PGCD de deux nombres et leurs
coefficients de bezout
Return : PGCD, Coef de X, Coef de Y
(X est le nombre le plus grand)
'''
def pgcd_etendu(a, b):
a, b = abs(a), abs(b)
# On s'assure que le plus grand nombre est a
#if b > a:
#b,a = a,b
# Vérification que le plus petit nombre n'est pas 0
assert(not (b == 0))
r = [None] * 100
q = [None] * 100
x = [None] * 100
y = [None] * 100
etape = 2
# Phase d'initialisation
r[0] = a
x[0] = 1
y[0] = 0
r[1] = b
x[1] = 0
y[1] = 1
while (r[etape-1] != 0):
r[etape] = a % b
q[etape] = a // b
x[etape] = x[etape-2] - q[etape] * x[etape-1]
y[etape] = y[etape-2] - q[etape] * y[etape-1]
a, b = b, r[etape]
etape += 1
return r[etape-2], x[etape-2], y[etape-2]
def pgcd_etendu_verif(a, b, x, y, pgcd):
if (pgcd == (a*x + b*y)):
return True
return False
if __name__ == '__main__':
b = 4991
a = 1197
print(pgcd_etendu(a, b))
pgcd, x, y = pgcd_etendu(a, b)
print(pgcd_etendu_verif(a, b, x, y, pgcd))
\ No newline at end of file
def exp_rapide(nb: int, exp: int, mod: int) -> int:
"""Calcule l'exponentiation rapide d'un nombre
Args:
nb (int): nombre
exp (int): puissance
mod (int): module
Returns:
int: exponentitation rapide
"""
list_exp = [int(x) for x in bin(exp)[:1:-1]]
# Créer la liste de l'exp decomposé
a = []
a.append(nb ** 1 % mod)
for x in range(1, len(list_exp)):
a.append(a[x-1]**2 % mod)
# Calcule l'exp rapide
out = 1
for index, valeur in enumerate(list_exp):
if valeur == 1:
out *= a[index] % mod
return out % mod
if __name__ == '__main__':
u = 68393426
n = 124344401
d = 29995379
# print(exp_rapide(6, 392, 13))
print(exp_rapide(u, d, n))
from prime import *
from euclide import *
def find_p_q(n):
for p in range(3, int(n/2), 2):
if (n / p).is_integer():
return p, n/p
def find_private_key(p, q, e):
z = (p-1) * (q-1)
r, x, y = pgcd_etendu(e, z)
return (x % z)
if __name__ == '__main__':
n = 124344401 #Clé publique 1.
e = 1919 #Clé publique 2.
p, q = find_p_q(n)
private_key = find_private_key(p, q, e) #Clé privé
print(private_key)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment