0

我尝试通过java程序计算ELGAMAL生成密钥的执行时间,但我发现了问题。

生成器 g 是众所周知的。最初,任何数 {0,…,n−1}

当尝试计算生成密钥的时间时,生成器密钥需要很长时间,特别是在使用 3072 位作为素数时?我的算法如下所示

long start=System.nanoTime();
    Random r = new Random();    
    BigInteger p =BigInteger.probablePrime(3072, r);
    // Calculate a generator.
    BigInteger g = getGenerator(p, r);
        public static BigInteger getNextPrime(String ans) {

            BigInteger one = new BigInteger("1");
            BigInteger test = new BigInteger(ans);
            while (!test.isProbablePrime(99))
                test = test.add(one);
            return test;        
        }


        // Precondition - p is prime and it's reasonably small, say, no more than 
        //                5,000,000. If it's larger, this method will be quite
        //                time-consuming.
        // Postcondition - if a generator for p can be found, then it is returned
        //                 if no generator is found after 1000 tries, null is 
        //                 returned.
        public static BigInteger getGenerator(BigInteger p, Random r) {

            int numtries = 0;

            // Try finding a generator at random 100 times.
            while (numtries < 1000) {

                // Here's what we're trying as the generator this time.
                BigInteger rand = new BigInteger(p.bitCount()-1,r);

                BigInteger exp = BigInteger.ONE;
                BigInteger next = rand.mod(p);

                // We exponentiate our generator until we get 1 mod p.
                while (!next.equals(BigInteger.ONE)) {
                    next = (next.multiply(rand)).mod(p);
                    exp = exp.add(BigInteger.ONE);
                }

                // If the first time we hit 1 is the exponent p-1, then we have
                // a generator.
                if (exp.equals(p.subtract(BigInteger.ONE)))
                    return rand;
            }

            // None of the 1000 values we tried was a generator.
            return null;

      }
 long elapse=System.nanoTime()-start;

关于我的问题的任何建议

4

0 回答 0