27 lines
853 B
Python
27 lines
853 B
Python
from ElGamal import KeyGen, Encrypt_cc, Decrypt_cc, Encrypt_ce, Decrypt_ce, Add_cipher
|
|
from sympy import randprime, primitive_root
|
|
|
|
if __name__ == "__main__":
|
|
# On prend le groupe de (randprime - 1)
|
|
ordre = randprime(2, 1 << 10)
|
|
gen = primitive_root(ordre)
|
|
|
|
m = 22
|
|
pk, sk = KeyGen(gen, ordre)
|
|
print(f"Private key: {pk}, public key: {sk}")
|
|
cipher = Encrypt_cc(gen, ordre, pk, m)
|
|
# print(f"Cipher of {m}: {cipher}")
|
|
clear = Decrypt_cc(gen, ordre, sk, cipher)
|
|
print(f"Cleartext message : {clear}")
|
|
assert clear == m
|
|
|
|
cipher = 0
|
|
clear = 0
|
|
cipher = Encrypt_ce(gen, ordre, pk, m)
|
|
print(f"Cipher_ce: {cipher}")
|
|
clear = Decrypt_ce(gen, ordre, sk, cipher)
|
|
print(f"Cleartext_ce: {clear}")
|
|
assert clear == m
|
|
|
|
ciphers = [(1, 1), (1, 2), (1, 3)]
|
|
assert Add_cipher(ciphers) == 1 + 2 + 3
|