Example usage for org.bouncycastle.crypto.engines EthereumIESEngine processBlock

List of usage examples for org.bouncycastle.crypto.engines EthereumIESEngine processBlock

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.engines EthereumIESEngine processBlock.

Prototype

public byte[] processBlock(byte[] in, int inOff, int inLen) throws InvalidCipherTextException 

Source Link

Usage

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

License:Open Source License

/**
 *  Encryption equivalent to the Crypto++ default ECIES<ECP> settings:
 *
 *  DL_KeyAgreementAlgorithm:        DL_KeyAgreementAlgorithm_DH<struct ECPPoint,struct EnumToType<enum CofactorMultiplicationOption,0> >
 *  DL_KeyDerivationAlgorithm:       DL_KeyDerivationAlgorithm_P1363<struct ECPPoint,0,class P1363_KDF2<class SHA1> >
 *  DL_SymmetricEncryptionAlgorithm: DL_EncryptionAlgorithm_Xor<class HMAC<class SHA1>,0>
 *  DL_PrivateKey:                   DL_Key<ECPPoint>
 *  DL_PrivateKey_EC<class ECP>//from  ww w . j av a 2s.  c o  m
 *
 *  Used for Whisper V3
 */
public static byte[] decryptSimple(BigInteger privKey, byte[] cipher)
        throws IOException, InvalidCipherTextException {
    EthereumIESEngine iesEngine = new EthereumIESEngine(new ECDHBasicAgreement(),
            new MGF1BytesGeneratorExt(new SHA1Digest(), 1), new HMac(new SHA1Digest()), new SHA1Digest(), null);

    IESParameters p = new IESParameters(null, null, KEY_SIZE);
    ParametersWithIV parametersWithIV = new ParametersWithIV(p, new byte[0]);

    iesEngine.setHashMacKey(false);

    iesEngine.init(new ECPrivateKeyParameters(privKey, CURVE), parametersWithIV,
            new ECIESPublicKeyParser(ECKey.CURVE));

    return iesEngine.processBlock(cipher, 0, cipher.length);
}

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

License:Open Source License

public static byte[] decrypt(BigInteger prv, byte[] cipher) throws InvalidCipherTextException, IOException {
    ByteArrayInputStream is = new ByteArrayInputStream(cipher);
    byte[] ephemBytes = new byte[2 * ((curve.getCurve().getFieldSize() + 7) / 8) + 1];
    is.read(ephemBytes);//from  w w  w .j av a 2s. c  o  m
    ECPoint ephem = curve.getCurve().decodePoint(ephemBytes);
    byte[] IV = new byte[KEY_SIZE / 8];
    is.read(IV);
    byte[] cipherBody = new byte[is.available()];
    is.read(cipherBody);

    EthereumIESEngine iesEngine = makeIESEngine(false, ephem, prv, IV);

    byte[] message = iesEngine.processBlock(cipherBody, 0, cipherBody.length);
    return message;
}

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

License:Open Source License

public static byte[] encrypt(ECPoint toPub, byte[] plaintext) throws InvalidCipherTextException, IOException {

    ECKeyPairGenerator eGen = new ECKeyPairGenerator();
    SecureRandom random = new SecureRandom();
    KeyGenerationParameters gParam = new ECKeyGenerationParameters(curve, random);

    eGen.init(gParam);// w w  w.  j  a v  a2s  .  c  om

    byte[] IV = new byte[KEY_SIZE / 8];
    new SecureRandom().nextBytes(IV);

    AsymmetricCipherKeyPair ephemPair = eGen.generateKeyPair();
    BigInteger prv = ((ECPrivateKeyParameters) ephemPair.getPrivate()).getD();
    ECPoint pub = ((ECPublicKeyParameters) ephemPair.getPublic()).getQ();
    EthereumIESEngine iesEngine = makeIESEngine(true, toPub, prv, IV);

    ECKeyGenerationParameters keygenParams = new ECKeyGenerationParameters(curve, random);
    ECKeyPairGenerator generator = new ECKeyPairGenerator();
    generator.init(keygenParams);

    ECKeyPairGenerator gen = new ECKeyPairGenerator();
    gen.init(new ECKeyGenerationParameters(ECKey.CURVE, random));

    byte[] cipher = iesEngine.processBlock(plaintext, 0, plaintext.length);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bos.write(pub.getEncoded(false));
    bos.write(IV);
    bos.write(cipher);
    return bos.toByteArray();
}