diff --git a/elgamal.py b/elgamal.py index bdd1a53ca6ff65f3aca14b1602d8d9f87851406c..b51abfb52c901003aca0360436458f347bd91c26 100644 --- a/elgamal.py +++ b/elgamal.py @@ -1,7 +1,7 @@ from typing import Tuple from random import randint from sys import argv -from math import sqrt +from math import sqrt, gcd INPUT_NAME="fichier.txt" SIGNATURE_NAME="fichier.signed.txt" @@ -113,12 +113,31 @@ def get_random_generator(p: int) -> int: return generators[1] def generate_key_pair() -> Tuple[Privkey, Pubkey]: - p = get_random_prime(1000, 10_000) - g = get_random_generator(p) - a = randint(0, p - 1) + # p = get_random_prime(1000, 10_000) + # g = get_random_generator(p) + # a = randint(0, p - 1) + p = 7297 + g = 14 + a = 7057 A = pow(g, a, p) return Privkey(p, g, a), Pubkey(p, g, A) +def xgcd(a,b): + prevx , x = 1 , 0 + prevy , y = 0 , 1 + while b: + q = a//b + x, prevx = prevx - q*x, x + y, prevy = prevy - q*y, y + a, b = b, a % b + return a, prevx, prevy + +def generate_k(p: int) -> int: + k = randint(1, p - 2) + while gcd(k, p - 1) != 1: + k = randint(1, p - 2) + return k + def main(mode): if mode == 0: # open the fichier.txt file and read its content into a string @@ -128,7 +147,8 @@ def main(mode): # algorithm privkey, pubkey = generate_key_pair() - signature = sign(hash(m), 5, privkey) + k = generate_k(privkey.p) + signature = sign(hash(m), k, privkey) # write our files pubkey.write(PUBKEY_FILE) diff --git a/fichier.signed.txt b/fichier.signed.txt index dcbdea22f2d993f139862017acf6ba46ce29a189..9c454425b8de1c7a2c6df6da33f46b0a222d1eb8 100644 --- a/fichier.signed.txt +++ b/fichier.signed.txt @@ -1,2 +1,2 @@ -1117 2077 +3621 5011 Il faut pas respirer la compote, ça fait tousser. \ No newline at end of file diff --git a/privkey.txt b/privkey.txt index f51686ad8e4f9e387799995b5f6fe341623336ae..973946a06e5012c7f4aae4665d7537419e772991 100644 --- a/privkey.txt +++ b/privkey.txt @@ -1 +1 @@ -6659 6 337 \ No newline at end of file +7297 14 7057 \ No newline at end of file diff --git a/pubkey.txt b/pubkey.txt index 2255a709d25687d24bfa3355a6a8065efaaf9437..844dee23fae160ca04325d31b59164ff82554f74 100644 --- a/pubkey.txt +++ b/pubkey.txt @@ -1 +1 @@ -6659 6 1575 \ No newline at end of file +7297 14 1356 \ No newline at end of file