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

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

Introduction

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

Prototype

public ECDomainParameters(ECCurve curve, ECPoint G, BigInteger n) 

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 w w.  j  ava  2  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:card.CardClient.java

License:Open Source License

/**
 * Initialise the cryptographic parameters on the card
 *  //from www.  j av a  2 s .c  o  m
 * @param p Prime number for the finite field F_P
 * @param a A parameter defining the curve: y^2 = x^3 + Ax + B (mod P)
 * @param b B parameter defining the curve: y^2 = x^3 + Ax + B (mod P)
 * @param g Generator point on the curve
 * @param r Order of the generator
 * @return 
 */
public ECPoint initialise(BigInteger p, BigInteger r, BigInteger a, BigInteger b, ECPoint g) {
    try {
        curve = new ECCurve.Fp(p, a, b);
        ecParams = new ECParameterSpec(curve, g, r);
        ecDom = new ECDomainParameters(curve, g, r);
        keyGen = KeyPairGenerator.getInstance("ECDH", "BC");
        keyGen.initialize(ecParams);
        keys = keyGen.generateKeyPair();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return ((ECPublicKey) keys.getPublic()).getQ();
}

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

License:Open Source License

/**
 * The default constructor, creates an instance of ECKeyParam object using the
 * default named curve which at the present moment is secp256r1.
 *///w ww.  j  a v  a2 s.  c om
public ECKeyParam() {
    /* Instantiate the ECParameterSpec and ECDomainParameters objects */
    this.ECParamSpec = ECNamedCurveTable.getParameterSpec(defNamedCurve);
    this.ECDomainParam = new ECDomainParameters(ECParamSpec.getCurve(), // Curve
            ECParamSpec.getG(), // G
            ECParamSpec.getN()); // N   
}

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

License:Open Source License

/**
 * Creates an instance of the ECKeyParam object with using the named curve
 * specified for the elliptic curve./*w  ww .  j  a  v a  2  s .  com*/
 * 
 * For a list of supported curves and more informations on named curves see the
 * following.
 *
 * @see http://www.secg.org/collateral/sec2_final.pdf
 * @see http://www.bouncycastle.org/wiki/display/JA1/Supported+Curves+%28ECDSA+and+ECGOST%29
 * 
 * @param namedCurve The named elliptic curve to use
 * 
 * @throws InvalidParameterException if the named curve provided is not supported
 */
public ECKeyParam(String namedCurve) throws InvalidParameterException {
    /* Instantiate the ECParameterSpec and ECDomainParameters objects */
    this.ECParamSpec = ECNamedCurveTable.getParameterSpec(namedCurve);

    if (this.ECParamSpec == null) {
        throw new InvalidParameterException("Invalid named elliptic curve provided!");
    }

    this.ECDomainParam = new ECDomainParameters(ECParamSpec.getCurve(), // Curve
            ECParamSpec.getG(), // G
            ECParamSpec.getN()); // N      
}

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.j a  v  a2 s.c o  m
    m_keyGen = new ECKeyPairGenerator();
    m_keyGen.init(new ECKeyGenerationParameters(domainParams, new SecureRandom()));

}

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

License:Open Source License

@Override
public void initialize(String szCurveName, CryptoTypes.ESHAMode eShaMode) {
    ECParameterSpec paramSpec = ECNamedCurveTable.getParameterSpec(szCurveName);
    m_eccParameter = new ECDomainParameters(paramSpec.getCurve(), paramSpec.getG(), paramSpec.getN());
    m_signer = new ECDSASigner();
    m_shaHash = new CCryptoSHABC();
    m_shaHash.initialize(eShaMode);//from w ww. jav a 2  s  .  c  o m
}

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

License:Apache License

public static AsymmetricCipherKeyPair generateKeyPair(ECParameterSpec eccSpec, SecureRandom secureRandom) {
    ECKeyGenerationParameters ecParams = new ECKeyGenerationParameters(
            new ECDomainParameters(eccSpec.getCurve(), eccSpec.getG(), eccSpec.getN()), secureRandom);

    ECKeyPairGenerator ecKeyGen = new ECKeyPairGenerator();
    ecKeyGen.init(ecParams);//w  w w . j  a  v a  2s  . co  m

    return ecKeyGen.generateKeyPair();
}

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

License:Apache License

public static ECPrivateKeyParameters read(Input input) throws KryoException {
    byte[] bytes;
    int length;//from ww w .ja  v  a 2s  .  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;//from   ww w  .jav a2  s  .c om

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