From a4e5dff06d408f0e0609d6b10b436aaa84cefccc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Arroyo?= <frederic.arroyo@etu.hesge.ch>
Date: Thu, 25 Apr 2024 11:47:02 +0200
Subject: [PATCH]

---
 elgamal.py         | 30 +++++++++++++++++++++++++-----
 fichier.signed.txt |  2 +-
 privkey.txt        |  2 +-
 pubkey.txt         |  2 +-
 4 files changed, 28 insertions(+), 8 deletions(-)

diff --git a/elgamal.py b/elgamal.py
index bdd1a53..b51abfb 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 dcbdea2..9c45442 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 f51686a..973946a 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 2255a70..844dee2 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
-- 
GitLab