Java Code Examples for org.spongycastle.jce.spec.ECNamedCurveParameterSpec#getN()

The following examples show how to use org.spongycastle.jce.spec.ECNamedCurveParameterSpec#getN() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpenIdSigningKeyResolver.java    From line-sdk-android with Apache License 2.0 6 votes vote down vote up
private static ECPublicKey generateECPublicKey(final JWK jwk) {
    final BigInteger x = decodeBase64(jwk.getX());
    final BigInteger y = decodeBase64(jwk.getY());

    try {
        final KeyFactory factory = KeyFactory.getInstance("EC");

        final ECPoint point = new ECPoint(x, y);
        final ECNamedCurveParameterSpec paramSpec = ECNamedCurveTable.getParameterSpec(jwk.getCurve());
        final ECNamedCurveSpec params = new ECNamedCurveSpec(jwk.getCurve(),
                                                             paramSpec.getCurve(),
                                                             paramSpec.getG(),
                                                             paramSpec.getN());

        final ECPublicKeySpec spec = new ECPublicKeySpec(point, params);

        return (ECPublicKey) factory.generatePublic(spec);
    } catch (final NoSuchAlgorithmException | InvalidKeySpecException e) {
        Log.e(TAG, "failed to generate EC Public Key from JWK: " + jwk, e);

        return null;
    }
}
 
Example 2
Source File: KeyCodec.java    From UAF with Apache License 2.0 5 votes vote down vote up
/**
 * Decode based on X, Y 32 byte integers
 *
 * @param pubKey
 * @param curveName - Example secp256r1
 * @return
 * @throws InvalidKeySpecException
 * @throws NoSuchAlgorithmException
 * @throws NoSuchProviderException
 */
public static PublicKey getPubKeyFromCurve(byte[] pubKey, String curveName) throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException {

    ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec(curveName);
    KeyFactory kf = KeyFactory.getInstance("ECDSA", new BouncyCastleProvider());
    ECNamedCurveSpec params = new ECNamedCurveSpec(curveName, spec.getCurve(), spec.getG(), spec.getN());
    ECPoint point = ECPointUtil.decodePoint(params.getCurve(), pubKey);
    ECPublicKeySpec pubKeySpec = new ECPublicKeySpec(point, params);
    ECPublicKey pk = (ECPublicKey) kf.generatePublic(pubKeySpec);
    return pk;
}
 
Example 3
Source File: KeyCodec.java    From UAF with Apache License 2.0 5 votes vote down vote up
/**
 * Decode based on d - 32 byte integer
 *
 * @param privKey
 * @param curveName - Example secp256r1
 * @return
 * @throws InvalidKeySpecException
 * @throws NoSuchAlgorithmException
 * @throws NoSuchProviderException
 */
public static PrivateKey getPrivKeyFromCurve(byte[] privKey, String curveName) throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException {

    ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec(curveName);
    KeyFactory kf = KeyFactory.getInstance("ECDSA", new BouncyCastleProvider());
    ECNamedCurveSpec params = new ECNamedCurveSpec(curveName, spec.getCurve(), spec.getG(), spec.getN());
    ECPrivateKeySpec priKey = new ECPrivateKeySpec(
            new BigInteger(privKey), // d
            params);
    return kf.generatePrivate(priKey);
}