我认为问题可能是你的指数溢出了两倍,导致无穷大,每次都产生相同的值(除非你很幸运,最终为你的指数返回一个非常低的数字).
此外,请务必使用安全随机来获取随机值:
Random random = new SecureRandom();
// If you use more than 100 here, then
// with your value of 486 for G you will
// end up with infinity when doing Math.pow(G,Xa).
// Of course, this does not provide enough possible
// values to be cryptographically secure.
int Xa = random.nextInt(100);
int Ya = (int) (Math.pow(G, Xa) % P);
编辑:调试代码(以下对我有用):
double G = 42;
int P = 26;
Random random = new SecureRandom();
int Xa = random.nextInt(100);
double val = Math.pow(G, Xa);
System.out.println("Xa: " + Xa);
System.out.println("(double) Math.pow: " + val + " (int): " + (int) val);
int Ya = (int) (val % P);
System.out.println("Ya: " + Ya);