Example usage for org.bouncycastle.jcajce.provider.asymmetric.ec BCECPublicKey BCECPublicKey

List of usage examples for org.bouncycastle.jcajce.provider.asymmetric.ec BCECPublicKey BCECPublicKey

Introduction

In this page you can find the example usage for org.bouncycastle.jcajce.provider.asymmetric.ec BCECPublicKey BCECPublicKey.

Prototype

BCECPublicKey(String algorithm, ECPoint q, ECParameterSpec ecSpec, ProviderConfiguration configuration) 

Source Link

Usage

From source file:com.amazonaws.encryptionsdk.internal.DecryptionHandler.java

License:Open Source License

private PublicKey deserializeTrailingKeyFromEc(final String pubKey) throws GeneralSecurityException {
    final ECNamedCurveParameterSpec ecSpec;

    switch (cryptoAlgo_) {
    case ALG_AES_128_GCM_IV12_TAG16_HKDF_SHA256_ECDSA_P256:
        ecSpec = ECNamedCurveTable.getParameterSpec("secp256r1");
        break;//from  ww  w.ja va2  s.  com
    case ALG_AES_192_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384:
    case ALG_AES_256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384:
        ecSpec = ECNamedCurveTable.getParameterSpec("secp384r1");
        break;
    default:
        throw new IllegalStateException("Algorithm does not support trailing signature");
    }
    final ECPoint q = ecSpec.getCurve().decodePoint(Base64.decode(pubKey));
    ECPublicKeyParameters keyParams = new ECPublicKeyParameters(q,
            new ECDomainParameters(ecSpec.getCurve(), ecSpec.getG(), ecSpec.getN(), ecSpec.getH()));
    return new BCECPublicKey("ECDSA", keyParams, ecSpec, BouncyCastleProvider.CONFIGURATION);
}

From source file:dorkbox.util.crypto.EccTest.java

License:Apache License

@Test
public void EccJceSerialization() throws IOException {
    AsymmetricCipherKeyPair generateKeyPair = CryptoECC.generateKeyPair(CryptoECC.default_curve,
            new SecureRandom());
    ECPrivateKeyParameters privateKey = (ECPrivateKeyParameters) generateKeyPair.getPrivate();
    ECPublicKeyParameters publicKey = (ECPublicKeyParameters) generateKeyPair.getPublic();

    BCECPublicKey bcecPublicKey = new BCECPublicKey("EC", publicKey, (ECParameterSpec) null,
            BouncyCastleProvider.CONFIGURATION);
    byte[] publicBytes = bcecPublicKey.getEncoded();

    // relies on the BC public key.
    BCECPrivateKey bcecPrivateKey = new BCECPrivateKey("EC", privateKey, bcecPublicKey, (ECParameterSpec) null,
            BouncyCastleProvider.CONFIGURATION);
    byte[] privateBytes = bcecPrivateKey.getEncoded();

    ECPublicKeyParameters publicKey2 = (ECPublicKeyParameters) PublicKeyFactory.createKey(publicBytes);
    ECPrivateKeyParameters privateKey2 = (ECPrivateKeyParameters) PrivateKeyFactory.createKey(privateBytes);

    // test via signing
    byte[] bytes = "hello, my name is inigo montoya".getBytes();

    BigInteger[] signature = CryptoECC.generateSignature("SHA384", privateKey,
            new SecureRandom(entropySeed.getBytes()), bytes);

    boolean verify1 = CryptoECC.verifySignature("SHA384", publicKey, bytes, signature);

    if (!verify1) {
        fail("failed signature verification");
    }//w ww.j  ava 2 s.c  om

    boolean verify2 = CryptoECC.verifySignature("SHA384", publicKey2, bytes, signature);

    if (!verify2) {
        fail("failed signature verification");
    }

    // now reverse who signs what.
    BigInteger[] signatureB = CryptoECC.generateSignature("SHA384", privateKey2,
            new SecureRandom(entropySeed.getBytes()), bytes);

    boolean verifyB1 = CryptoECC.verifySignature("SHA384", publicKey, bytes, signatureB);

    if (!verifyB1) {
        fail("failed signature verification");
    }

    boolean verifyB2 = CryptoECC.verifySignature("SHA384", publicKey2, bytes, signatureB);

    if (!verifyB2) {
        fail("failed signature verification");
    }
}