如何为 Okamoto Uchiyama 密码系统生成 public 密钥?

How to generate public key for Okamoto Uchiyama Cryptosystem?

我正在 Java 中实施 Okamoto Uchiyama 密码系统。算法是here

对于 public 键,参数值 h 计算为,

h = g^n mod n

其中,

g是素数的原根p(340位)

n = p^2 * q (1024bits)

我为 ghn 使用了 BigInteger 类型。当我运行代码时,它没有终止。这是代码。请建议我如何计算 h.

import java.math.BigInteger;
import java.util.Random;

public class KeyPairGeneration {

    PrimeGenerator PrimeGeneratorObject = new PrimeGenerator();
    BigInteger PrivateFields[] = new BigInteger[2];
    BigInteger PublicFields[] = new BigInteger[4];
    BigInteger ONE = new BigInteger("1");
    BigInteger DECREMENT = new BigInteger("-1");

    /**
     * Generate Private Key with two large primes
     */
    public BigInteger[] PrivateKeyGenerator() {
        PrivateFields[0] = PrimeGeneratorObject.RandomPrime();      // p
        PrivateFields[1] = PrimeGeneratorObject.RandomPrime();      // q

        if(PrivateFields[0] != PrivateFields[1])
            return PrivateFields;
        else
            return PrivateKeyGenerator();
    }

    /**
     * Generate Public Key from Private Key
     */
    public BigInteger[] PublicKeyGenerator() {

        PublicFields[0] = PrivateFields[0].multiply(PrivateFields[0]).multiply(PrivateFields[1]);   // n = p*p*q

        BigInteger g = new BigInteger("1");
        Random randomGenerator = new Random();
        for (int idx = 1; idx <= PrivateFields[0].intValue(); ++idx){
          int randomInt = randomGenerator.nextInt(100);
          if (primitiveRoot(randomInt, PrivateFields[0].intValue())) {
              BigInteger primitiveRoot = new BigInteger(String.valueOf(randomInt));

              PublicFields[1] = g.multiply(primitiveRoot);                                          // g = primitive root of P
              break;
          }
        }

        int randomizationValue = randomGenerator.nextInt(100);
        PublicFields[2] = BigInteger.valueOf(randomizationValue);                                   // r < n

        PublicFields[3] = PublicFields[1].modPow(PublicFields[0],PublicFields[0]);                  // h = g^n mod n

        return PublicFields;
    }

     public static boolean primitiveRoot(int a, int p) {
            int val = a, i;
            // Calculate each modular exponent a^2, a^3... mod p.
            for (i=2; i<p; i++) {
                val = val*a%p;
                if (val == 1)
                    break;
            }
            // If the exponent is p-1, it's a primitive root!
                return (i == p-1);
        }

}

p 不适合 int,所以 PrivateFields[0].intValue() 一定是错误的。您的 primitiveRoot 算法必须采用 BigInteger 而不是 int.