Example usage for org.bouncycastle.crypto.generators ECKeyPairGenerator ECKeyPairGenerator

List of usage examples for org.bouncycastle.crypto.generators ECKeyPairGenerator ECKeyPairGenerator

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.generators ECKeyPairGenerator ECKeyPairGenerator.

Prototype

ECKeyPairGenerator

Source Link

Usage

From source file:ECIESTest.java

public TestResult perform() {
    SecureRandom random = new SecureRandom();
    ECCurve.Fp curve = new ECCurve.Fp(
            new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839"), // q
            new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc", 16), // a
            new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16)); // b

    ECDomainParameters params = new ECDomainParameters(curve,
            curve.decodePoint(Hex.decode("020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf")), // G
            new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307")); // n

    ECKeyPairGenerator pGen = new ECKeyPairGenerator();
    ECKeyGenerationParameters genParam = new ECKeyGenerationParameters(params, random);

    pGen.init(genParam);/*from  www .  j a v  a2 s  . c  om*/

    AsymmetricCipherKeyPair p1 = pGen.generateKeyPair();
    AsymmetricCipherKeyPair p2 = pGen.generateKeyPair();

    //
    // stream test
    //
    IESEngine i1 = new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()),
            new HMac(new SHA1Digest()));
    IESEngine i2 = new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()),
            new HMac(new SHA1Digest()));
    byte[] d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
    byte[] e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 };
    IESParameters p = new IESParameters(d, e, 64);

    i1.init(true, p1.getPrivate(), p2.getPublic(), p);
    i2.init(false, p2.getPrivate(), p1.getPublic(), p);

    byte[] message = Hex.decode("1234567890abcdef");

    try {
        byte[] out1 = i1.processBlock(message, 0, message.length);

        byte[] out2 = i2.processBlock(out1, 0, out1.length);

        if (!sameAs(out2, message)) {
            return new SimpleTestResult(false, this.getName() + ": stream cipher test failed");
        }

    } catch (Exception ex) {
        return new SimpleTestResult(false, this.getName() + ": stream cipher test exception " + ex.toString());
    }

    //
    // twofish with IV0 test
    //
    BufferedBlockCipher c1 = new PaddedBufferedBlockCipher(new CBCBlockCipher(new TwofishEngine()));
    BufferedBlockCipher c2 = new PaddedBufferedBlockCipher(new CBCBlockCipher(new TwofishEngine()));
    i1 = new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()),
            new HMac(new SHA1Digest()), c1);
    i2 = new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()),
            new HMac(new SHA1Digest()), c2);
    d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
    e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 };
    p = new IESWithCipherParameters(d, e, 64, 128);

    i1.init(true, p1.getPrivate(), p2.getPublic(), p);
    i2.init(false, p2.getPrivate(), p1.getPublic(), p);

    message = Hex.decode("1234567890abcdef");

    try {
        byte[] out1 = i1.processBlock(message, 0, message.length);

        byte[] out2 = i2.processBlock(out1, 0, out1.length);

        if (!sameAs(out2, message)) {
            return new SimpleTestResult(false, this.getName() + ": twofish cipher test failed");
        }
    } catch (Exception ex) {
        return new SimpleTestResult(false, this.getName() + ": twofish cipher test exception " + ex.toString());
    }

    return new SimpleTestResult(true, this.getName() + ": Okay");
}

From source file:com.bitsofproof.supernode.api.ECKeyPair.java

License:Apache License

public static ECKeyPair createNew(boolean compressed) {
    ECKeyPairGenerator generator = new ECKeyPairGenerator();
    ECKeyGenerationParameters keygenParams = new ECKeyGenerationParameters(domain, secureRandom);
    generator.init(keygenParams);/*from  w  w w.ja  va2 s. c o  m*/
    AsymmetricCipherKeyPair keypair = generator.generateKeyPair();
    ECPrivateKeyParameters privParams = (ECPrivateKeyParameters) keypair.getPrivate();
    ECPublicKeyParameters pubParams = (ECPublicKeyParameters) keypair.getPublic();
    ECKeyPair k = new ECKeyPair();
    k.priv = privParams.getD();
    k.compressed = compressed;
    if (compressed) {
        ECPoint q = pubParams.getQ();
        k.pub = new ECPoint.Fp(domain.getCurve(), q.getX(), q.getY(), true).getEncoded();
    } else {
        k.pub = pubParams.getQ().getEncoded();
    }
    return k;
}

From source file:com.DSC.crypto.ECKey.java

License:Open Source License

/**
 * ECKey object constructor//from  www.  ja  v  a  2  s  .  co m
 * @param param ECDomainParameters for creating the elliptic curve keypair
 */
public ECKey(ECDomainParameters param) {
    /* Instantiate the elliptic curve key parameters */
    this.ECKeyPairGen = new ECKeyPairGenerator();
    this.keyGenParam = new ECKeyGenerationParameters(param, new SecureRandom());
}

From source file:com.DSC.crypto.ECKey.java

License:Open Source License

/**
 * ECKey object constructor, uses the default key domain parameters
 *///w  w w .j  a v  a 2s . c o m
public ECKey() {
    ECKeyParam param = new ECKeyParam();

    /* Instantiate the elliptic curve key parameters */
    this.ECKeyPairGen = new ECKeyPairGenerator();
    this.keyGenParam = new ECKeyGenerationParameters(param.getECDomainParam(), new SecureRandom());
}

From source file:com.google.bitcoin.core.ECKey.java

License:Apache License

/**
 * Generates an entirely new keypair. Point compression is used so the resulting public key will be 33 bytes
 * (32 for the co-ordinate and 1 byte to represent the y bit).
 *//*from   ww  w  .j a va2  s . c om*/
public ECKey() {
    ECKeyPairGenerator generator = new ECKeyPairGenerator();
    ECKeyGenerationParameters keygenParams = new ECKeyGenerationParameters(CURVE, secureRandom);
    generator.init(keygenParams);
    AsymmetricCipherKeyPair keypair = generator.generateKeyPair();
    ECPrivateKeyParameters privParams = (ECPrivateKeyParameters) keypair.getPrivate();
    ECPublicKeyParameters pubParams = (ECPublicKeyParameters) keypair.getPublic();
    priv = privParams.getD();
    // Unfortunately Bouncy Castle does not let us explicitly change a point to be compressed, even though it
    // could easily do so. We must re-build it here so the ECPoints withCompression flag can be set to true.
    ECPoint uncompressed = pubParams.getQ();
    ECPoint compressed = compressPoint(uncompressed);
    pub = compressed.getEncoded();

    creationTimeSeconds = Utils.currentTimeMillis() / 1000;
}

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

License:Apache License

/**
 * Init key pair generation engine/*from www .ja  v a 2s .  c  om*/
 */
private void initEngine() {
    // only public key params, see specification
    if (publicKey != null) {
        keyGenerationParameters = ((KeyImpl) publicKey).getKeyGenerationParameters(rnd);
    }
    switch (algorithm) {
    case KeyPair.ALG_RSA:
    case KeyPair.ALG_RSA_CRT:
        if (keyGenerationParameters == null) {
            keyGenerationParameters = RSAKeyImpl.getDefaultKeyGenerationParameters(keyLength, rnd);
        }
        engine = new RSAKeyPairGenerator();
        break;
    //
    case KeyPair.ALG_DSA:
        if (keyLength < 512 || keyLength > 1024 || keyLength % 64 != 0) {
            CryptoException.throwIt(CryptoException.ILLEGAL_VALUE);
        }
        if (keyGenerationParameters == null) {
            keyGenerationParameters = DSAKeyImpl.getDefaultKeyGenerationParameters(keyLength, rnd);
        }
        engine = new DSAKeyPairGenerator();
        break;

    // ecc
    case KeyPair.ALG_EC_F2M:
    case KeyPair.ALG_EC_FP:
        if (keyGenerationParameters == null) {
            keyGenerationParameters = ECKeyImpl.getDefaultKeyGenerationParameters(algorithm, keyLength, rnd);
        }
        engine = new ECKeyPairGenerator();
        break;

    default:
        CryptoException.throwIt(CryptoException.NO_SUCH_ALGORITHM);
        break;
    }
    engine.init(keyGenerationParameters);

}

From source file:common.crypto.bouncycastle.CEccKeyGeneratorBC.java

License:Open Source License

@Override
public void initialize(IX9ECParameters curveSpec) throws Exception {

    ECDomainParameters domainParams = new ECDomainParameters(
            (ECCurve.Fp) ((IAdapter) curveSpec.getCurveFP()).getObject(),
            (ECPoint.Fp) ((IAdapter) curveSpec.getG()).getObject(), curveSpec.getN());
    m_keyGen = null;/*  ww  w  .  j  ava2  s . c  o m*/
    m_keyGen = new ECKeyPairGenerator();
    m_keyGen.init(new ECKeyGenerationParameters(domainParams, new SecureRandom()));

}

From source file:COSE.OneKey.java

static private OneKey generateECDSAKey(String curveName, CBORObject curve) {
    X9ECParameters p = NISTNamedCurves.getByName(curveName);

    ECDomainParameters parameters = new ECDomainParameters(p.getCurve(), p.getG(), p.getN(), p.getH());
    ECKeyPairGenerator pGen = new ECKeyPairGenerator();
    ECKeyGenerationParameters genParam = new ECKeyGenerationParameters(parameters, null);
    pGen.init(genParam);//ww w  .j  a  va  2  s  . com

    AsymmetricCipherKeyPair p1 = pGen.generateKeyPair();

    ECPublicKeyParameters keyPublic = (ECPublicKeyParameters) p1.getPublic();
    ECPrivateKeyParameters keyPrivate = (ECPrivateKeyParameters) p1.getPrivate();

    byte[] rgbX = keyPublic.getQ().normalize().getXCoord().getEncoded();
    byte[] rgbY = keyPublic.getQ().normalize().getYCoord().getEncoded();
    boolean signY = true;
    byte[] rgbD = keyPrivate.getD().toByteArray();

    OneKey key = new OneKey();

    key.add(KeyKeys.KeyType, KeyKeys.KeyType_EC2);
    key.add(KeyKeys.EC2_Curve, curve);
    key.add(KeyKeys.EC2_X, CBORObject.FromObject(rgbX));
    key.add(KeyKeys.EC2_Y, CBORObject.FromObject(rgbY));
    key.add(KeyKeys.EC2_D, CBORObject.FromObject(rgbD));

    return key;
}

From source file:COSE.Recipient.java

private void ECDH_GenerateEphemeral() throws CoseException {
    X9ECParameters p = privateKey.GetCurve();
    ECDomainParameters parameters = new ECDomainParameters(p.getCurve(), p.getG(), p.getN(), p.getH());

    ECKeyPairGenerator pGen = new ECKeyPairGenerator();
    ECKeyGenerationParameters genParam = new ECKeyGenerationParameters(parameters, null);
    pGen.init(genParam);//w  w  w .j a v  a  2s.  co m

    AsymmetricCipherKeyPair p1 = pGen.generateKeyPair();

    CBORObject epk = CBORObject.NewMap();
    epk.Add(KeyKeys.KeyType.AsCBOR(), KeyKeys.KeyType_EC2);
    epk.Add(KeyKeys.EC2_Curve.AsCBOR(), privateKey.get(KeyKeys.EC2_Curve.AsCBOR()));
    ECPublicKeyParameters priv = (ECPublicKeyParameters) p1.getPublic();

    byte[] rgbEncoded = priv.getQ().normalize().getEncoded(true);
    byte[] X = new byte[rgbEncoded.length - 1];
    System.arraycopy(rgbEncoded, 1, X, 0, X.length);
    epk.Add(KeyKeys.EC2_X.AsCBOR(), CBORObject.FromObject(X));
    epk.Add(KeyKeys.EC2_Y.AsCBOR(), CBORObject.FromObject((rgbEncoded[0] & 1) == 1));
    addAttribute(HeaderKeys.ECDH_EPK, epk, Attribute.UNPROTECTED);

    OneKey secretKey = new OneKey();
    secretKey.add(KeyKeys.KeyType, KeyKeys.KeyType_EC2);
    secretKey.add(KeyKeys.EC2_Curve, privateKey.get(KeyKeys.EC2_Curve.AsCBOR()));
    secretKey.add(KeyKeys.EC2_X, CBORObject.FromObject(X));
    secretKey.add(KeyKeys.EC2_Y, CBORObject.FromObject((rgbEncoded[0] & 1) == 1));
    ECPrivateKeyParameters priv1 = (ECPrivateKeyParameters) p1.getPrivate();
    secretKey.add(KeyKeys.EC2_D, CBORObject.FromObject(BigIntegers.asUnsignedByteArray(priv1.getD())));

    senderKey = secretKey;
}

From source file:COSE.Sign1MessageTest.java

@BeforeClass
public static void setUpClass() throws CoseException {

    X9ECParameters p = NISTNamedCurves.getByName("P-256");

    ECDomainParameters parameters = new ECDomainParameters(p.getCurve(), p.getG(), p.getN(), p.getH());
    ECKeyPairGenerator pGen = new ECKeyPairGenerator();
    ECKeyGenerationParameters genParam = new ECKeyGenerationParameters(parameters, null);
    pGen.init(genParam);//  w  w w.  ja v  a2s  . c  o  m

    AsymmetricCipherKeyPair p1 = pGen.generateKeyPair();

    keyPublic = (ECPublicKeyParameters) p1.getPublic();
    keyPrivate = (ECPrivateKeyParameters) p1.getPrivate();

    byte[] rgbX = keyPublic.getQ().normalize().getXCoord().getEncoded();
    byte[] rgbY = keyPublic.getQ().normalize().getYCoord().getEncoded();
    boolean signY = true;
    byte[] rgbD = keyPrivate.getD().toByteArray();

    CBORObject key = CBORObject.NewMap();
    key.Add(KeyKeys.KeyType.AsCBOR(), KeyKeys.KeyType_EC2);
    key.Add(KeyKeys.EC2_Curve.AsCBOR(), KeyKeys.EC2_P256);
    key.Add(KeyKeys.EC2_X.AsCBOR(), rgbX);
    key.Add(KeyKeys.EC2_Y.AsCBOR(), rgbY);
    cnKeyPublic = new OneKey(key);

    key = CBORObject.NewMap();
    key.Add(KeyKeys.KeyType.AsCBOR(), KeyKeys.KeyType_EC2);
    key.Add(KeyKeys.EC2_Curve.AsCBOR(), KeyKeys.EC2_P256);
    key.Add(KeyKeys.EC2_X.AsCBOR(), rgbX);
    key.Add(KeyKeys.EC2_Y.AsCBOR(), rgbY);
    cnKeyPublicCompressed = new OneKey(key);

    key = CBORObject.NewMap();
    key.Add(KeyKeys.KeyType.AsCBOR(), KeyKeys.KeyType_EC2);
    key.Add(KeyKeys.EC2_Curve.AsCBOR(), KeyKeys.EC2_P256);
    key.Add(KeyKeys.EC2_D.AsCBOR(), rgbD);
    cnKeyPrivate = new OneKey(key);
}