List of usage examples for java.math BigInteger gcd
public BigInteger gcd(BigInteger val)
From source file:Main.java
public static void main(String[] args) { // assign values to bi1, bi2 BigInteger bi1 = new BigInteger("18"); BigInteger bi2 = new BigInteger("24"); // assign gcd of bi1, bi2 to bi3 BigInteger bi3 = bi1.gcd(bi2); System.out.println(bi3);/*from w w w . j a v a2 s . c om*/ }
From source file:cc.redberry.core.number.Exponentiation.java
public static Complex findIntegerRoot(Complex base, BigInteger power) { BigInteger rDenominator = ((Rational) base.getReal()).getDenominator(); BigInteger iDenominator = ((Rational) base.getImaginary()).getDenominator(); BigInteger lcm = rDenominator.gcd(iDenominator); lcm = rDenominator.divide(lcm);/*from w w w . j a va2 s .c o m*/ lcm = lcm.multiply(iDenominator); BigInteger lcmRoot = findIntegerRoot(lcm, power); if (lcm == null) return null; base = base.multiply(lcm); Complex numericValue = base.pow(1.0 / power.doubleValue()); double real = numericValue.getReal().doubleValue(); double imaginary = numericValue.getImaginary().doubleValue(); int ceilReal = (int) Math.ceil(real), floorReal = (int) Math.floor(real), ceilImaginary = (int) Math.ceil(imaginary), floorImaginary = (int) Math.floor(imaginary); Complex candidate; if ((candidate = new Complex(ceilReal, ceilImaginary)).pow(power).equals(base)) return candidate.divide(lcmRoot); if ((candidate = new Complex(floorReal, ceilImaginary)).pow(power).equals(base)) return candidate.divide(lcmRoot); if ((candidate = new Complex(ceilReal, floorImaginary)).pow(power).equals(base)) return candidate.divide(lcmRoot); if ((candidate = new Complex(floorReal, floorImaginary)).pow(power).equals(base)) return candidate.divide(lcmRoot); return null; }
From source file:RSA.java
/** Generate a new public and private key set. */ public synchronized void generateKeys() { SecureRandom r = new SecureRandom(); BigInteger p = new BigInteger(bitlen / 2, 100, r); BigInteger q = new BigInteger(bitlen / 2, 100, r); n = p.multiply(q);/*from ww w . j a v a 2s .c o m*/ BigInteger m = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE)); e = new BigInteger("3"); while (m.gcd(e).intValue() > 1) { e = e.add(new BigInteger("2")); } d = e.modInverse(m); }
From source file:RSA.java
/** Create an instance that can both encrypt and decrypt. */ public RSA(int bits) { bitlen = bits;// www .j av a 2 s.c o m SecureRandom r = new SecureRandom(); BigInteger p = new BigInteger(bitlen / 2, 100, r); BigInteger q = new BigInteger(bitlen / 2, 100, r); n = p.multiply(q); BigInteger m = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE)); e = new BigInteger("3"); while (m.gcd(e).intValue() > 1) { e = e.add(new BigInteger("2")); } d = e.modInverse(m); }
From source file:net.pms.util.Rational.java
/** * Returns an instance that represents the value of {@code value}. * * @param value the value./* w ww. ja v a2 s. co m*/ * @return An instance that represents the value of {@code value}. */ @Nullable public static Rational valueOf(@Nullable BigDecimal value) { if (value == null) { return null; } BigInteger numerator; BigInteger denominator; if (value.signum() == 0) { return ZERO; } if (BigDecimal.ONE.equals(value)) { return ONE; } if (value.scale() > 0) { BigInteger unscaled = value.unscaledValue(); BigInteger tmpDenominator = BigInteger.TEN.pow(value.scale()); BigInteger tmpGreatestCommonDivisor = unscaled.gcd(tmpDenominator); numerator = unscaled.divide(tmpGreatestCommonDivisor); denominator = tmpDenominator.divide(tmpGreatestCommonDivisor); } else { numerator = value.toBigIntegerExact(); denominator = BigInteger.ONE; } return new Rational(numerator, denominator, BigInteger.ONE, numerator, denominator); }
From source file:net.pms.util.Rational.java
/** * Calculates the greatest common divisor for two {@link BigInteger}s using * {@link BigInteger#gcd}.//from w w w. j a va 2 s. c o m * * @param u the first number. * @param v the second number. * @return The GDC, always 1 or greater. */ @Nullable public static BigInteger calculateGreatestCommonDivisor(@Nullable BigInteger u, @Nullable BigInteger v) { if (u == null || v == null) { return null; } if (u.abs().compareTo(BigInteger.ONE) <= 0 || v.abs().compareTo(BigInteger.ONE) <= 0) { return BigInteger.ONE; } return u.gcd(v); }
From source file:net.pms.util.Rational.java
/** * Returns a {@link Rational} whose value is {@code (this * value)}. * * @param value the value to be multiplied by this {@link Rational}. * @return The multiplication result.//from w w w. j a v a2s . com */ @Nullable public Rational multiply(@Nullable Rational value) { if (value == null) { return null; } if (isNaN() || value.isNaN()) { return NaN; } if (isInfinite() || value.isInfinite()) { if (signum() == 0 || value.signum() == 0) { return NaN; // Infinity by zero } return numerator.signum() == value.signum() ? POSITIVE_INFINITY : NEGATIVE_INFINITY; } if (value.signum() == 0) { return ZERO; } if (value.numerator.abs().equals(value.denominator)) { return value.signum() < 0 ? this.negate() : this; } BigInteger newNumerator = reducedNumerator.multiply(value.reducedNumerator); BigInteger newDenominator = reducedDenominator.multiply(value.reducedDenominator); BigInteger gcd = newNumerator.gcd(newDenominator); return valueOf(newNumerator.divide(gcd), newDenominator.divide(gcd)); }
From source file:org.jbpm.formModeler.core.processing.formRendering.FormRenderingFormatter.java
protected BigInteger calculateMCM(List colspans) { if (colspans == null || colspans.isEmpty()) { return new BigInteger("1"); } else if (colspans.size() == 1) { return (BigInteger) colspans.get(0); } else if (colspans.size() == 2) { BigInteger b1 = (BigInteger) colspans.get(0); BigInteger b2 = (BigInteger) colspans.get(1); return b1.multiply(b2).divide(b1.gcd(b2)); } else { //Size > 2 int halfLength = colspans.size() / 2; List firstHalf = colspans.subList(0, halfLength); List secondHalf = colspans.subList(halfLength, colspans.size()); BigInteger b1 = calculateMCM(firstHalf); BigInteger b2 = calculateMCM(secondHalf); return b1.multiply(b2).divide(b1.gcd(b2)); }// w w w . ja va 2s. c o m }
From source file:test.be.fedict.eid.applet.RSATest.java
@Test public void testManualEncryption() throws Exception { while (true) { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", BouncyCastleProvider.PROVIDER_NAME); SecureRandom random = new SecureRandom(); int keySize = 128; keyPairGenerator.initialize(new RSAKeyGenParameterSpec(keySize, RSAKeyGenParameterSpec.F0), random); KeyPair keyPair = keyPairGenerator.generateKeyPair(); PrivateKey privateKey = keyPair.getPrivate(); PublicKey publicKey = keyPair.getPublic(); RSAPrivateCrtKey rsaPrivateKey = (RSAPrivateCrtKey) privateKey; LOG.debug("private key modulus: " + rsaPrivateKey.getModulus()); RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey; LOG.debug("public key modulus: " + rsaPublicKey.getModulus()); LOG.debug("public key exponent: " + rsaPublicKey.getPublicExponent()); LOG.debug("modulus size: " + rsaPublicKey.getModulus().toByteArray().length); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.ENCRYPT_MODE, privateKey); int dataSize = keySize / 8 - 11; byte[] data1 = new byte[dataSize]; for (int i = 0; i < data1.length; i++) { data1[i] = 0x00;/* w ww .j a v a 2 s. co m*/ } byte[] data2 = new byte[dataSize]; for (int i = 0; i < data2.length; i++) { data2[i] = 0x00; } data2[data2.length - 1] = 0x07; byte[] signatureValue1 = cipher.doFinal(data1); LOG.debug("signature size: " + signatureValue1.length); cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.ENCRYPT_MODE, privateKey); byte[] signatureValue2 = cipher.doFinal(data2); BigInteger sigBigInt1 = new BigInteger(signatureValue1); BigInteger sigBigInt2 = new BigInteger(signatureValue2); BigInteger msgBigInt1 = sigBigInt1.modPow(rsaPublicKey.getPublicExponent(), rsaPublicKey.getModulus()); BigInteger msgBigInt2 = sigBigInt2.modPow(rsaPublicKey.getPublicExponent(), rsaPublicKey.getModulus()); LOG.debug("msg big int: " + msgBigInt1); byte[] msgBytes1 = msgBigInt1.toByteArray(); LOG.debug("original message size: " + msgBytes1.length); LOG.debug("original message1: " + new String(Hex.encodeHex(msgBytes1))); LOG.debug("original message2: " + new String(Hex.encodeHex(msgBigInt2.toByteArray()))); LOG.debug("msg1 prime: " + msgBigInt1.isProbablePrime(100)); LOG.debug("msg2 prime: " + msgBigInt2.isProbablePrime(100)); // BigInteger.pow offers a very naive implementation LOG.debug("calculating s1^e..."); BigInteger s1_e = sigBigInt1.pow(rsaPublicKey.getPublicExponent().intValue()); LOG.debug("s1^e: " + s1_e); LOG.debug("calculating s2^e..."); BigInteger s2_e = sigBigInt2.pow(rsaPublicKey.getPublicExponent().intValue()); LOG.debug("s2^e: " + s2_e); LOG.debug("calculating GCD..."); LOG.debug("msg1: " + msgBigInt1); LOG.debug("msg2: " + msgBigInt2); BigInteger a = s1_e.subtract(msgBigInt1); BigInteger b = s2_e.subtract(msgBigInt2); LOG.debug("a: " + a); LOG.debug("b: " + b); BigInteger candidateModulus = a.gcd(b); LOG.debug("candidate modulus: " + candidateModulus); LOG.debug("candidate modulus size: " + candidateModulus.toByteArray().length); BigInteger s_e = s1_e.multiply(s2_e); BigInteger m = msgBigInt1.multiply(msgBigInt2); while (false == rsaPublicKey.getModulus().equals(candidateModulus)) { LOG.error("incorrect candidate modulus"); LOG.debug("modulus | candidate modulus: " + candidateModulus.remainder(rsaPublicKey.getModulus()).equals(BigInteger.ZERO)); s_e = s_e.multiply(s1_e); m = m.multiply(msgBigInt1); BigInteger n1 = s_e.subtract(m).gcd(a); BigInteger n2 = s_e.subtract(m).gcd(b); candidateModulus = n1.gcd(n2); // try / 2 LOG.debug("new modulus: " + n1); LOG.debug("new modulus: " + n2); LOG.debug("candidate modulus: " + candidateModulus); LOG.debug("actual mod: " + rsaPublicKey.getModulus()); } } }