Example usage for org.bouncycastle.jce.interfaces ECPrivateKey getD

List of usage examples for org.bouncycastle.jce.interfaces ECPrivateKey getD

Introduction

In this page you can find the example usage for org.bouncycastle.jce.interfaces ECPrivateKey getD.

Prototype

public BigInteger getD();

Source Link

Document

return the private value D.

Usage

From source file:org.gluu.com.ox_push2.u2f.v2.cert.KeyPairGeneratorImpl.java

License:MIT License

public String keyPairToJson(KeyPair keyPair) throws U2FException {
    ECPrivateKey privateKey = (ECPrivateKey) keyPair.getPrivate();
    ECPublicKey publicKey = (ECPublicKey) keyPair.getPublic();

    BigInteger x = publicKey.getQ().getAffineXCoord().toBigInteger();
    BigInteger y = publicKey.getQ().getAffineYCoord().toBigInteger();
    BigInteger d = privateKey.getD();

    try {/*from  w  w  w . j a  v a2  s  .  co m*/
        JSONObject jsonPrivateKey = new JSONObject();
        jsonPrivateKey.put("d", Utils.encodeHexString(d.toByteArray()));

        JSONObject jsonPublicKey = new JSONObject();
        jsonPublicKey.put("x", Utils.encodeHexString(x.toByteArray()));
        jsonPublicKey.put("y", Utils.encodeHexString(y.toByteArray()));

        JSONObject jsonKeyPair = new JSONObject();
        jsonKeyPair.put("privateKey", jsonPrivateKey);
        jsonKeyPair.put("publicKey", jsonPublicKey);

        String keyPairJson = jsonKeyPair.toString();

        if (BuildConfig.DEBUG)
            Log.d(TAG, "JSON key pair: " + keyPairJson);

        return keyPairJson;
    } catch (JSONException ex) {
        throw new U2FException("Failed to serialize key pair to JSON", ex);
    }
}