List of usage examples for java.math BigInteger pow
public BigInteger pow(int exponent)
(thisexponent)
. From source file:org.limewire.mojito.util.DHTSizeEstimator.java
/** * Computes and returns the approximate DHT size based * on the given List of Contacts./*from w w w .ja va 2s. com*/ */ public synchronized BigInteger computeSize(Collection<? extends Contact> nodes) { // Works only with more than two Nodes if (nodes.size() < MIN_NODE_COUNT) { // There's always us! return BigInteger.ONE.max(BigInteger.valueOf(nodes.size())); } // Get the Iterator. We assume the Contacts are sorted by // their xor distance! Iterator<? extends Contact> contacts = nodes.iterator(); // See Azureus DHTControlImpl.estimateDHTSize() // Di = nearestId xor NodeIDi // Dc = sum(i * Di) / sum(i * i) // Size = 2**160 / Dc BigInteger sum1 = BigInteger.ZERO; BigInteger sum2 = BigInteger.ZERO; // The algorithm works relative to the ID space. KUID nearestId = contacts.next().getNodeID(); // We start 1 because the nearest Node is the 0th item! for (int i = 1; contacts.hasNext(); i++) { Contact node = contacts.next(); BigInteger distance = nearestId.xor(node.getNodeID()).toBigInteger(); BigInteger j = BigInteger.valueOf(i); sum1 = sum1.add(j.multiply(distance)); sum2 = sum2.add(j.pow(2)); } BigInteger estimatedSize = BigInteger.ZERO; if (!sum1.equals(BigInteger.ZERO)) { estimatedSize = KUID.MAXIMUM.toBigInteger().multiply(sum2).divide(sum1); } // And there is always us! estimatedSize = BigInteger.ONE.max(estimatedSize); // Get the average of the local estimations BigInteger localSize = BigInteger.ZERO; localSizeHistory.add(estimatedSize); // Adjust the size of the List. The Setting is SIMPP-able // and may change! int maxLocalHistorySize = ContextSettings.MAX_LOCAL_HISTORY_SIZE.getValue(); while (localSizeHistory.size() > maxLocalHistorySize && !localSizeHistory.isEmpty()) { localSizeHistory.remove(0); } if (!localSizeHistory.isEmpty()) { BigInteger localSizeSum = BigInteger.ZERO; for (BigInteger size : localSizeHistory) { localSizeSum = localSizeSum.add(size); } localSize = localSizeSum.divide(BigInteger.valueOf(localSizeHistory.size())); } // Get the combined average // S = (localEstimation + sum(remoteEstimation[i]))/count BigInteger combinedSize = localSize; if (ContextSettings.COUNT_REMOTE_SIZE.getValue()) { // Prune all duplicates and sort the values Set<BigInteger> remoteSizeSet = new TreeSet<BigInteger>(remoteSizeHistory); if (remoteSizeSet.size() >= 3) { BigInteger[] remote = remoteSizeSet.toArray(new BigInteger[0]); // Skip the smallest and largest values int count = 1; int skip = ContextSettings.SKIP_REMOTE_ESTIMATES.getValue(); for (int i = skip; (skip >= 0) && (i < (remote.length - skip)); i++) { combinedSize = combinedSize.add(remote[i]); count++; } combinedSize = combinedSize.divide(BigInteger.valueOf(count)); // Make sure we didn't exceed the MAXIMUM number as // we made an addition with the local estimation which // might be already 2**160 bit! combinedSize = combinedSize.min(MAXIMUM); } } // There is always us! return BigInteger.ONE.max(combinedSize); }
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;//from ww w .j a v a2s .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()); } } }