@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; }
@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); }
/** * 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); }
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);
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);
@Override public Optional<ECPrivateKey> importKey(String curveName, byte[] data) { X9ECParameters x9ECParameters = ECAssistant.x9ECParameters(curveName); int fieldLength = ECAssistant.fieldLength(x9ECParameters); if (fieldLength(data.length) != fieldLength) { logger.warn("-- importKey() - bad data length: {} curve: {} data:0x{}", data.length, curveName, Hex.toHexString(data)); } BigInteger x = BigIntegers.fromUnsignedByteArray(data, 0, fieldLength); BigInteger y = ECPointsCompact.y(x9ECParameters.getCurve(), x); BigInteger d = BigIntegers.fromUnsignedByteArray(data, fieldLength, fieldLength); return ECKeyFactories.privateKeyFactory() .createECPrivateKey(x, y, d, curveName); }
@Override public Optional<ECPrivateKey> 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)); } BigInteger d = BigIntegers.fromUnsignedByteArray(data, 0, fieldLength); return ECKeyFactories.privateKeyFactory() .createECPrivateKey(d, curveName); }
@Override public Optional<ECPublicKey> importKey(String curveName, byte[] data) { X9ECParameters x9ECParameters = ECAssistant.x9ECParameters(curveName); int fieldLength = ECAssistant.fieldLength(x9ECParameters); if (fieldLength(data.length) != fieldLength) { logger.warn("-- importKey() - bad data length: {} curve: {} data:0x{}", data.length, curveName, Hex.toHexString(data)); } BigInteger x = BigIntegers.fromUnsignedByteArray(data); BigInteger y = ECPointsCompact.y(x9ECParameters.getCurve(), x); return ECKeyFactories.publicKeyFactory() .createECPublicKey(x, y, curveName); }