public_key_cryptography course - challenge 8 - pourquoi utiliser 2 nombres premiers au lieu d'un\
je pensais qu'on prenait un nombre premier au carré, mais en fait il semble que le N du sujet est directement premier.
This commit is contained in:
parent
965057128d
commit
fbd5e62697
2 changed files with 29 additions and 2 deletions
|
|
@ -1,5 +1,5 @@
|
|||
from hashlib import sha256
|
||||
from Crypto.Util.number import bytes_to_long
|
||||
from Crypto.Util.number import bytes_to_long, long_to_bytes
|
||||
from math import ceil, sqrt
|
||||
|
||||
|
||||
|
|
@ -61,3 +61,24 @@ def Fermat(N: int) -> (int, int):
|
|||
# Challenge PK-crypto-7
|
||||
def prime_factorization(N: int):
|
||||
return Fermat(N)
|
||||
|
||||
|
||||
# Challenge PK-crypto-8
|
||||
def _find_single_prime_RSA_private_key(N: int, d: int):
|
||||
# N = x²
|
||||
# p = sqrt(N) -> on factorise facilement p
|
||||
# phi(N) = phi(p²) = p * (p-1)
|
||||
# phi(N) = sqrt(N) * (sqrt(N) - 1) = p * (p - 1)
|
||||
# d*e = 1 mod phi(N)
|
||||
# Il ne reste plus qu'à trouver l'inverse modulaire de e mod Phi(N) pour avoir d
|
||||
p = int(sqrt(N))
|
||||
phi_N = p * (p - 1)
|
||||
e = pow(d, -1, phi_N)
|
||||
print(f"N: {N}, p: {p}, phi_N: {phi_N}, e: {e}")
|
||||
return e
|
||||
|
||||
|
||||
def reverse_single_prime_RSA_cipher(c: int, N: int, d: int):
|
||||
# On calcule la clé privée puis on applique la formule de déchiffrement
|
||||
e = _find_single_prime_RSA_private_key(N, d)
|
||||
return long_to_bytes(pow(c, e, N))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue