2026-11-14 12:41:40 +01:00
|
|
|
from hashlib import sha256
|
2026-11-14 13:24:19 +01:00
|
|
|
from Crypto.Util.number import bytes_to_long, long_to_bytes
|
2026-11-14 13:02:10 +01:00
|
|
|
from math import ceil, sqrt
|
2026-11-14 12:41:40 +01:00
|
|
|
|
|
|
|
|
|
2026-11-05 12:08:15 +01:00
|
|
|
# Challenge PK-crypto-1
|
2026-11-05 12:09:50 +01:00
|
|
|
def modular_exponentiation() -> int:
|
2026-11-05 12:08:15 +01:00
|
|
|
return pow(101, 17, 22663)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Challenge PK-crypto-2
|
2026-11-05 12:09:50 +01:00
|
|
|
def basic_encryption(message: int, p: int, q: int) -> int:
|
2026-11-05 12:08:15 +01:00
|
|
|
return pow(message, 0x10001, p * q)
|
2026-11-05 12:09:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# Challenge PK-crypto-3
|
|
|
|
|
def Euler_totient(p: int, q: int) -> int:
|
|
|
|
|
return (p - 1) * (q - 1)
|
2026-11-05 12:12:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# Challenge PK-crypto-4
|
|
|
|
|
def private_key(p: int, q: int, exp: int) -> int:
|
|
|
|
|
return pow(exp, -1, (p - 1) * (q - 1))
|
2026-11-05 13:06:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# 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)
|
2026-11-14 12:41:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# 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)
|
2026-11-14 13:02:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# 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)
|
2026-11-14 13:24:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# Challenge PK-crypto-8
|
|
|
|
|
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
|
2026-11-14 13:25:54 +01:00
|
|
|
# N est premier donc phi(N) = N - 1 donc on peut facilement calculer la clé privée.
|
|
|
|
|
e = pow(d, -1, (N - 1))
|
2026-11-14 13:24:19 +01:00
|
|
|
return long_to_bytes(pow(c, e, N))
|