Java Utililty Methods BigInteger Calculate

List of utility methods to do BigInteger Calculate

Description

The list of methods to do BigInteger Calculate are organized into topic(s).

Method

byterightshift(byte b1, BigInteger b2)
rightshift
return (byte) ((b1 & 0xFF) >>> b2.intValue());
booleansaveKey(File destDir, String fileName, BigInteger modulus, BigInteger publicExponent)
save Key
try {
    FileOutputStream fos = new FileOutputStream(destDir + File.separator + fileName);
    ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(fos));
    oos.writeObject(modulus);
    oos.writeObject(publicExponent);
    oos.close();
    fos.close();
    return true;
...
voidsaveToFile(String fileName, BigInteger mod, BigInteger exp)
Save modulus and exponent of a key to file
ObjectOutputStream oout = null;
try {
    oout = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(fileName)));
    oout.writeObject(mod);
    oout.writeObject(exp);
} catch (IOException e) {
    e.printStackTrace();
} finally {
...
StringserializeModexpNoBase(BigInteger[] modexp, boolean withResult)
Helper method creating the JSON string for a single modexp without base and with or without given result.
return withResult
        ? String.format("{\"m\":\"%s\",\"e\":\"%s\",\"r\":\"%s\"}", modexp[0].toString(16),
                modexp[2].toString(16), modexp[3].toString(16))
        : String.format("{\"m\":\"%s\",\"e\":\"%s\"}", modexp[0].toString(16), modexp[2].toString(16));
StringserializeModexpResponse(BigInteger response)
serialize Modexp Response
return String.format("{\"r\":\"%s\"}", response.toString(16));
StringserializeQuery(BigInteger modulus, BigInteger base, BigInteger exponent, boolean brief, String... modexps)
Helper method creating the JSON string for a modexp-queryResponse with optional default base, exponent and modulus.
String query = "{";
query = modulus != null ? query + "\"m\":\"" + modulus.toString(16) + "\"," : query;
query = base != null ? query + "\"b\":\"" + base.toString(16) + "\"," : query;
query = exponent != null ? query + "\"e\":\"" + exponent.toString(16) + "\"," : query;
query = brief ? query : query + "\"brief\":false,";
query = modexps.length > 0 ? query + "\"modexps\":[" : query;
for (int i = 0; i < modexps.length; i++) {
    query += modexps[i];
...
BigIntegershift(BigInteger integer, int distance)
Returns a BigInteger whose value is shifted by the given distance.
if (integer == null)
    return null;
return integer.shiftLeft(distance);
BigIntegershiftLeft(final BigInteger register, final BigInteger input, final int n)
Shifts register left by n bits and sets these n bits to the n rightmost bits of input.
final BigInteger cutSuffix = getNRightmostBits(input, n);
return register.shiftLeft(n).or(cutSuffix);
BigInteger[]split(BigInteger maxValue, int numberOfSplits)
Splits the given BigInteger into numberOfSplits parts
BigInteger[] splits = new BigInteger[numberOfSplits - 1];
BigInteger sizeOfEachSplit = maxValue.divide(BigInteger.valueOf(numberOfSplits));
for (int i = 1; i < numberOfSplits; i++) {
    splits[i - 1] = sizeOfEachSplit.multiply(BigInteger.valueOf(i));
return splits;
BigInteger[]splitBigInt(BigInteger value, int n)
split Big Int
if (n <= 1) {
    return new BigInteger[] { value };
BigInteger[] values = new BigInteger[n];
BigInteger total = BigInteger.ZERO;
for (int i = 0; i < n; ++i) {
    values[i] = value.multiply(BigInteger.valueOf(Math.round(Math.random() * 100)));
    total = total.add(values[i]);
...