1

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.

marifkind
  • 115
  • 11
  • Wanted to add that the error is occurring on line 15 on the encrypt function call. – marifkind May 28 '19 at 21:38
  • Not too familiar w/ Python...any chance that it is lazy-evaluating `k`, and running into a type issue related to `int` vs. `long`? Maybe try printing `k` just before `key.encrypt` to see. – lockcmpxchg8b Jun 01 '19 at 02:40
  • We cannot see lines and you cannot expect us to count them, and we would need the full stack trace to see where the error is. There is no direct indexation in the code, it seems. – Maarten Bodewes Jun 04 '19 at 23:19

1 Answers1

1

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.

SoySolisCarlos
  • 736
  • 1
  • 6
  • 13