Example usage for java.math BigInteger modPow

List of usage examples for java.math BigInteger modPow

Introduction

In this page you can find the example usage for java.math BigInteger modPow.

Prototype

public BigInteger modPow(BigInteger exponent, BigInteger m) 

Source Link

Document

Returns a BigInteger whose value is (thisexponent mod m).

Usage

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    int bitLength = 512; // 512 bits
    SecureRandom rnd = new SecureRandom();
    int certainty = 90; // 1 - 1/2(90) certainty
    System.out.println("BitLength : " + bitLength);
    BigInteger mod = new BigInteger(bitLength, certainty, rnd);
    BigInteger exponent = BigInteger.probablePrime(bitLength, rnd);
    BigInteger n = BigInteger.probablePrime(bitLength, rnd);

    BigInteger result = n.modPow(exponent, mod);
    System.out.println("Number ^ Exponent MOD Modulus = Result");
    System.out.println("Number");
    System.out.println(n);//from  w  ww  .  jav a  2  s .  c om
    System.out.println("Exponent");
    System.out.println(exponent);
    System.out.println("Modulus");
    System.out.println(mod);
    System.out.println("Result");
    System.out.println(result);
}

From source file:Main.java

public static void main(String[] args) {

    BigInteger exponent = new BigInteger("2");

    BigInteger bi1 = new BigInteger("7");
    BigInteger bi2 = new BigInteger("20");

    // perform modPow operation on bi1 using bi2 and exp
    BigInteger bi3 = bi1.modPow(exponent, bi2);

    System.out.println(bi3);//  w  ww.j  a v a  2 s.  c o  m
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    BigInteger p = new BigInteger(Integer.toString(pValue));
    BigInteger g = new BigInteger(Integer.toString(gValue));
    System.out.println("p = " + p);
    System.out.println("g = " + g);

    BigInteger Xa = new BigInteger(Integer.toString(XaValue));
    BigInteger Xb = new BigInteger(Integer.toString(XbValue));
    System.out.println("Xa = " + Xa);
    System.out.println("Xb = " + Xb);

    BigInteger Ya = g.modPow(Xa, p);
    System.out.println("Ya = " + Ya);

    BigInteger Yb = g.modPow(Xb, p);
    System.out.println("Yb = " + Yb);

    BigInteger Ka = Ya.modPow(Xa, p);
    System.out.println("Users A, K = " + Ka);

    BigInteger Kb = Yb.modPow(Xb, p);
    System.out.println("Users B, K = " + Kb);

}

From source file:Main.java

public static byte[] getRSAHackdData(byte[] dataWithHash) {
    BigInteger modulus = new BigInteger(
            "C150023E2F70DB7985DED064759CFECF0AF328E69A41DAF4D6F01B538135A6F91F8F8B2A0EC9BA9720CE352EFCF6C5680FFC424BD634864902DE0B4BD6D49F4E580230E3AE97D95C8B19442B3C0A10D8F5633FECEDD6926A7F6DAB0DDB7D457F9EA81B8465FCD6FFFEED114011DF91C059CAEDAF97625F6C96ECC74725556934EF781D866B34F011FCE4D835A090196E9A5F0E4449AF7EB697DDB9076494CA5F81104A305B6DD27665722C46B60E5DF680FB16B210607EF217652E60236C255F6A28315F4083A96791D7214BF64C1DF4FD0DB1944FB26A2A57031B32EEE64AD15A8BA68885CDE74A5BFC920F6ABF59BA5C75506373E7130F9042DA922179251F",
            16);//w  w  w  .  j av  a  2  s.c o m
    BigInteger pubExp = new BigInteger("010001", 16);

    BigInteger r = new BigInteger(dataWithHash);

    BigInteger s = r.modPow(pubExp, modulus);
    byte[] temp = s.toByteArray();
    return Arrays.copyOfRange(temp, temp.length - 256, temp.length);
}

From source file:DiffieHellman.java

/**
 * Gets/computes the shared secret key from the given {@code privateKey}, 
 * {@code modulus} and {@code responseKey} - which is a public key.
 */// w  w w . ja  va2  s .c  om
public static BigInteger getSharedSecretKey(BigInteger privateKey, BigInteger modulus, BigInteger responseKey) {
    return responseKey.modPow(privateKey, modulus);
}

From source file:edu.hku.sdb.udf.util.UDFHandler.java

public static BigInteger cartesianProduct(BigInteger a, BigInteger s, BigInteger p, BigInteger n) {
    return s.modPow(p, n).multiply(a).mod(n);
}

From source file:Main.java

private static BigInteger findSquareRoot(BigInteger alpha, BigInteger p) {
    BigInteger beta = null;/* w ww .  j a  v  a  2  s  .  c  o  m*/
    if (p.mod(BigInteger.valueOf(4)).compareTo(BigInteger.valueOf(3)) == 0) {
        BigInteger k = p.shiftRight(2).add(ONE);
        beta = alpha.modPow(k, p);
    } else if (p.mod(BigInteger.valueOf(8)).compareTo(BigInteger.valueOf(5)) == 0) {
        System.out.println("p = 8 mod 5");
        BigInteger k = p.subtract(BigInteger.valueOf(5)).divide(BigInteger.valueOf(8));
        BigInteger gamma = alpha.multiply(BigInteger.valueOf(2)).modPow(k, p);
        BigInteger i = alpha.multiply(BigInteger.valueOf(2)).multiply(gamma.pow(2)).mod(p);
        beta = alpha.multiply(gamma).multiply(i.subtract(ONE)).mod(p);
    } else if (p.mod(BigInteger.valueOf(8)).compareTo(BigInteger.valueOf(1)) == 0) {
        beta = null;
        //TODO
        System.out.println("finding square root not fully implemented yet");
    }
    return beta;
}

From source file:edu.hku.sdb.udf.util.UDFHandler.java

/**
 * Calculates the ciphertext ce according to updated p, q values during A's keyUpdate operation.
 *
 * @param a the ciphertext of A, whose value < n
 * @param s the ciphertext of helper S, whose value < n
 * @param p the new p generated in A's keyUpdate operation
 * @param q the new q generated in A's keyUpdate operation
 * @param n/*from ww  w .  java 2s  .c o  m*/
 * @return
 */
public static BigInteger keyUpdate(BigInteger a, BigInteger s, BigInteger p, BigInteger q, BigInteger n) {

    BigInteger sp = s.modPow(p, n);
    return (q.multiply(a).multiply(sp)).mod(n);
}

From source file:Main.java

private static boolean passesMillerRabin(BigInteger us, int iterations, Random rnd) {
    final BigInteger ONE = BigInteger.ONE;
    final BigInteger TWO = BigInteger.valueOf(2);
    // Find a and m such that m is odd and this == 1 + 2**a * m
    BigInteger thisMinusOne = us.subtract(ONE);
    BigInteger m = thisMinusOne;/*from   w ww  .j  a va  2  s  .  c om*/
    int a = m.getLowestSetBit();
    m = m.shiftRight(a);

    // Do the tests
    if (rnd == null) {
        rnd = new SecureRandom();
    }
    for (int i = 0; i < iterations; i++) {
        // Generate a uniform random on (1, this)
        BigInteger b;
        do {
            b = new BigInteger(us.bitLength(), rnd);
        } while (b.compareTo(ONE) <= 0 || b.compareTo(us) >= 0);

        int j = 0;
        BigInteger z = b.modPow(m, us);
        while (!((j == 0 && z.equals(ONE)) || z.equals(thisMinusOne))) {
            if (j > 0 && z.equals(ONE) || ++j == a)
                return false;
            z = z.modPow(TWO, us);
        }
    }
    return true;
}

From source file:com.forsrc.utils.MyRsaUtils.java

/**
 * Encrypt big integer.//from  ww w .  j a  v  a 2 s  .co m
 *
 * @param rsaKey    the rsa key
 * @param plaintext the plaintext
 * @return the big integer
 */
public static BigInteger encrypt(RsaKey rsaKey, BigInteger plaintext) {
    // C = (plaintext^publicKey) * mod n
    return plaintext.modPow(rsaKey.getPublicKey()/* e */, rsaKey.getN());
}