## Random Prime Numbers[Back] Within public-key encryption we often have to generate a random prime number. This page uses a prime number generator, which starts of with a random number and then keeps decrementing until the number is prime (with 100% confidence). [8-bit] [16-bit] [32-bit] [64-bit] [96-bit] [128-bit] [256-bit] [384-bit] [512-bit] [1024-bit] [1280-bit]
## CodeWith the code we start off with a random number generated from the SecureRandom class, and then create a Big Integer from this. Next we test if it is prime, if not, we decrement it by one, and test again. This is then repeated until we find a prime number: static public Org.BouncyCastle.Math.BigInteger genpr2(int bits) { Org.BouncyCastle.Security.SecureRandom ran = new Org.BouncyCastle.Security.SecureRandom(); Org.BouncyCastle.Math.BigInteger c = new Org.BouncyCastle.Math.BigInteger(bits, ran); for (; ; ) { if (c.IsProbablePrime(1) == true) break; c = c.Subtract(new Org.BouncyCastle.Math.BigInteger("1")); } return (c); } |