com_projet/public_key_cryptography.py
boyreau fbd5e62697 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.
2026-11-14 13:24:19 +01:00

84 lines
3.3 KiB
Python

from hashlib import sha256
from Crypto.Util.number import bytes_to_long, long_to_bytes
from math import ceil, sqrt
# Challenge PK-crypto-1
def modular_exponentiation() -> int:
return pow(101, 17, 22663)
# Challenge PK-crypto-2
def basic_encryption(message: int, p: int, q: int) -> int:
return pow(message, 0x10001, p * q)
# Challenge PK-crypto-3
def Euler_totient(p: int, q: int) -> int:
return (p - 1) * (q - 1)
# Challenge PK-crypto-4
def private_key(p: int, q: int, exp: int) -> int:
return pow(exp, -1, (p - 1) * (q - 1))
# Challenge PK-crypto-5
def decode(p: int, q: int, exp: int, c: int):
pk = private_key(p, q, exp)
return pow(c, pk, p * q)
# Challenge PK-crypto-6
def hash_and_encrypt(m: str):
d = 11175901210643014262548222473449533091378848269490518850474399681690547281665059317155831692300453197335735728459259392366823302405685389586883670043744683993709123180805154631088513521456979317628012721881537154107239389466063136007337120599915456659758559300673444689263854921332185562706707573660658164991098457874495054854491474065039621922972671588299315846306069845169959451250821044417886630346229021305410340100401530146135418806544340908355106582089082980533651095594192031411679866134256418292249592135441145384466261279428795408721990564658703903787956958168449841491667690491585550160457893350536334242689
N = 15216583654836731327639981224133918855895948374072384050848479908982286890731769486609085918857664046075375253168955058743185664390273058074450390236774324903305663479046566232967297765731625328029814055635316002591227570271271445226094919864475407884459980489638001092788574811554149774028950310695112688723853763743238753349782508121985338746755237819373178699343135091783992299561827389745132880022259873387524273298850340648779897909381979714026837172003953221052431217940632552930880000919436507245150726543040714721553361063311954285289857582079880295199632757829525723874753306371990452491305564061051059885803
m_bytes = m.encode("UTF-8")
hash = bytes_to_long(sha256(m_bytes).digest())
return pow(hash, d, N)
# Repris du TP3 de math pour la crypto
def estCarre(x: int) -> bool:
return pow(ceil(sqrt(x)), 2) == x
def Fermat(N: int) -> (int, int):
"""Find the prime factors of a number.
Args:
N: a prime product.
Returns:
p, q the prime factors of N.
"""
x = ceil(sqrt(N))
while not estCarre(pow(x, 2) - N):
x += 1
p = x + sqrt(pow(x, 2) - N)
q = x - sqrt(pow(x, 2) - N)
return (ceil(p), ceil(q))
# 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))