Tout jusqu'à la fonction dépouillement exclue.
This commit is contained in:
parent
c58e985561
commit
6e0e362e7e
2 changed files with 79 additions and 12 deletions
57
ElGamal.py
57
ElGamal.py
|
|
@ -42,7 +42,7 @@ def Decrypt_cc(g: int, q: int, sk: int, c: (int, int)) -> int:
|
||||||
Returns:
|
Returns:
|
||||||
The clear message.
|
The clear message.
|
||||||
"""
|
"""
|
||||||
return c[1] // pow(c[0], sk, q)
|
return (c[1] * pow(c[0], -sk, q)) % q
|
||||||
|
|
||||||
|
|
||||||
def Encrypt_ce(g: int, q: int, pk: int, m: int) -> (int, int):
|
def Encrypt_ce(g: int, q: int, pk: int, m: int) -> (int, int):
|
||||||
|
|
@ -85,20 +85,57 @@ def Add_cipher(c: list[(int, int)]) -> int:
|
||||||
return reduce(lambda x, y: x + y[1], c, 0)
|
return reduce(lambda x, y: x + y[1], c, 0)
|
||||||
|
|
||||||
|
|
||||||
def KeyGen_central(g: int, q: int, N: int) -> (int, int):
|
def KeyGen_central(g: int, q: int, N: int) -> ([int], int):
|
||||||
"""Generate N public/private key pairs.
|
"""Generate N private key fragments and a public key.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
g: A generator for the group.
|
g: A generator for the group.
|
||||||
q: The order of a group.
|
q: The order of a group.
|
||||||
N: The number of keypairs to generate.
|
N: The number of privkey fragment to generate.
|
||||||
Returns:
|
Returns:
|
||||||
N public keys and 1 private key.
|
N private keys fragments and 1 public key.
|
||||||
"""
|
"""
|
||||||
keys = []
|
keys = []
|
||||||
|
K = 1
|
||||||
for i in range(N):
|
for i in range(N):
|
||||||
r = randint(1, q - 1)
|
pk_i, sk_i = KeyGen(g, q)
|
||||||
keys.push(pow(g, r, q))
|
keys.append(sk_i)
|
||||||
K = reduce(lambda x, y: x * y, keys, 1)
|
K = (K * pk_i) % q
|
||||||
keys.push(K)
|
return keys, K
|
||||||
return keys
|
|
||||||
|
|
||||||
|
def Decrypt_central_cc(g: int, q: int, k: [int], c: (int, int)) -> int:
|
||||||
|
"""Decipher a secret using multiple private key fragments.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
g: A generator for the group.
|
||||||
|
q: The order of a group.
|
||||||
|
k: A list of private key fragments.
|
||||||
|
c: A cipher to decipher.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The deciphered message in cleartext.
|
||||||
|
"""
|
||||||
|
Y = 1
|
||||||
|
for K_i in k:
|
||||||
|
Y = (Y * pow(c[0], K_i, q)) % q
|
||||||
|
return (c[1] * pow(Y, -1, q)) % q
|
||||||
|
|
||||||
|
|
||||||
|
def Decrypt_central_ce(g: int, q: int, k: [int], c: (int, int)) -> int:
|
||||||
|
"""Decipher a secret using multiple private key fragments (exponential).
|
||||||
|
|
||||||
|
Args:
|
||||||
|
g: A generator for the group.
|
||||||
|
q: The order of a group.
|
||||||
|
k: A list of private key fragments.
|
||||||
|
c: A cipher to decipher.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The deciphered message in cleartext.
|
||||||
|
"""
|
||||||
|
Y = 1
|
||||||
|
for K_i in k:
|
||||||
|
Y = (Y * pow(c[0], K_i, q)) % q
|
||||||
|
M = (c[1] * pow(Y, -1, q)) % q
|
||||||
|
return discrete_log(q, M, g)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,16 @@
|
||||||
from ElGamal import KeyGen, Encrypt_cc, Decrypt_cc, Encrypt_ce, Decrypt_ce, Add_cipher
|
from sympy import primitive_root, randprime
|
||||||
from sympy import randprime, primitive_root
|
|
||||||
|
from ElGamal import (
|
||||||
|
Add_cipher,
|
||||||
|
Decrypt_cc,
|
||||||
|
Decrypt_ce,
|
||||||
|
Encrypt_cc,
|
||||||
|
Encrypt_ce,
|
||||||
|
KeyGen,
|
||||||
|
KeyGen_central,
|
||||||
|
Decrypt_central_cc,
|
||||||
|
Decrypt_central_ce,
|
||||||
|
)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# On prend le groupe de (randprime - 1)
|
# On prend le groupe de (randprime - 1)
|
||||||
|
|
@ -25,3 +36,22 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
ciphers = [(1, 1), (1, 2), (1, 3)]
|
ciphers = [(1, 1), (1, 2), (1, 3)]
|
||||||
assert Add_cipher(ciphers) == 1 + 2 + 3
|
assert Add_cipher(ciphers) == 1 + 2 + 3
|
||||||
|
|
||||||
|
# Le chiffrement par clé publique est déchiffrable par la clé privée reconstituée.
|
||||||
|
privkeys, pubkey = KeyGen_central(gen, ordre, 2)
|
||||||
|
clear = 12
|
||||||
|
print(f"Clés privées : {privkeys}")
|
||||||
|
print(f"Clé publique : {pubkey}")
|
||||||
|
crypt = Encrypt_cc(gen, ordre, pubkey, clear)
|
||||||
|
assert clear == Decrypt_central_cc(gen, ordre, privkeys, crypt)
|
||||||
|
|
||||||
|
assert clear != Decrypt_cc(
|
||||||
|
gen, ordre, privkeys[0], Encrypt_cc(gen, ordre, pubkey, clear)
|
||||||
|
)
|
||||||
|
assert clear != Decrypt_cc(
|
||||||
|
gen, ordre, privkeys[1], Encrypt_cc(gen, ordre, pubkey, clear)
|
||||||
|
)
|
||||||
|
|
||||||
|
crypt = Encrypt_ce(gen, ordre, pubkey, clear)
|
||||||
|
assert clear == Decrypt_central_ce(gen, ordre, privkeys, crypt)
|
||||||
|
assert clear != Decrypt_central_ce(gen, ordre, [privkeys[0]], crypt)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue