List of usage examples for java.math BigInteger divideAndRemainder
public BigInteger[] divideAndRemainder(BigInteger val)
From source file:Main.java
public static void main(String[] args) { BigInteger bi1 = new BigInteger("-100"); BigInteger bi2 = new BigInteger("3"); // BigInteger array bi stores result of bi1/bi2 BigInteger bi[] = bi1.divideAndRemainder(bi2); System.out.println("Quotient is " + bi[0]); System.out.println("Remainder is " + bi[1]); }
From source file:Main.java
public static String toBase58(byte[] b) { if (b.length == 0) { return ""; }/*w ww.j a v a2 s.co m*/ int lz = 0; while (lz < b.length && b[lz] == 0) { ++lz; } StringBuffer s = new StringBuffer(); BigInteger n = new BigInteger(1, b); while (n.compareTo(BigInteger.ZERO) > 0) { BigInteger[] r = n.divideAndRemainder(BigInteger.valueOf(58)); n = r[0]; char digit = b58[r[1].intValue()]; s.append(digit); } while (lz > 0) { --lz; s.append("1"); } return s.reverse().toString(); }
From source file:Main.java
public static String encodeBase58(byte[] input) { if (input == null) { return null; }/*w w w . j a v a 2 s.c o m*/ StringBuilder str = new StringBuilder((input.length * 350) / 256 + 1); BigInteger bn = new BigInteger(1, input); long rem; while (true) { BigInteger[] divideAndRemainder = bn.divideAndRemainder(BASE58_CHUNK_MOD); bn = divideAndRemainder[0]; rem = divideAndRemainder[1].longValue(); if (bn.compareTo(BigInteger.ZERO) == 0) { break; } for (int i = 0; i < BASE58_CHUNK_DIGITS; i++) { str.append(BASE58[(int) (rem % 58)]); rem /= 58; } } while (rem != 0) { str.append(BASE58[(int) (rem % 58)]); rem /= 58; } str.reverse(); int nLeadingZeros = 0; while (nLeadingZeros < input.length && input[nLeadingZeros] == 0) { str.insert(0, BASE58[0]); nLeadingZeros++; } return str.toString(); }
From source file:Main.java
public static String BytesToBase58(byte[] value) { //Add 1 for each 00 byte. //From lowest base58 fill with division remainders. String returnValue = ""; boolean justStarted = true; BigInteger bigValue = new BigInteger(value); //TODO: Check that it works as it should. BigInteger base58 = new BigInteger("58"); BigInteger zero = new BigInteger("0"); BigInteger[] divisionResult;// www. ja v a 2 s. c o m while (bigValue.compareTo(zero) == 1) { //Means greater than. divisionResult = bigValue.divideAndRemainder(base58); bigValue = divisionResult[0]; returnValue = base58Array.toCharArray()[divisionResult[1].intValue()] + returnValue; } for (int i = 0; i < value.length; i++) { if (value[i] == 0 && justStarted) { returnValue = "1" + returnValue; } else { break; } justStarted = false; } return returnValue; }
From source file:cc.redberry.core.number.Exponentiation.java
static BigInteger findIntegerRoot(BigInteger base, BigInteger power) { BigInteger maxBits = BigInteger.valueOf(base.bitLength() + 1); // base < 2 ^ (maxBits + 1) // => base ^ ( 1 / power ) < 2 ^ ( (maxBits + 1) / power ) BigInteger[] divResult = maxBits.divideAndRemainder(power); if (divResult[1].signum() == 0) // i.e. divResult[1] == 0 maxBits = divResult[0];// ww w .ja v a2s . c o m else maxBits = divResult[0].add(BigInteger.ONE); if (maxBits.bitLength() > 31) throw new RuntimeException("Too many bits..."); int targetBitsNumber = maxBits.intValue(); int resultLengthM1 = targetBitsNumber / 8 + 1; //resultLength minus one byte[] result = new byte[resultLengthM1]; resultLengthM1--; int bitNumber = targetBitsNumber; int cValue; BigInteger testValue; while ((--bitNumber) >= 0) { //setting bit result[resultLengthM1 - (bitNumber >> 3)] |= 1 << (bitNumber & 0x7); //Testing testValue = new BigInteger(result); cValue = ArithmeticUtils.pow(testValue, power).compareTo(base); if (cValue == 0) return testValue; if (cValue > 0) result[resultLengthM1 - (bitNumber >> 3)] &= ~(1 << (bitNumber & 0x7)); } return null; }
From source file:com.kactech.otj.Utils.java
public static String base62Encode(BigInteger number) { if (number.compareTo(BigInteger.ZERO) == -1) { // number < 0 throw new IllegalArgumentException("number must not be negative"); }// w w w . jav a 2s . c om StringBuilder result = new StringBuilder(); while (number.compareTo(BigInteger.ZERO) == 1) { // number > 0 BigInteger[] divmod = number.divideAndRemainder(B62_BASE); number = divmod[0]; int digit = divmod[1].intValue(); result.insert(0, B62_DIGITS.charAt(digit)); } return (result.length() == 0) ? B62_DIGITS.substring(0, 1) : result.toString(); }
From source file:eu.dety.burp.joseph.attacks.bleichenbacher_pkcs1.BleichenbacherPkcs1DecryptionAttackExecutor.java
private BigInteger step3ComputeUpperBound(final BigInteger s, final BigInteger modulus, final BigInteger upperIntervalBound) { BigInteger upperBound = upperIntervalBound.multiply(s); upperBound = upperBound.subtract(BigInteger.valueOf(2).multiply(bigB)); // ceil//from ww w . j a va 2s.c o m BigInteger[] tmp = upperBound.divideAndRemainder(modulus); if (BigInteger.ZERO.compareTo(tmp[1]) != 0) { upperBound = BigInteger.ONE.add(tmp[0]); } else { upperBound = tmp[0]; } return upperBound; }
From source file:eu.dety.burp.joseph.attacks.bleichenbacher_pkcs1.BleichenbacherPkcs1DecryptionAttackExecutor.java
private void stepTwoA() throws Exception { byte[] send;//from w ww .j a v a 2s . c o m BigInteger n = this.pubKey.getModulus(); loggerInstance.log(getClass(), "Step 2a: Starting the search", Logger.LogLevel.INFO); // si = ceil(n/(3B)) BigInteger tmp[] = n.divideAndRemainder(BigInteger.valueOf(3).multiply(bigB)); if (BigInteger.ZERO.compareTo(tmp[1]) != 0) { this.si = tmp[0].add(BigInteger.ONE); } else { this.si = tmp[0]; } // correction will be done in do while this.si = this.si.subtract(BigInteger.ONE); IHttpRequestResponse response; byte[] request; do { // Check if user has cancelled the worker if (isCancelled()) { loggerInstance.log(getClass(), "Decryption Attack Executor Worker cancelled by user", Logger.LogLevel.INFO); return; } this.si = this.si.add(BigInteger.ONE); send = prepareMsg(this.c0, this.si); request = this.requestResponse.getRequest(); String[] components = Decoder.getComponents(this.parameter.getJoseValue()); components[1] = Decoder.base64UrlEncode(send); String newComponentsConcatenated = Decoder.concatComponents(components); request = JoseParameter.updateRequest(request, this.parameter, helpers, newComponentsConcatenated); response = callbacks.makeHttpRequest(this.httpService, request); updateAmountRequest(); } while (oracle.getResult(response.getResponse()) != BleichenbacherPkcs1Oracle.Result.VALID); loggerInstance.log(getClass(), "Matching response: " + helpers.bytesToString(response.getResponse()), Logger.LogLevel.DEBUG); }
From source file:eu.dety.burp.joseph.attacks.bleichenbacher_pkcs1.BleichenbacherPkcs1DecryptionAttackExecutor.java
private void stepThree(final int i) throws Exception { BigInteger n = this.pubKey.getModulus(); BigInteger r;/*from w w w . jav a 2s . c om*/ BigInteger upperBound; BigInteger lowerBound; BigInteger max; BigInteger min; BigInteger[] tmp; ArrayList<Interval> ms = new ArrayList<>(15); for (Interval interval : this.m) { upperBound = step3ComputeUpperBound(this.si, n, interval.upper); lowerBound = step3ComputeLowerBound(this.si, n, interval.lower); r = lowerBound; // lowerBound <= r <= upperBound while (r.compareTo(upperBound) < 1) { // ceil((2*B+r*n)/si) max = (BigInteger.valueOf(2).multiply(this.bigB)).add(r.multiply(n)); tmp = max.divideAndRemainder(this.si); if (BigInteger.ZERO.compareTo(tmp[1]) != 0) { max = tmp[0].add(BigInteger.ONE); } else { max = tmp[0]; } // floor((3*B-1+r*n)/si min = BigInteger.valueOf(3).multiply(this.bigB); min = min.subtract(BigInteger.ONE); min = min.add(r.multiply(n)); min = min.divide(this.si); // build new interval if (interval.lower.compareTo(max) > 0) { max = interval.lower; } if (interval.upper.compareTo(min) < 0) { min = interval.upper; } if (max.compareTo(min) <= 0) { ms.add(new Interval(max, min)); } // one further.... r = r.add(BigInteger.ONE); } } loggerInstance.log(getClass(), " # of intervals for M" + i + ": " + ms.size(), Logger.LogLevel.INFO); if (ms.size() == 0) { throw new Exception("Zero intervals left, validity oracle seems to be wrong!"); } this.m = ms.toArray(new Interval[ms.size()]); }
From source file:org.hyperledger.common.ByteUtils.java
/** * convert a byte array to a human readable base58 string. Base58 is a Bitcoin specific encoding similar to widely used base64 but avoids using characters * of similar shape, such as 1 and l or O an 0 * * @param b/*ww w . ja v a2 s.c om*/ * @return */ public static String toBase58(byte[] b) { if (b.length == 0) { return ""; } int lz = 0; while (lz < b.length && b[lz] == 0) { ++lz; } StringBuilder s = new StringBuilder(); BigInteger n = new BigInteger(1, b); while (n.compareTo(BigInteger.ZERO) > 0) { BigInteger[] r = n.divideAndRemainder(BigInteger.valueOf(58)); n = r[0]; char digit = b58[r[1].intValue()]; s.append(digit); } while (lz > 0) { --lz; s.append("1"); } return s.reverse().toString(); }