Example usage for org.bouncycastle.crypto.params ECKeyGenerationParameters ECKeyGenerationParameters

List of usage examples for org.bouncycastle.crypto.params ECKeyGenerationParameters ECKeyGenerationParameters

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.params ECKeyGenerationParameters ECKeyGenerationParameters.

Prototype

public ECKeyGenerationParameters(ECDomainParameters domainParams, SecureRandom random) 

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 w ww  .j ava  2s .  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 ww. j  a v  a  2 s .  com
    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/* www  . ja v  a2  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  a2s. 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  w  ww  .  j  a  v  a 2s . c  o  m
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.ECKeyImpl.java

License:Apache License

/**
 * Get/*from  w w  w . j a v  a 2 s.c  o  m*/
 * <code>ECKeyGenerationParameters</code>
 *
 * @param rnd Secure Random Generator
 * @return parameters for use with BouncyCastle API
 */
public KeyGenerationParameters getKeyGenerationParameters(SecureRandom rnd) {
    if (isDomainParametersInitialized()) {
        return new ECKeyGenerationParameters(getDomainParameters(), rnd);
    }
    return new ECKeyGenerationParameters(getDefaultsDomainParameters(type, size), rnd);
}

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

License:Apache License

/**
 * Get default/*  w  w w.  j av a2  s .  c  o  m*/
 * <code>ECKeyGenerationParameters</code>
 *
 * @param algorithm
 * @param keySize key size in bits
 * @param rnd Secure Random Generator
 * @return parameters for use with BouncyCastle API
 */
static KeyGenerationParameters getDefaultKeyGenerationParameters(byte algorithm, short keySize,
        SecureRandom rnd) {
    byte keyType = algorithm == KeyPair.ALG_EC_FP ? KeyBuilder.TYPE_EC_FP_PUBLIC
            : KeyBuilder.TYPE_EC_F2M_PUBLIC;
    return new ECKeyGenerationParameters(getDefaultsDomainParameters(keyType, keySize), rnd);
}

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;//from w w  w . ja  va  2 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);//from w  ww. j a v a  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 ww.  j a v  a  2 s.c  om

    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;
}