![](/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.