Out of boredom, intends to implement the RSA algorithm again
The first step, large prime number generation
In Java's BigInteger, there's a ready-made method.
public static BigInteger probableprime (int bitlength, Random rnd) {
Bitlength is the number of bits that is expected to be generated, and Rnd is the random number generator
The function note indicates that the return value of this method is composite and the probability is 2^-100
Generates 100 1024-bit primes, time consuming 13471ms
But obviously I'm not going to use this function directly, to do it from the bottom up!
The current practice is based on the primality detection
If a is an integer, p is a prime number, and A,p coprime (that is, there is only one convention of 1), then the remainder of a (p-1) divided by P is constant equal to 1.
In other words, if p is a prime number, then for any a<p, there is
A ^ p% p = = A was established
And its inverse proposition has at least 1/2 probability of being set up.
Then we can reduce the probability of the occurrence of false primes by multiple primality detection.
The prime number theorem also points out that the density of prime numbers is inversely proportional to ln (x), that is, we can randomly generate an n-bit integer, and if not prime, continue to take it backwards, then, about N, you can hit a prime.
That's probably how it works.
There are some optimizations in the middle to reduce the direct calculation of large integers
Enclose the code as follows
1 /**2 * Calculation base^exp% n3 *4 * @paramBase5 * @paramExp6 * @paramN7 * @return8 */9 Public StaticBigInteger Expmod (intBase, BigInteger exp, BigInteger N) {Ten if(Exp.equals (Biginteger.zero)) { One returnBiginteger.one; A } - - if(!exp.testbit (0)) {//If an even number the returnExpmod (Base, Exp.divide (Biginteger.valueof (2)), N). Pow (2). remainder (n); -}Else { - return(Expmod (Base, Exp.subtract (Biginteger.one). Divide (biginteger.valueof (2)), N). Pow (2). Multiply (biginteger.valueof (base)). Remainder (n); - } + } - + /** A * Horse Test, if return false, then n must be composite, if true, then n is more than half the probability of prime at * - * @paramN - * @return - */ - Public Static BooleanFermattest (BigInteger N) { - intBase = 0; in if(N.compareto (biginteger.valueof (integer.max_value)) < 0) { -Base = Ran.nextint (N.intvalue ()-1) + 1; to}Else { +Base = Ran.nextint (integer.max_value-1) + 1; - } the if(Expmod (base, N, N). Equals (Biginteger.valueof (base))) { * return true; $}Else {Panax Notoginseng return false; - } the } + A the Public Static BooleanIsPrime (BigInteger N) { + returnIsPrime (N, 100); - } $ $ /** - * Multiple calls to Fermat test to determine whether N is a prime number - * the * @paramN - * @paramTrytimeWuyi * @return the */ - Public Static BooleanIsPrime (BigInteger N,inttrytime) { Wu for(inti = 0; i < trytime; i++) { - if(!fermattest (n)) { About return false; $ } - } - return true; - } A + /** the * generates an n-bit prime number - * $ * @paramBitcount the * @return the */ the Public StaticBigInteger Getprime (intBitcount) { the //randomly generate a large integer of n bit -BigInteger init =NewBigInteger (Bitcount, ran); in //if n is an even number, the addition becomes an odd number the if(!init.testbit (0)) { theinit = init.setbit (0); About } the inti = 0; the //based on the prime number theorem, a prime number can be found on average with less than n searches. the while(!IsPrime (init)) { +i++; -init = Init.add (biginteger.valueof (2)); the }Bayi //System.out.println (String.Format ("Try%d\ttimes", i)); the returnInit; the}
View Code
The RSA algorithm of repeating wheel making (a) large prime number generation