Here you can find the source of modPow(BigInteger base, BigInteger e, BigInteger m)
public static BigInteger modPow(BigInteger base, BigInteger e, BigInteger m)
//package com.java2s; //License from project: Open Source License import java.math.BigInteger; public class Main { public static BigInteger modPow(BigInteger base, BigInteger e, BigInteger m) { BigInteger result;//from w ww .j a v a 2 s. c o m result = BigInteger.ONE; base = base.mod(m); for (int i = 0; i < e.bitLength(); ++i) { if (e.testBit(i)) { result = result.multiply(base).mod(m); } base = base.multiply(base).mod(m); } return result; } }