-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathelgamal.py
55 lines (45 loc) · 1.55 KB
/
elgamal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python3
# Created by Rob 'mubix' Fuller
# Source of Info: https://www.debjitbiswas.com/elgamal/
from Crypto.Util.number import inverse
import sympy
import random
from binascii import hexlify, unhexlify
def shared_secret(g,x,p):
# Shared Secret (h)
h = pow(g,x,p)
return h
def encrypt(m,r,g,p,h):
c1 = pow(g,r,p)
# Stolen from https://github.com/dlitz/pycrypto/blob/master/lib/Crypto/PublicKey/ElGamal.py
c2 = (m * pow(h, r, p) ) % p
return c1,c2
def decrypt(x,c1,c2,p):
s = pow(c1,x,p)
dm = (c2 * inverse(s,p)) % p
return dm
if __name__ == "__main__":
input_message = input("Enter message to encrypt: ")
inputbytes = str.encode(input_message)
m = int(hexlify(inputbytes), 16)
p = sympy.randprime(m*2, m*4)
g = sympy.randprime(int(m/2), m)
x = random.randint(int(m/2),m)
r = random.randint(int(m/2),m)
print("Bob's MESSAGE : {}".format(input_message))
print("MESSAGE as an int (M) : {}".format(m))
print("Prime number (P) : {}".format(p))
print("Generator (G) : {}".format(g))
print("Alice private key (X) : {}".format(x))
print("Bob's private key (R) : {}".format(r))
h = shared_secret(g,x,p)
print("Shared secret (H) : {}".format(h))
c1, c2 = encrypt(m,r,g,p,h)
print("Encrypted Message (C1): {}".format(c1))
print("Encrypted Message (C2): {}".format(c2))
dm = decrypt(x,c1,c2,p)
print("Decrypted Integer (dm): {}".format(dm))
x = format(dm, 'x')
print("Decrypted Hex (x) : {}".format(x))
message = unhexlify(x)
print("Decrypted Message : {}".format(message))