![](/img/trans.png)
[英]BigInteger exponentiation with BigInteger number: ArithmeticException, would overflow supported range
[英]BigInteger exponentiation with negative number
我该怎么做与此Python(使用鼠尾草)代码相同:
def elGamalDecrypt(c1, c2, p, x):
return Mod(c2*c1^(-x),p)
标准的Java 7库? 所有数字均为BigInteger
。
我已经尝试了很多无济于事。 在Python中,它非常简单且快速。
Java 7中的BigInteger类具有一个modPow
方法,该方法可处理模块化幂运算。 因此,类似以下的内容应该可以工作(尽管我还没有测试过):
c2.multiply(c1.modPow(x.negate(), p)).mod(p)
如果c1
和p
为互质数,则modPow
方法仅接受负指数-x
。 (名称p
表示p
是质数,如果c1
和p
不是互质数,则c1
将被p
整除,因此取幂是没有意义的,因此我怀疑这不会成为问题。)
由于c2 * c1^-x = c2 / (c1 ^ x)
因此应该这样做:
BigInteger elGamalDecrypt(BigInteger c1, BigInteger c2, BigInteger p, int x) {
return c2.divide(c1.pow(x)).mod(p);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.