byte[] rndBytes = BigIntegers.asUnsignedByteArray(bi);
/** * Return a positive BigInteger in the range of 0 to 2**bitLength - 1. * * @param bitLength maximum bit length for the generated BigInteger. * @param random a source of randomness. * @return a positive BigInteger */ public static BigInteger createRandomBigInteger(int bitLength, SecureRandom random) { return new BigInteger(1, createRandom(bitLength, random)); }
private BigInteger generateRandomElement(BigInteger p, SecureRandom random) { return BigIntegers.createRandomInRange(ONE, p.subtract(ONE), random); }
/** * Generate and encapsulate a random session key. * * @param out the output buffer for the encapsulated key. * @param outOff the offset for the output buffer. * @param keyLen the length of the random session key. * @return the random session key. */ public CipherParameters encrypt(byte[] out, int outOff, int keyLen) throws IllegalArgumentException { if (key.isPrivate()) { throw new IllegalArgumentException("Public key required for encryption"); } BigInteger n = key.getModulus(); BigInteger e = key.getExponent(); // Generate the ephemeral random and encode it BigInteger r = BigIntegers.createRandomInRange(ZERO, n.subtract(ONE), rnd); // Encrypt the random and encode it BigInteger c = r.modPow(e, n); byte[] C = BigIntegers.asUnsignedByteArray((n.bitLength() + 7) / 8, c); System.arraycopy(C, 0, out, outOff, C.length); return generateKey(n, r, keyLen); }
/** * generate a signature for the loaded message using the key we were * initialised with. */ public byte[] generateSignature() throws CryptoException { createSignatureBlock(trailer); BigInteger t = new BigInteger(1, cipher.processBlock(block, 0, block.length)); clearBlock(block); t = t.min(kParam.getModulus().subtract(t)); int size = BigIntegers.getUnsignedByteLength(kParam.getModulus()); return BigIntegers.asUnsignedByteArray(size, t); }
return createRandomInRange(ZERO, max.subtract(min), random).add(min); BigInteger x = createRandomBigInteger(max.bitLength(), random); if (x.compareTo(min) >= 0 && x.compareTo(max) <= 0) return createRandomBigInteger(max.subtract(min).bitLength() - 1, random).add(min);
@Deprecated public static ECPoint decodeFPPoint(ECCurve curve, byte[] data) { // Patched org.bouncycastle.math.ec.ECCurve#decodePoint code. int expectedLength = (curve.getFieldSize() + 7) / 8; if (expectedLength != data.length) { throw new IllegalArgumentException("incorrect data length for compact encoding"); } BigInteger X = BigIntegers.fromUnsignedByteArray(data, 0, expectedLength); ECPoint p = decompressFPPoint(curve, X); if (!satisfiesCofactor(curve, p)) { throw new IllegalArgumentException("invalid point"); } return p; }
private BigInteger generateRandomElement(BigInteger p, SecureRandom random) { return BigIntegers.createRandomInRange(ONE, p.subtract(ONE), random); }
/** * Generate and encapsulate a random session key. * * @param out the output buffer for the encapsulated key. * @param outOff the offset for the output buffer. * @param keyLen the length of the random session key. * @return the random session key. */ public CipherParameters encrypt(byte[] out, int outOff, int keyLen) throws IllegalArgumentException { if (key.isPrivate()) { throw new IllegalArgumentException("Public key required for encryption"); } BigInteger n = key.getModulus(); BigInteger e = key.getExponent(); // Generate the ephemeral random and encode it BigInteger r = BigIntegers.createRandomInRange(ZERO, n.subtract(ONE), rnd); // Encrypt the random and encode it BigInteger c = r.modPow(e, n); byte[] C = BigIntegers.asUnsignedByteArray((n.bitLength() + 7) / 8, c); System.arraycopy(C, 0, out, outOff, C.length); return generateKey(n, r, keyLen); }
for (;;) BigInteger x = BigIntegers.createRandomBigInteger(limit, random).setBit(limit - 1); if (WNafUtil.getNafWeight(x) >= minWeight) for (;;) BigInteger x = BigIntegers.createRandomInRange(min, max, random); if (WNafUtil.getNafWeight(x) >= minWeight)
@Override public Optional<ECPublicKey> importKey(String curveName, byte[] data) { int fieldLength = ECAssistant.fieldLength(curveName); if (fieldLength(data.length) != fieldLength) { logger.warn("-- importKey() - bad data length: {} curve: {} data:0x{}", data.length, curveName, Hex.toHexString(data)); } if (!checkType(data[0])) { logger.warn("-- importKey() - bad data type: 0x{}", Integer.toHexString(data[0])); } BigInteger x = BigIntegers.fromUnsignedByteArray(data, 1, fieldLength); BigInteger y = BigIntegers.fromUnsignedByteArray(data, 1 + fieldLength, fieldLength); return ECKeyFactories.publicKeyFactory() .createECPublicKey(x, y, curveName); }
public static byte[] serializeECFieldElement(int fieldSize, BigInteger x) throws IOException { return BigIntegers.asUnsignedByteArray((fieldSize + 7) / 8, x); }
/** * Return a value that can be used as x2 or x4 during round 1. * <p> * The returned value is a random value in the range <tt>[1, q-1]</tt>. */ public static BigInteger generateX2( BigInteger q, SecureRandom random) { BigInteger min = ONE; BigInteger max = q.subtract(ONE); return BigIntegers.createRandomInRange(min, max, random); }
/** * Load the private key from a URL-safe base64 encoded string * * @param encodedPrivateKey * @return * @throws NoSuchProviderException * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException */ public static PrivateKey loadPrivateKey(String encodedPrivateKey) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException { byte[] decodedPrivateKey = Base64Encoder.decode(encodedPrivateKey); BigInteger s = BigIntegers.fromUnsignedByteArray(decodedPrivateKey); ECParameterSpec parameterSpec = ECNamedCurveTable.getParameterSpec(CURVE); ECPrivateKeySpec privateKeySpec = new ECPrivateKeySpec(s, parameterSpec); KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM, PROVIDER_NAME); return keyFactory.generatePrivate(privateKeySpec); }
byte[] base = createRandom(bitLength, random);
public static byte[] serializeECFieldElement(int fieldSize, BigInteger x) throws IOException { return BigIntegers.asUnsignedByteArray((fieldSize + 7) / 8, x); }
/** * Return a value that can be used as x1 or x3 during round 1. * <p> * The returned value is a random value in the range <tt>[0, q-1]</tt>. */ public static BigInteger generateX1( BigInteger q, SecureRandom random) { BigInteger min = ZERO; BigInteger max = q.subtract(ONE); return BigIntegers.createRandomInRange(min, max, random); }
BigInteger X = BigIntegers.fromUnsignedByteArray(encoded, 1, expectedLength); BigInteger X = BigIntegers.fromUnsignedByteArray(encoded, 1, expectedLength); BigInteger Y = BigIntegers.fromUnsignedByteArray(encoded, 1 + expectedLength, expectedLength); BigInteger X = BigIntegers.fromUnsignedByteArray(encoded, 1, expectedLength); BigInteger Y = BigIntegers.fromUnsignedByteArray(encoded, 1 + expectedLength, expectedLength);
public static byte[] encodedField(int length, BigInteger i) { return BigIntegers.asUnsignedByteArray(length, i); }
/** * Return a value that can be used as x2 or x4 during round 1. * <p> * The returned value is a random value in the range <tt>[1, q-1]</tt>. */ public static BigInteger generateX2( BigInteger q, SecureRandom random) { BigInteger min = ONE; BigInteger max = q.subtract(ONE); return BigIntegers.createRandomInRange(min, max, random); }