Created
October 21, 2018 12:47
-
-
Save ykm11/85968ad6624b29f96a0d0e3add7f50c1 to your computer and use it in GitHub Desktop.
test_elgamal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from useless.math import * | |
from Crypto.Util.number import * | |
import random | |
def gen_key(bits): | |
p = getPrime(bits) | |
g = random.randint(1, p-1) | |
x = random.randint(1, p-1) | |
h = pow(g, x, p) | |
return g, h, p, x | |
def encrypt(m): | |
r = random.randint(1, p-1) | |
c1 = pow(g, r, p) | |
c2 = m * pow(h, r, p) % p | |
return c1, c2 | |
def decrypt(c1, c2): | |
c1 = modinv(c1, p) | |
return c2 * pow(c1, x, p) % p | |
g, h, p, x = gen_key(512) | |
m = 114514 | |
c1, c2 = encrypt(m) | |
t = 5 | |
assert decrypt(c1, c2) == m | |
assert decrypt(c1, t*c2) == t*m |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment