计算Java中两个BigIntegers的指数(实现社会主义百万富翁)

use*_*825 2 java cryptography biginteger

我正在尝试将Python文件移植到Java,但是我遇到了一些麻烦(可能是因为我对Python的了解有限).python文件是实现社会主义百万富翁问题的一个例子.我在处理BitInteger操作时遇到了一些问题.

蟒蛇:

def step1(self):
    self.x2 = createRandomExponent()
    self.x3 = createRandomExponent()

    self.g2 = pow(self.gen, self.x2, self.mod)
    self.g3 = pow(self.gen, self.x3, self.mod)

    (c1, d1) = self.createLogProof('1', self.x2)
    (c2, d2) = self.createLogProof('2', self.x3)

    # Send g2a, g3a, c1, d1, c2, d2
    return packList(self.g2, self.g3, c1, d1, c2, d2)
Run Code Online (Sandbox Code Playgroud)

Java的:

public BigInteger[] step1() {
    x2 = getRandomExponent();
    x3 = getRandomExponent();

    g2 = new BigInteger(gen + "").pow(x2.intValue()).pow(mod.intValue());
    g3 = new BigInteger(gen + "").pow(x3.intValue()).pow(mod.intValue());

    BigInteger[] logProof1 = createLogProof("1", g2);
    BigInteger[] logProof2 = createLogProof("2", g3);

    BigInteger c1 = logProof1[0];
    BigInteger d1 = logProof1[1];
    BigInteger c2 = logProof2[0];
    BigInteger d2 = logProof2[1];

    return new BigInteger[] { g2, g3, c1, d1, c2, d2 };
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误(第24行是计算g2的地方):

Exception in thread "main" java.lang.ArithmeticException: Negative exponent
    at java.math.BigInteger.pow(BigInteger.java:1395)
    at n/a.crypto.SMPCheck.step1(SMPCheck.java:42)
    at n/a.Testing.main(Testing.java:24)
Run Code Online (Sandbox Code Playgroud)

这是因为BigInteger.intValue()在调用时产生负数.有没有人有计算两个BigIntegers指数的解决方案?

Python来源:http://shanetully.com/2013/08/mitm-protection-via-the-socialist-millionaire-protocol-otr-style/

Pet*_*rey 5

使用高功率然后执行mod非常慢.出于这个原因,BigInteger提供了一个modPow(BigInteger,BigInteger)方法,它看起来像你想要的那样.