Example usage for org.bouncycastle.crypto EphemeralKeyPair getKeyPair

List of usage examples for org.bouncycastle.crypto EphemeralKeyPair getKeyPair

Introduction

In this page you can find the example usage for org.bouncycastle.crypto EphemeralKeyPair getKeyPair.

Prototype

public AsymmetricCipherKeyPair getKeyPair() 

Source Link

Usage

From source file:org.ethereum.crypto.EthereumIESEngine.java

License:Open Source License

public byte[] processBlock(byte[] in, int inOff, int inLen, byte[] macData) throws InvalidCipherTextException {
    if (forEncryption) {
        if (keyPairGenerator != null) {
            EphemeralKeyPair ephKeyPair = keyPairGenerator.generate();

            this.privParam = ephKeyPair.getKeyPair().getPrivate();
            this.v = ephKeyPair.getEncodedPublicKey();
        }/*ww w.  j  a  v  a2s  . c  o m*/
    } else {
        if (keyParser != null) {
            ByteArrayInputStream bIn = new ByteArrayInputStream(in, inOff, inLen);

            try {
                this.pubParam = keyParser.readKey(bIn);
            } catch (IOException e) {
                throw new InvalidCipherTextException(
                        "unable to recover ephemeral public key: " + e.getMessage(), e);
            }

            int encLength = (inLen - bIn.available());
            this.v = Arrays.copyOfRange(in, inOff, inOff + encLength);
        }
    }

    // Compute the common value and convert to byte array.
    agree.init(privParam);
    BigInteger z = agree.calculateAgreement(pubParam);
    byte[] Z = BigIntegers.asUnsignedByteArray(agree.getFieldSize(), z);

    // Create input to KDF.
    byte[] vz;
    //        if (v.length != 0)
    //        {
    //            VZ = new byte[v.length + Z.length];
    //            System.arraycopy(v, 0, VZ, 0, v.length);
    //            System.arraycopy(Z, 0, VZ, v.length, Z.length);
    //        }
    //        else
    {
        vz = Z;
    }

    // Initialise the KDF.
    DerivationParameters kdfParam;
    if (kdf instanceof MGF1BytesGeneratorExt) {
        kdfParam = new MGFParameters(vz);
    } else {
        kdfParam = new KDFParameters(vz, param.getDerivationV());
    }
    kdf.init(kdfParam);

    return forEncryption ? encryptBlock(in, inOff, inLen, macData) : decryptBlock(in, inOff, inLen, macData);
}