Overflow Error w/ Python Implementation of El Gamal Encryption-Decryption with PyCrypto

1.9k Views Asked by At

I am currently using the PyCrypto library in order to implement ElGamal encryption for an academic assignment and I am getting the following error:

OverflowError: cannot fit 'int' into an index-sized integer

from Crypto import Random
from Crypto.Random import random
from Crypto.PublicKey import ElGamal
from Crypto.Util.number import GCD


message = "Hello!"

key = ElGamal.generate(1024, Random.new().read)

while 1:
    k = random.StrongRandom().randint(1, key.p - 1)

    if GCD(k, key.p - 1) == 1:
        break

h = key.encrypt(message, k)

d = key.decrypt(h)
print(d)

I am not sure if I am reading the documentation incorrectly but I am basing it around this page:

https://www.dlitz.net/software/pycrypto/api/current/Crypto.PublicKey.ElGamal.ElGamalobj-class.html#encrypt

If anybody has any code examples of the proper implementation I would appreciate it.

1

There are 1 best solutions below

0
SoySolisCarlos On

instead of:

message = "Hello!"

In python 3 you must convert the text string into binaries, as follows:

message = b"Hello!"

Simply by adding a b before the quotation marks in the text.