Skip to content
Snippets Groups Projects
Verified Commit e8487f5c authored by frederic.arroyo's avatar frederic.arroyo
Browse files

No commit message

No commit message
parents
Branches
No related tags found
No related merge requests found
from typing import Tuple
from random import randint
from sys import argv
from math import sqrt
INPUT_NAME="fichier.txt"
SIGNATURE_NAME="fichier.signed.txt"
PUBKEY_FILE="pubkey.txt"
PRIVKEY_FILE="privkey.txt"
class Pubkey:
p: int
g: int
A: int
def __init__(self, p: int, g: int, A: int) -> None:
self.p = p
self.g = g
self.A = A
def write(self, filename: str) -> None:
with open(filename, "w") as f:
f.write(f"{self.p} {self.g} {self.A}")
def read(filename: str) -> "Pubkey":
with open(filename, "r") as f:
p, g, A = map(int, f.read().split())
return Pubkey(p, g, A)
class Privkey:
p: int
g: int
a: int
def __init__(self, p: int, g: int, a: int) -> None:
self.p = p
self.g = g
self.a = a
def write(self, filename: str) -> None:
with open(filename, "w") as f:
f.write(f"{self.p} {self.g} {self.a}")
def read(filename: str) -> "Privkey":
with open(filename, "r") as f:
p, g, a = map(int, f.read().split())
return Privkey(p, g, a)
class Signature:
Y: int
S: int
def __init__(self, Y: int, S: int) -> None:
self.Y = Y
self.S = S
def write(self, filename: str, data: str) -> None:
with open(filename, "w") as f:
f.write(f"{self.Y} {self.S}\n{data}")
def read(filename: str) -> Tuple["Signature", str]:
with open(filename, "r") as f:
Y, S = map(int, f.readline().split())
data = f.read()
return Signature(Y, S), data
def hash(m: str) -> int:
return sum(ord(c) for c in m)
def sign(m: int, k: int, privkey: Privkey) -> Signature:
p = privkey.p
g = privkey.g
a = privkey.a
Y = pow(g, k, p)
S = pow((m - a * Y) * k, -1, p - 1)
return Signature(Y, S)
def verify(m: int, sig: Signature, pubkey: Pubkey) -> bool:
Y = sig.Y
S = sig.S
p = pubkey.p
g = pubkey.g
A = pubkey.A
return (pow(A, Y, p) * pow(Y, S, p)) % p == pow(g, m, p)
def is_prime(n: int) -> bool:
if n <= 1:
return False
for i in range(3, int(sqrt(n)) + 1, 2):
if n % i == 0:
return False
return True
def get_random_prime(min: int, max: int) -> int:
# trouver un nombre premier aléatoire entre min et max
while True:
n = randint(min, max)
if is_prime(n):
print(f"Found prime: {n}")
return n
def get_all_generators(p: int) -> int:
return [g for g in range(2, p) if all(pow(g, i, p) != 1 for i in range(1, p - 1))]
def get_random_generator(p: int) -> int:
generators = get_all_generators(p)
rand = randint(0, len(generators) - 1)
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)
A = pow(g, a, p)
return Privkey(p, g, a), Pubkey(p, g, A)
def main(mode):
if mode == 0:
# open the fichier.txt file and read its content into a string
m = ""
with open(INPUT_NAME, "r") as f:
m = f.read()
# algorithm
privkey, pubkey = generate_key_pair()
signature = sign(hash(m), 5, privkey)
# write our files
pubkey.write(PUBKEY_FILE)
privkey.write(PRIVKEY_FILE)
signature.write(SIGNATURE_NAME, m)
elif mode == 1:
signature, m = Signature.read(SIGNATURE_NAME)
pubkey = Pubkey.read(PUBKEY_FILE)
is_valid = verify(hash(m), signature, pubkey)
print("Signature is valid" if is_valid else "Signature is invalid")
else:
print("Usage: python elgamal.py [mode]")
print("mode: 0 for signing, 1 for verifying")
exit(1)
main(int(argv[1]) if len(argv) == 2 else -1)
\ No newline at end of file
1117 2077
Il faut pas respirer la compote, ça fait tousser.
\ No newline at end of file
Il faut pas respirer la compote, ça fait tousser.
\ No newline at end of file
6659 6 337
\ No newline at end of file
6659 6 1575
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment