0

I am using PyCryptodome libray for my cryptography tasks. I want to generate RSA key as shown in example.

I have typed this code:

from Cryptodome.PublicKey import RSA
key = RSA.generate(2048)

and it takes forever to execute. (While typing this question it has passed 10 minutes already and it is still not completed).

I am running it in Jupyter Notebook on Windows 10. Do you maybe have any idea why is it so slow or how to make it work? I have read documentation and tried to find similar questions but without any success.

i_rezic
  • 372
  • 1
  • 15
  • I dont know why, but it seems that this problem happens to some of my colleagues too. For now I will use old PyCrypto for RSA and PyCryptodome for others. – i_rezic May 23 '18 at 09:50
  • I have looked into the issue, since I experience the same never ending key generation. Generate calls generate_probable_prime which in turn calls test_probable_prime which calls miller_rabin_test which calls candidate.is_even() which calls _gmp.mpz_tstsbit which then always returns true. This is clearly a bug,since several candidates that was not even still returned True. – Ove Jun 13 '18 at 12:42
  • This is due to a bad interaction between pycryptodome and the MPIR library that conda happens to rename to gmp.dll. [It is fixed in pycryptodome and pycryptodomex version 3.6.2](https://github.com/Legrandin/pycryptodome/issues/172). – SquareRootOfTwentyThree Jun 20 '18 at 18:09

3 Answers3

1

I have reinstalled Anaconda to the latest version and this time I have installed pycryptodomex through pip(as shown on github), previously I installed it through Anaconda cloud (I think this doesn't matter, but let it stay here, just to be sure)

i_rezic
  • 372
  • 1
  • 15
1

It seems there is a bug in pycryptodome. The primality test miller_rabin_test calls is_even on the candidate integer which incorrectly always returns true, leading to a never ending loop. I replaced line 579 in site-packages/Cryptodome/Math/_Numbers_gmp.py with

def is_even(self):
    return (int(self) & 1)==0        
    #return _gmp.mpz_tstbit(self._mpz_p, 0) == 0 #Old non-functional line of code.

Tested and working here.

Ove
  • 125
  • 1
  • 6
0

That takes me less than a second (on average) on an average laptop with Windows 7 64 bits.

Are you sure you are using the most recent version of the pycryptodomex package (3.6.1)?

In earlier versions, you had to install a separate optimized numerical library (MPIR) to get decent performance: the alternative was pure Python based, and it was indeed very slow.

  • Afer typing "conda list" in my Anaconda Prompt this is what I get **pycryptodomex 3.6.1 py35h65cbd90_0 anaconda** so I think I do – i_rezic May 23 '18 at 19:59