Many public key methods use a \(\pmod p\) operation and where \(p\) is a prime number. We can generate a prime number of a given size in order to define the strength of the security. For RSA, the prime number typically is 2,048 bits long.
Generate Prime Numbers with Given Number of Bits |
Coding
The coding is here:
import sys import libnum bitsize=40 if (len(sys.argv)>1): bitsize=int(sys.argv[1]) r=libnum.randint_bits(bitsize) print ("Random: %d Length: %d" % (r,libnum.len_in_bits(r))) p=libnum.generate_prime(bitsize) print ("\nPrime (p): %d. Length: %d bits, Digits: %d" % (p,libnum.len_in_bits(p), len(str(p)) )) q=libnum.generate_prime(bitsize) print ("\nPrime (q): %d. Length: %d bits, Digits: %d" % (q,libnum.len_in_bits(q), len(str(q)) )) N=p*q print ("\nPrime (N): %d. Length: %d bits, Digits: %d" % (N,libnum.len_in_bits(N), len(str(N)) ))
A sample run:
Random: 1030183127192836844195769 Length: 80 Prime (p): 864304872207118774292573. Length: 80 bits, Digits: 24 Prime (q): 1021474090220744263000943. Length: 80 bits, Digits: 25 Prime (N): 882865033011123283515906055612738686262856896339. Length: 160 bits, Digits: 48