Example usage for org.bouncycastle.math.ec ECCurve decodePoint

List of usage examples for org.bouncycastle.math.ec ECCurve decodePoint

Introduction

In this page you can find the example usage for org.bouncycastle.math.ec ECCurve decodePoint.

Prototype

public ECPoint decodePoint(byte[] encoded) 

Source Link

Document

Decode a point on this curve from its ASN.1 encoding.

Usage

From source file:com.distrimind.util.crypto.ASymmetricEncryptionType.java

License:Open Source License

static org.bouncycastle.jce.spec.ECPublicKeySpec deserializePublicKey(byte[] publicKey, boolean lazy) {

    if (publicKey.length <= 32) {
        if (lazy && (publicKey.length == 32)) {
            return null;
        }/*from   w ww  . j  a va 2s. c o m*/
        byte[] key = new byte[33];
        int offset = 33 - publicKey.length;
        for (int i = publicKey.length - 1; i >= 0; i--) {
            key[offset++] = publicKey[i];
        }
        key[0] = 3;
        ECCurve curve = getCurve25519().getCurve();
        org.bouncycastle.math.ec.ECPoint q = curve.decodePoint(key);
        return new org.bouncycastle.jce.spec.ECPublicKeySpec(q, getCurve25519());
    } else if (publicKey.length == 33) { // TODO make 32 byte representation normal form
        if (lazy) {
            return null;
        }
        ECCurve curve = getCurve25519().getCurve();
        org.bouncycastle.math.ec.ECPoint q = curve.decodePoint(publicKey);
        return new org.bouncycastle.jce.spec.ECPublicKeySpec(q, getCurve25519());
    } else {
        throw new IllegalArgumentException();
    }
}

From source file:com.github.horrorho.inflatabledonkey.crypto.rfc6637.RFC6637.java

License:Open Source License

ECPoint decodePoint(byte[] data) {
    ECCurve curve = ECNamedCurveTable.getByName(curveName).getCurve();
    int compactExportSize = (curve.getFieldSize() + 7) / 8;

    return data.length == compactExportSize ? ECPointsCompact.decodeFPPoint(curve, data) // Compact keys support, non RFC6636 compliant.
            : curve.decodePoint(data);
}

From source file:com.licel.jcardsim.crypto.ECKeyImpl.java

License:Apache License

/**
 * Get/*  w  w  w.  j a va 2  s . c  o m*/
 * <code>ECDomainParameters</code>
 *
 * @return parameters for use with BouncyCastle API
 * @see ECDomainParameters
 */
public ECDomainParameters getDomainParameters() {
    if (!isDomainParametersInitialized()) {
        CryptoException.throwIt(CryptoException.UNINITIALIZED_KEY);
    }
    ECCurve curve = null;
    if (fp.isInitialized()) {
        curve = new ECCurve.Fp(fp.getBigInteger(), a.getBigInteger(), b.getBigInteger());
    } else {
        curve = new ECCurve.F2m(size, e1, e2, e3, a.getBigInteger(), b.getBigInteger(), r.getBigInteger(),
                BigInteger.valueOf(k));
    }
    return new ECDomainParameters(curve, curve.decodePoint(g.getBytes(JCSystem.CLEAR_ON_RESET)),
            r.getBigInteger(), BigInteger.valueOf(k));
}

From source file:com.netflix.msl.keyx.AsymmetricWrappedExchangeSuite.java

License:Open Source License

@BeforeClass
public static synchronized void setup() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException,
        MslEncodingException, MslCryptoException {
    if (ctx == null) {
        Security.addProvider(new BouncyCastleProvider());

        final ECCurve curve = new ECCurve.Fp(EC_Q, EC_A, EC_B);
        final AlgorithmParameterSpec paramSpec = new ECParameterSpec(curve,
                curve.decodePoint(EC_G.toByteArray()), EC_N);
        final KeyPairGenerator eccGenerator = KeyPairGenerator.getInstance("ECIES");
        eccGenerator.initialize(paramSpec);
        final KeyPair eccKeyPair = eccGenerator.generateKeyPair();
        ECC_PUBLIC_KEY = eccKeyPair.getPublic();
        ECC_PRIVATE_KEY = eccKeyPair.getPrivate();

        final KeyPairGenerator rsaGenerator = KeyPairGenerator.getInstance("RSA");
        rsaGenerator.initialize(2048);/* w  ww .j av  a2 s. c o  m*/
        final KeyPair rsaKeyPair = rsaGenerator.generateKeyPair();
        RSA_PUBLIC_KEY = rsaKeyPair.getPublic();
        RSA_PRIVATE_KEY = rsaKeyPair.getPrivate();

        ctx = new MockMslContext(EntityAuthenticationScheme.PSK, false);
        MASTER_TOKEN = MslTestUtils.getMasterToken(ctx, 1, 1);
        ENCRYPTION_KEY = MASTER_TOKEN.getEncryptionKey().getEncoded();
        HMAC_KEY = MASTER_TOKEN.getHmacKey().getEncoded();
    }
}

From source file:de.fraunhofer.fokus.openeid.cryptography.ECKeyPair.java

License:Open Source License

/**
 * @proposed by mkh: Adapter constructor
 *///from  w  ww . ja va2s . c  om
public ECKeyPair(KeyPair keyPair, ECCurve curve) throws InvalidKeySpecException, NoSuchAlgorithmException {
    privateKey = ((ECPrivateKey) keyPair.getPrivate()).getS();
    byte[] encoded = keyPair.getPublic().getEncoded();
    publicKey = curve.decodePoint(encoded);
}

From source file:dorkbox.util.serialization.EccPrivateKeySerializer.java

License:Apache License

public static ECPrivateKeyParameters read(Input input) throws KryoException {
    byte[] bytes;
    int length;//ww  w.  java 2 s  .c  o m

    ECCurve curve = EccPrivateKeySerializer.deserializeCurve(input);

    // N
    /////////////
    length = input.readInt(true);
    bytes = new byte[length];
    input.readBytes(bytes, 0, length);
    BigInteger n = new BigInteger(bytes);

    // G
    /////////////
    length = input.readInt(true);
    bytes = new byte[length];
    input.readBytes(bytes, 0, length);
    ECPoint g = curve.decodePoint(bytes);

    // D
    /////////////
    length = input.readInt(true);
    bytes = new byte[length];
    input.readBytes(bytes, 0, length);
    BigInteger D = new BigInteger(bytes);

    ECDomainParameters ecDomainParameters = new ECDomainParameters(curve, g, n);

    return new ECPrivateKeyParameters(D, ecDomainParameters);
}

From source file:dorkbox.util.serialization.EccPublicKeySerializer.java

License:Apache License

public static ECPublicKeyParameters read(Input input) throws KryoException {
    byte[] bytes;
    int length;// w  w  w. j a va  2  s . c o m

    ECCurve curve = EccPrivateKeySerializer.deserializeCurve(input);

    // N
    /////////////
    length = input.readInt(true);
    bytes = new byte[length];
    input.readBytes(bytes, 0, length);
    BigInteger n = new BigInteger(bytes);

    // G
    /////////////
    length = input.readInt(true);
    bytes = new byte[length];
    input.readBytes(bytes, 0, length);
    ECPoint g = curve.decodePoint(bytes);

    ECDomainParameters ecDomainParameters = new ECDomainParameters(curve, g, n);

    // Q
    /////////////
    length = input.readInt(true);
    bytes = new byte[length];
    input.readBytes(bytes, 0, length);
    ECPoint Q = curve.decodePoint(bytes);

    return new ECPublicKeyParameters(Q, ecDomainParameters);
}

From source file:eu.betaas.taas.securitymanager.common.ec.ECKeyPairGen.java

License:Apache License

/**
 * Generate a random EC (Elliptic Curve) random 192-bit key pair (equivalent 
 * to 1536-bit RSA) based on NIST and SECG, using Bc (Bouncy Castle) classes
 * @return a pair of EC keys (AsymmetricCipherKeyPair type)
 *///ww  w  . j av a  2s  . c  o m
public static AsymmetricCipherKeyPair generateECKeyPair192() {
    AsymmetricCipherKeyPairGenerator kpGen = new ECKeyPairGenerator();

    // First, define an EC curve
    // ECCurve.Fp(p, a, b); p = prime; a,b = constants defined in equation E: y^2=x^3+ax+b (mod p)
    ECCurve curve = new ECCurve.Fp(new BigInteger(ECParams.P_192_R1, 16), // p 
            new BigInteger(ECParams.A_192_R1, 16), // a
            new BigInteger(ECParams.B_192_R1, 16)); // b

    byte[] seed = Hex.decode(ECParams.SEED_192_R1);

    // finally use the seed in the ECKeyGenerationParameters along with the others
    // ECKeyGenerationParameters(ECDomainParameters(ECCurve, G, n, h),random)
    kpGen.init(new ECKeyGenerationParameters(
            new ECDomainParameters(curve, curve.decodePoint(Hex.decode(ECParams.G_192_R1_NCOMP)), // G       
                    new BigInteger(ECParams.N_192_R1, 16), // n
                    new BigInteger(ECParams.H_192_R1, 16), // h 
                    seed), // seed
            new SecureRandom()));

    return kpGen.generateKeyPair();
}

From source file:eu.betaas.taas.securitymanager.common.ec.ECKeyPairGen.java

License:Apache License

/**
 * Generate a random EC (Elliptic Curve) random 224-bit key pair (equivalent 
 * to 2048-bit RSA) based on NIST and SECG, using Bc (Bouncy Castle) classes
 * @return a pair of EC keys (AsymmetricCipherKeyPair type)
 *///from w  w w . jav  a 2  s. co  m
public static AsymmetricCipherKeyPair generateECKeyPair224() {
    AsymmetricCipherKeyPairGenerator kpGen = new ECKeyPairGenerator();

    // ECCurve.Fp(p, a, b); p = prime; a,b = constants defined in equation E: y^2=x^3+ax+b (mod p)
    ECCurve curve = new ECCurve.Fp(new BigInteger(ECParams.P_224_R1, 16), new BigInteger(ECParams.A_224_R1, 16),
            new BigInteger(ECParams.B_224_R1, 16));

    byte[] seed = Hex.decode(ECParams.SEED_224_R1);

    // finally use the seed in the ECKeyGenerationParameters along with the others
    // ECKeyGenerationParameters(ECDomainParameters(ECCurve, G, n, h),random)
    kpGen.init(new ECKeyGenerationParameters(
            new ECDomainParameters(curve, curve.decodePoint(Hex.decode(ECParams.G_224_R1_NCOMP)),
                    new BigInteger(ECParams.N_224_R1, 16), new BigInteger(ECParams.H_224_R1, 16), seed),
            new SecureRandom()));

    return kpGen.generateKeyPair();
}

From source file:eu.betaas.taas.securitymanager.common.ec.ECKeyPairGen.java

License:Apache License

/**
 * Generate random 192-bit EC Public Key given the Q/W parameters of EC Public
 * Key, i.e. the X and Y coordinate  /*from  www  . j  a  va2 s. c  om*/
 * @param Wx: X coordinate of Q or W point representing the EC public key
 * @param Wy: Y coordinate of Q or W point representing the EC public key
 * @return
 * @throws Exception
 */
public static ECPublicKeyParameters generateECPublicKey192(BigInteger Wx, BigInteger Wy) throws Exception {
    // First, define an EC curve
    // ECCurve.Fp(p, a, b); p = prime; a,b = constants defined in equation E: y^2=x^3+ax+b (mod p)
    ECCurve curve = new ECCurve.Fp(new BigInteger(ECParams.P_192_R1, 16), // p 
            new BigInteger(ECParams.A_192_R1, 16), // a
            new BigInteger(ECParams.B_192_R1, 16)); // b

    byte[] seed = Hex.decode(ECParams.SEED_192_R1);

    org.bouncycastle.math.ec.ECPoint gPoint = curve.createPoint(Wx, Wy);

    return new ECPublicKeyParameters(gPoint,
            new ECDomainParameters(curve, curve.decodePoint(Hex.decode(ECParams.G_192_R1_NCOMP)), // G       
                    new BigInteger(ECParams.N_192_R1, 16), // n
                    new BigInteger(ECParams.H_192_R1, 16), // h 
                    seed));
}