Example usage for java.math BigInteger mod

List of usage examples for java.math BigInteger mod

Introduction

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

Prototype

public BigInteger mod(BigInteger m) 

Source Link

Document

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

Usage

From source file:com.og.elliptic.sdk.main.java

private static void elGamalString(WeierStrassCurve curve) {
    // Droulement de El Gamal
    // Cryptage/*from ww w. ja  va2s .c  o  m*/
    PointGMP generateur = new PointGMP(curve.getGx(), curve.getGy(), curve);// le point gnrateur qui servira  calculer notre clef
    // secrte
    Random rnd = new Random();

    BigInteger clefSecrete = BigInteger.probablePrime(7, rnd);
    PointGMP publicKey = generateur.mult(clefSecrete);// on calcule notre clef secrte, qui nous servira  crypter les messages (h)

    BigInteger m = new BigInteger(Base64.decodeBase64("bonjour lesil"));

    BigInteger k = BigInteger.probablePrime(7, rnd);// on choisit un k random

    PointGMP C1 = generateur.mult(k);// on multiplie le gnrateur par k
    PointGMP tmp = publicKey.mult(k);
    BigInteger C2 = m.add(tmp.getX());// on multiplie notre clef secrte par k

    // Dcryptage
    PointGMP temp = C1.mult(clefSecrete);// on multiplie C1 par notre clef secrte
    BigInteger messageTraduit = C2.subtract(temp.getX());// et on obtient le message en ajoutant m2 avec l'oppos de temp
    messageTraduit = messageTraduit.mod(curve.getP());

    if (m == messageTraduit)
        System.out.println("El Gamal fonctionnel");
    else
        System.out.println("El Gamal non fonctionnel");

    System.out.println("Message a retrouver " + m + " " + Base64.encodeBase64String(m.toByteArray()) + "\n");
    System.out.println("Resultat El gamal " + messageTraduit + " "
            + Base64.encodeBase64String(messageTraduit.toByteArray()) + "\n");

}

From source file:Main.java

static BigInteger inverseMod(BigInteger a, BigInteger b) {
    BigInteger b0 = b, t, q;//w  ww  .j  av  a 2 s  .co  m
    BigInteger x0 = BigInteger.ZERO, x1 = BigInteger.ONE;
    if (b.equals(BigInteger.ONE))
        return BigInteger.ONE;
    while (a.subtract(BigInteger.ONE).signum() > 0) {
        q = a.divide(b);
        t = b;
        b = a.mod(b);
        a = t;
        t = x0;
        x0 = x1.subtract(q.multiply(x0));
        x1 = t;
    }
    if (x1.signum() < 0)
        x1 = x1.add(b0);
    return x1;
}

From source file:org.mozilla.gecko.sync.Utils.java

public static BigInteger generateBigIntegerLessThan(BigInteger r) {
    int maxBytes = (int) Math.ceil(((double) r.bitLength()) / 8);
    BigInteger randInt = new BigInteger(generateRandomBytes(maxBytes));
    return randInt.mod(r);
}

From source file:com.amazonaws.services.kinesis.log4j.helpers.AmazonKinesisPutRecordsHelper.java

public static int calculateShardBucket(String partitionKey, int totalNumOfShards) {
    MessageDigest m = null;/*  ww  w  .ja  v a  2  s  .  c  o m*/
    int shardBucket = 1;
    try {
        m = MessageDigest.getInstance("MD5");
        m.reset();
        m.update(partitionKey.getBytes());
        byte[] digest = m.digest();
        BigInteger bigInt = new BigInteger(1, digest);
        shardBucket = bigInt.mod(BigInteger.valueOf(totalNumOfShards)).intValue() + 1;
    } catch (NoSuchAlgorithmException e) {
        //ignore
    }
    return shardBucket;
}

From source file:burstcoin.jminer.core.round.Round.java

private static int calcScoopNumber(long blockNumber, byte[] generationSignature) {
    if (blockNumber > 0 && generationSignature != null) {
        ByteBuffer buf = ByteBuffer.allocate(32 + 8);
        buf.put(generationSignature);//from   ww w . j  a  v  a  2 s .  co  m
        buf.putLong(blockNumber);

        // generate new scoop number
        Shabal256 md = new Shabal256();
        md.update(buf.array());

        BigInteger hashnum = new BigInteger(1, md.digest());
        return hashnum.mod(BigInteger.valueOf(MiningPlot.SCOOPS_PER_PLOT)).intValue();
    }
    return 0;
}

From source file:Main.java

public static BigInteger getZ(ArrayList<byte[]> c1, ArrayList<byte[]> c2, BigInteger p) {
    BigInteger z = BigInteger.ZERO;

    //TODO: make sure c1 and c2 are of the same size
    int size = c1.size();
    if (size > c2.size()) {
        size = c2.size();/*from   w w  w  .  j  a v  a  2 s  .co  m*/
    }

    for (int i = 0; i < size; i++) {
        BigInteger c1BI = new BigInteger(1, c1.get(i));
        BigInteger c2BI = new BigInteger(1, c2.get(i));
        BigInteger exp = new BigInteger(1, ByteBuffer.allocate(8).putLong((long) Math.pow(2, i)).array());

        z = z.add((c1BI.multiply(c2BI)).modPow(exp, p));
        Log.d("CeCk", "z calculation " + i + "/" + size + " round");
    }
    return z.mod(p);
}

From source file:org.apache.pdfbox.pdmodel.encryption.StandardSecurityHandler.java

private static byte[] computeHash2B(byte[] input, byte[] password, byte[] userKey) throws IOException {
    try {//from   w  ww.  j av a 2 s  .  c o  m
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        byte[] k = md.digest(input);

        byte[] e = null;
        for (int round = 0; round < 64 || ((int) e[e.length - 1] & 0xFF) > round - 32; round++) {
            byte[] k1;
            if (userKey != null && userKey.length >= 48) {
                k1 = new byte[64 * (password.length + k.length + 48)];
            } else {
                k1 = new byte[64 * (password.length + k.length)];
            }

            int pos = 0;
            for (int i = 0; i < 64; i++) {
                System.arraycopy(password, 0, k1, pos, password.length);
                pos += password.length;
                System.arraycopy(k, 0, k1, pos, k.length);
                pos += k.length;
                if (userKey != null && userKey.length >= 48) {
                    System.arraycopy(userKey, 0, k1, pos, 48);
                    pos += 48;
                }
            }

            byte[] kFirst = new byte[16];
            byte[] kSecond = new byte[16];
            System.arraycopy(k, 0, kFirst, 0, 16);
            System.arraycopy(k, 16, kSecond, 0, 16);

            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            SecretKeySpec keySpec = new SecretKeySpec(kFirst, "AES");
            IvParameterSpec ivSpec = new IvParameterSpec(kSecond);
            cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
            e = cipher.doFinal(k1);

            byte[] eFirst = new byte[16];
            System.arraycopy(e, 0, eFirst, 0, 16);
            BigInteger bi = new BigInteger(1, eFirst);
            BigInteger remainder = bi.mod(new BigInteger("3"));
            String nextHash = HASHES_2B[remainder.intValue()];

            md = MessageDigest.getInstance(nextHash);
            k = md.digest(e);
        }

        if (k.length > 32) {
            byte[] kTrunc = new byte[32];
            System.arraycopy(k, 0, kTrunc, 0, 32);
            return kTrunc;
        } else {
            return k;
        }
    } catch (GeneralSecurityException e) {
        logIfStrongEncryptionMissing();
        throw new IOException(e);
    }
}

From source file:com.offbynull.peernetic.common.identification.IdGenerator.java

/**
 * Generates a {@link Id}.// w  w  w  .j  a v a 2 s.  c om
 * @return a {@link Id} between {@code 0} and {@code limit}
 * @throws NullPointerException if any arguments are {@code null}
 */
public Id generate() {
    try {
        int rawLen = limit.length;
        byte[] raw = new byte[rawLen];
        random.nextBytes(raw);

        BigInteger rawBd = new BigInteger(1, raw);
        BigInteger limitBd = new BigInteger(1, limit);

        rawBd = rawBd.mod(limitBd);

        return new Id(rawBd.toByteArray(), limit);
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}

From source file:org.stem.domain.DHT.java

public int getSection(byte[] keyBytes) {
    BigInteger token = new BigInteger(new String(Hex.encodeHex(keyBytes)), 16);
    BigInteger sectionSize = capacity.divide(BigInteger.valueOf(sections));

    int quotient = token.divide(sectionSize).intValue();
    BigInteger mod = token.mod(sectionSize);
    int index;//from   w  ww  .j a  va2s.  c o  m
    if (quotient == 0)
        index = 0;
    else if (mod.equals(BigInteger.ZERO))
        index = Math.max(quotient - 1, 0);
    else
        index = Math.max(quotient, 0);

    if (index >= sections) {
        return sections - 1;
    }

    return index;
}

From source file:org.trnltk.numeral.DigitsToTextConverter.java

private int getNthGroupNumber(BigInteger naturalNumber, int n) {
    naturalNumber = naturalNumber.divide(ONE_THOUSAND.pow(n));
    naturalNumber = naturalNumber.mod(ONE_THOUSAND);
    return naturalNumber.intValue();
}