Signature
Signature
et
RSA (Rivest–Shamir–Adleman).
In [ ]: import math
# MD5 Implementation
Args:
message (str): The input message to hash.
Returns:
str: The hexadecimal representation of the MD5 hash.
"""
# Initialize constants
K = [int(abs(math.sin(i + 1)) * 2 ** 32) for i in range(64)]
# shifts values
s = [7, 12, 17, 22] * 4 + [5, 9, 14, 20] * 4 + [4, 11, 16, 23] * 4 + [6, 10, 15, 21] * 4
A, B, C, D = a, b, c, d
for j in range(64):
if j <= 15:
F_func = (B & C) | (~B & D)
g = j
elif j <= 31:
F_func = (D & B) | (~D & C)
g = (5 * j + 1) % 16
elif j <= 47:
F_func = B ^ C ^ D
g = (3 * j + 5) % 16
else:
F_func = C ^ (B | ~D)
g = (7 * j) % 16
temp = D
D = C
C = B
B = (B + leftRotate(A + F_func + K[j] + SubBlocks[g], s[j])) & 0xFFFFFFFF
A = temp
a = (a + A) & 0xFFFFFFFF
b = (b + B) & 0xFFFFFFFF
c = (c + C) & 0xFFFFFFFF
d = (d + D) & 0xFFFFFFFF
return hashMD5Hex
# Message hashing
hashMessage = MD5(message)
Message =
Salut! On fait un test de notre implémentation des signatures numériques avec MD5 et RSA!
On va signer ce message et par la suite vérifier s'il n'a pas été altéré!
A plus pour la vérification...
Message haché =
00598162889abc65837467a804fbef25
In [ ]: import random
Parameters:
n (int): The number to test for primality. Must be greater than 3 and odd.
k (int): The number of iterations of the test. Must be positive. Default value is 10.
Returns:
bool: True if n is probably prime, False if n is definitely composite.
Complexity:
O(k log^3(n)), where k is the number of iterations and n is the number to test.
This is a polynomial complexity in the size of the number, which means that the
computation time increases reasonably with the size of the number.
This is much faster than deterministic primality testing methods, which have
Probability:
The probability that a false positive occurs is less than (1/4)^k. For example,
with k=10, this probability is less than 10^-6.
"""
# Check if n is valid
if n <= 3:
return n == 2 or n == 3
if n % 2 == 0:
return False
# Find s and d such that n-1 = 2^s * d
s = 0
d = n - 1
while d % 2 == 0:
s += 1
d //= 2
# Repeat the test k times
for i in range(k):
# Choose a random number a between 2 and n-2
a = random.randint(2, n-2)
# Compute a^d mod n using fast exponentiation
x = pow(a, d, n)
# If x is equal to 1 or to n-1, go to the next iteration
if x == 1 or x == n - 1:
continue
# Repeat the calculation by multiplying x by itself s times
for j in range(s - 1):
x = pow(x, 2, n)
# If x is equal to 1, then n is not prime
if x == 1:
return False
# If x is equal to n-1, go to the next iteration
if x == n - 1:
break
else:
# If no value of x is equal to n-1, then n is not prime
return False
# Otherwise, n is probably prime with high probability
return True
Parameters:
bits (int): The bit size of the prime number to generate. Must be positive.
Returns:
int: A random prime number of the given bit size.
"""
while True:
# Choose a random odd number between 2^(bits-1) and 2^bits - 1
# The Bretrand thoremen guarantees us to have at least a primer number in this interval
p = random.randrange(2**(nbits-1) + 1, 2**nbits+3, 2)
# Test if n is prime using the miller_rabin function with k=10 iterations
if isPrimeMillerRabin(p):
return p
Args:
nbits (int): Number of bits for the key size.
Returns:
tuple[int, int, int]: (n, e, d) where:
- n: modulus
- e: public exponent
- d: private exponent
"""
# Generate two distinct prime numbers p and q
while True:
p = genPrimeMillerRabin(nbits)
q = genPrimeMillerRabin(nbits)
if p != q:
break
n = p * q
phi = (p - 1) * (q - 1)
# Ensure d is positive
if d < 0:
d += phi
return n, e, d
nbits = 1024 # minimum number of bits used to generate the random prime numbers
n, e, d = genKeysRSA(nbits)
f.write(str(d))
print("Modulo : n = ", n, "\nClé publique : e = ", e, "\nClé privée : d = ", d, sep = "\n")
Modulo : n =
16824991855836491824853359085697758966400671053237560846573348145978697882836991335454579293792955711788040981078050
45893948205284959672479207523969239873263158112065818751773678353986166531489599843634656819545796404934436028299152
70756078191606682064573609901588719274162931492601716217750490950837385180306862008409416452778617409288216609245656
99355844518803554275853625241628231633314537069165822548237740838060805531294972507397377660876874057255835088299234
01451524491109902885143645106762140181970503037418304625623903235205664086431353047152778539398273752956060137482018
3994008950985968697280062148213442681
Clé publique : e =
55798284315982155081451059060746905107514864426670909808242865814350871877955255508708337388275040064670825724670273
06667789608069194665498957361372941316390198505973188403285817950552603231997906998158862780961225743403691395454672
88873653383810815859815192251667919181858185374033848481932364323487793978237905301536786302578610049449018970483851
33556442248136087456689634253045313237363047085783893879344939139362641582563892918572891012763074365201768799105284
73247056165951858703180577051220158774507628687237482988296439134927692225475392205962374641391054083289596217395540
468872153628257518499108223535267805
Clé privée : d =
27318421682432543972036943668359397772947169470564104033241677658513834371708309508873486824189541376878650346023303
38307969128434744233786515115102560029868197558391844241051344447523663506136756066249003337188041496583525457940737
18693295537771133306105114927292918687418634128509556945556965424357509022633327521066802029868077623682682153329075
81850550682197577558552152104343289272844913436200405519613855377339934043278015323586462022897965934412974479183973
31837978388485819796634284306628916630167668009523285744345780334400839586167101790649939576994916367668846061045958
561755904163800116933931318327247221
Args:
msg (str): The plaintext message.
n (int): The modulus.
key (int): The encryption/decryption key (either public or private).
Returns:
int: The ciphertext.
"""
# Convert characters to their ASCII values and pad with zeros
msg_padded = [str(ord(c)).zfill(3) for c in msg]
SignedMessage = cipherRSA(hashMessage, n, d)
Message signé =
1705005349270368498277509172464249830183610499908347876055100540079446178871775775625534318189666959363658281035106
20014076522006406820281559597222311502372285971682366339058893809026759964243011709466555681902343140492411557791117
30963788834098657920256898308736063022455560107595034120552352838746548580633064994529437278733134697823570111232982
59383299241882065533418358360173543344926543697779599980587570845155583920095419046047780300177531971118376805183556
42265606141020055286412950735220141735359301511497586383028927852728329005787244893192449307109608390400590686799705
2478078662508872995853236404431392527