How can I do the same as in this python (using sage) code:
def elGamalDecrypt(c1, c2, p, x):
return Mod(c2*c1^(-x),p)
with standard Java 7 libraries? All numbers are BigInteger
.
I've tried a lot with no avail. In Python it's really simple and FAST.
The BigInteger class in Java 7 has a modPow
method, which handles modular exponentiation. So, something like the following should work (although I haven't tested it):
c2.multiply(c1.modPow(x.negate(), p)).mod(p)
The modPow
method will only accept a negative exponent -x
if c1
and p
are coprime. (The name p
suggests that p
is prime, and if c1
and p
aren't coprime, c1
would be divisible by p
, and hence the exponentiation would make no sense, so I suspect that this won't be a problem.)
This should do the same since c2 * c1^-x = c2 / (c1 ^ x)
:
BigInteger elGamalDecrypt(BigInteger c1, BigInteger c2, BigInteger p, int x) {
return c2.divide(c1.pow(x)).mod(p);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.