initial la moitié du TP est fait mais commit
This commit is contained in:
commit
c58e985561
4 changed files with 145 additions and 0 deletions
104
ElGamal.py
Normal file
104
ElGamal.py
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
from random import randint
|
||||
from sympy import discrete_log
|
||||
from functools import reduce
|
||||
|
||||
|
||||
def KeyGen(g: int, q: int) -> (int, int):
|
||||
"""Generate a secure key for ElGamal.
|
||||
|
||||
Args:
|
||||
g: A generator for the group.
|
||||
q: The order of a group.
|
||||
Returns:
|
||||
return A public key and a secret key.
|
||||
"""
|
||||
k = randint(2, q - 1)
|
||||
return pow(g, k, q), k
|
||||
|
||||
|
||||
def Encrypt_cc(g: int, q: int, pk: int, m: int) -> (int, int):
|
||||
"""Encrypt data using a public key
|
||||
|
||||
Args:
|
||||
g: A generator for the group
|
||||
q: The order of the group
|
||||
pk: The public key used to cipher the message
|
||||
m: A message to cipher.
|
||||
Returns:
|
||||
The ciphered message.
|
||||
"""
|
||||
r = randint(2, q - 1)
|
||||
return (pow(g, r, q), (m * pow(pk, r, q)))
|
||||
|
||||
|
||||
def Decrypt_cc(g: int, q: int, sk: int, c: (int, int)) -> int:
|
||||
"""Decrypt an ElGamal cipher using a public key
|
||||
|
||||
Args:
|
||||
g: A generator for the group
|
||||
q: The order of the group
|
||||
sk: The secret key to use to decipher
|
||||
c: A ciphered message to clarify
|
||||
Returns:
|
||||
The clear message.
|
||||
"""
|
||||
return c[1] // pow(c[0], sk, q)
|
||||
|
||||
|
||||
def Encrypt_ce(g: int, q: int, pk: int, m: int) -> (int, int):
|
||||
"""Encrypt an ElGamal cipher using a public key using exponential
|
||||
|
||||
Args:
|
||||
g: A generator for the group
|
||||
q: The order of the group
|
||||
pk: The public key to use to cipher
|
||||
m: A message to cipher
|
||||
Returns:
|
||||
The ciphered message.
|
||||
"""
|
||||
r = randint(2, q - 1)
|
||||
return (pow(g, r, q), (pow(g, m, q) * pow(pk, r, q)) % q)
|
||||
|
||||
|
||||
def Decrypt_ce(g: int, q: int, sk: int, c: (int, int)) -> int:
|
||||
"""Encrypt an ElGamal cipher using a public key (exponential)
|
||||
|
||||
Args:
|
||||
g: A generator for the group
|
||||
q: The order of the group
|
||||
sk: The secret key to use to decipher
|
||||
c: A ciphered message to decipher
|
||||
Returns:
|
||||
The clear message.
|
||||
"""
|
||||
s = c[1] * pow(c[0], -sk, q)
|
||||
return discrete_log(q, s, g)
|
||||
|
||||
|
||||
def Add_cipher(c: list[(int, int)]) -> int:
|
||||
"""Add multiple ciphers together into a new ElGamal cipher.
|
||||
Args:
|
||||
c: A list of ciphers.
|
||||
Returns:
|
||||
The accumulation of ciphers.
|
||||
"""
|
||||
return reduce(lambda x, y: x + y[1], c, 0)
|
||||
|
||||
|
||||
def KeyGen_central(g: int, q: int, N: int) -> (int, int):
|
||||
"""Generate N public/private key pairs.
|
||||
|
||||
Args:
|
||||
g: A generator for the group.
|
||||
q: The order of a group.
|
||||
N: The number of keypairs to generate.
|
||||
Returns:
|
||||
N public keys and 1 private key.
|
||||
"""
|
||||
keys = []
|
||||
for i in range(N):
|
||||
r = randint(1, q - 1)
|
||||
keys.push(pow(g, r, q))
|
||||
K = reduce(lambda x, y: x * y, keys, 1)
|
||||
keys.push(K)
|
||||
return keys
|
||||
Loading…
Add table
Add a link
Reference in a new issue