6

I'm trying to generate a large prime number (2048 bits) using the crypto library in python in order to implement RSA. However, I do not really understand the syntax of the getPrime() function. I currently have:

from Crypto.Util import number

n_length = 2048

primeNum = number.getPrime(n_length, randFunc)

I don't understand what the randFunc is supposed to be in the getPrime function.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
winsticknova
  • 365
  • 2
  • 5
  • 17
  • 1
    This is a question about python; perhaps stack-exchange would be a better place? – poncho Feb 02 '16 at 21:49
  • I think the question is off-topic because software related. The randFunc is a function that outputs random bytes. From the API docs (https://www.dlitz.net/software/pycrypto/api/current/Crypto.Util.number-module.html#getPrime): getPrime(N, randfunc=None) getPrime(N:int, randfunc:callable):long Return a random N-bit prime number. If randfunc is omitted, then Random.new().read is used. – ddddavidee Feb 02 '16 at 21:51
  • 1
    You might also want to try implementing your own large prime number function. The Fermat primality test is very simple to implement. – Daffy Feb 02 '16 at 22:43
  • Does that library deliver provable primes or, as is commonly the case with software for large prime number generations, only statistically highly probable primes based on e.g. the Miller-Rabin test? In the 2nd case you may consider preferring to use provable primes instead. I have a Python code implementing Maurer's algorithm of generation of provable primes, with fairly comparable efficiency to employment of Miller-Rabin. See http://s13.zetaboards.com/Crypto/topic/7234475/1/ – Mok-Kong Shen Feb 03 '16 at 13:03
  • 1
    One way to do this is to use RSA library. You can call, `from Crypto.PublicKey import RSA`. Then you create the object, `keyA = RSA.generate(2048)`. Finally, you use `keyA.p;keyA.q`. – 111 Feb 03 '16 at 14:14
  • [typo for the previous comment] since you want 2048 prime number, you have to write `keyA = RSA.generate(4096)` – 111 Feb 03 '16 at 14:40

1 Answers1

8

n_length is the "size" of the prime number. It will return a number around 2^n_length. randFunc is a callable function that accepts a single argument N and then returns a string of N random bytes. (os.urandom is an example of this). In most cases, randFunc can (and should) be omitted, since the default is PyCrypto's own random number generator.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Daffy
  • 841
  • 9
  • 23
  • I tried to omit the randFunc part of it, but I get an error saying that the function takes exactly 2 arguments, when only 1 is given. This is when I try this line: primeNum = number.getPrime(n_length) – winsticknova Feb 02 '16 at 23:04
  • 1
    `>>> number.getPrime(16) # 33037L` Do you have a recent version of python and pycrypto? If you're still having issues, try this. `import os; number.getPrime(nbits, os.urandom)` – Daffy Feb 03 '16 at 00:38
  • 1
    yeah. I realized that my pypcrypto was not up-to-date, so I needed to add the os.urandom. thanks! – winsticknova Feb 03 '16 at 02:51