org.bouncycastle.crypto.params.ParametersWithIV Java Examples

The following examples show how to use org.bouncycastle.crypto.params.ParametersWithIV. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SecurityHandler.java    From sambox with Apache License 2.0 7 votes vote down vote up
/**
 * Encrypt or decrypt data with AES256.
 *
 * @param data The data to encrypt.
 * @param output The output to write the encrypted data to.
 *
 * @throws IOException If there is an error reading the data.
 */
private void decryptDataAES256(InputStream data, OutputStream output) throws IOException
{
    byte[] iv = new byte[16];

    // read IV from stream
    int ivSize = data.read(iv);
    if (ivSize == -1)
    {
        return;
    }

    if (ivSize != iv.length)
    {
        throw new IOException("AES initialization vector not fully read: only " + ivSize
                + " bytes read instead of " + iv.length);
    }
    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
            new CBCBlockCipher(new AESFastEngine()));
    cipher.init(false, new ParametersWithIV(new KeyParameter(encryptionKey), iv));
    try (CipherInputStream cis = new CipherInputStream(data, cipher))
    {
        IOUtils.copy(cis, output);
    }
}
 
Example #2
Source File: BurstCryptoImpl.java    From burstkit4j with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] aesDecrypt(byte[] encrypted, byte[] signingKey, byte[] nonce) {
    if (signingKey.length != 32) {
        throw new IllegalArgumentException("Key length must be 32 bytes");
    }
    try {
        if (encrypted.length < 16 || encrypted.length % 16 != 0) {
            throw new InvalidCipherTextException("invalid ciphertext");
        }
        byte[] iv = Arrays.copyOfRange(encrypted, 0, 16);
        byte[] ciphertext = Arrays.copyOfRange(encrypted, 16, encrypted.length);
        for (int i = 0; i < 32; i++) {
            signingKey[i] ^= nonce[i];
        }
        byte[] key = getSha256().digest(signingKey);
        PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));
        CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
        aes.init(false, ivAndKey);
        byte[] output = new byte[aes.getOutputSize(ciphertext.length)];
        int plaintextLength = aes.processBytes(ciphertext, 0, ciphertext.length, output, 0);
        plaintextLength += aes.doFinal(output, plaintextLength);
        byte[] result = new byte[plaintextLength];
        System.arraycopy(output, 0, result, 0, result.length);
        return result;
    } catch (InvalidCipherTextException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
 
Example #3
Source File: AESEncrypterBC.java    From fingen with Apache License 2.0 6 votes vote down vote up
/**
 * encrypt 16 bytes (AES standard block size) or less
 * starting at "pos" within "in" byte[]
 */
protected void encryptBlock( byte[] in, int pos, int length ) {
	byte[] encryptedIn = new byte[blockSize];
	byte[] ivBytes = ByteArrayHelper.toByteArray( nonce++, 16 );
	ParametersWithIV ivParams = new ParametersWithIV(cipherParameters, ivBytes);
	aesCipher.init( true, ivParams );

	int remainingCount = length-pos;
	if( remainingCount>=blockSize ) {
		aesCipher.processBlock( in, pos, encryptedIn, 0 );
		System.arraycopy( encryptedIn, 0, in, pos, blockSize );
		mac.update( encryptedIn, 0, blockSize );
	} else {
		byte[] extendedIn = new byte[blockSize];
		System.arraycopy( in, pos, extendedIn, 0, remainingCount );
		aesCipher.processBlock( extendedIn, 0, encryptedIn, 0 );
		System.arraycopy( encryptedIn, 0, in, pos, remainingCount );
		mac.update( encryptedIn, 0, remainingCount );
	}
}
 
Example #4
Source File: AESDecrypterBC.java    From fingen with Apache License 2.0 6 votes vote down vote up
/**
 * encrypt 16 bytes (AES standard block size) or less
 * starting at "pos" within "in" byte[]
 */
protected void decryptBlock( byte[] in, int pos, int length ) {
	byte[] decryptedIn = new byte[blockSize];
	byte[] ivBytes = ByteArrayHelper.toByteArray( nonce++, 16 );
	ParametersWithIV ivParams = new ParametersWithIV(cipherParameters, ivBytes);
	aesCipher.init( false, ivParams );

	int remainingCount = length-pos;
	if( remainingCount>=blockSize ) {
		mac.update( in, pos, blockSize );
		aesCipher.processBlock( in, pos, decryptedIn, 0 );
		System.arraycopy( decryptedIn, 0, in, pos, blockSize );
	} else {
		mac.update( in, pos, remainingCount );
		byte[] extendedIn = new byte[blockSize];
		System.arraycopy( in, pos, extendedIn, 0, remainingCount );
		aesCipher.processBlock( extendedIn, 0, decryptedIn, 0 );
		System.arraycopy( decryptedIn, 0, in, pos, remainingCount );
	}
}
 
Example #5
Source File: FileDecrypter.java    From LiquidDonkey with MIT License 6 votes vote down vote up
byte[] decrypt(
        InputStream input,
        OutputStream output,
        long blockCount,
        ParametersWithIV ivKey,
        KeyParameter fileKey) throws IOException {

    byte[] hash = new byte[digest.getDigestSize()];
    digest.reset();
    
    for (int block = 0; block < blockCount; block++) {
        int length = input.read(in);
        if (length == -1) {
            logger.warn("-- decrypt() > empty block");
            break;
        }
        digest.update(in, 0, length);
        decryptBlock(fileKey, deriveIv(ivKey, block), in, length, out);
        output.write(out, 0, length);
    }

    digest.doFinal(hash, 0);
    return hash;
}
 
Example #6
Source File: EmulatorP11Identity.java    From xipki with Apache License 2.0 6 votes vote down vote up
private byte[] aesGmac(P11Params params, byte[] contentToSign) throws P11TokenException {
  if (params == null) {
    throw new P11TokenException("iv may not be null");
  }

  byte[] iv;
  if (params instanceof P11Params.P11IVParams) {
    iv = ((P11Params.P11IVParams) params).getIV();
  } else {
    throw new P11TokenException("params must be instanceof P11IVParams");
  }

  GMac gmac = new GMac(new GCMBlockCipher(new AESEngine()));
  ParametersWithIV paramsWithIv =
      new ParametersWithIV(new KeyParameter(signingKey.getEncoded()), iv);
  gmac.init(paramsWithIv);
  gmac.update(contentToSign, 0, contentToSign.length);
  byte[] signature = new byte[gmac.getMacSize()];
  gmac.doFinal(signature, 0);
  return signature;
}
 
Example #7
Source File: AESEncrypt.java    From nuls-v2 with MIT License 6 votes vote down vote up
/**
 * 数据通过KeyParameter和初始化向量加密
 *
 * @param plainBytes 需要加密的数据
 * @param iv         初始化向量
 * @param aesKey     秘钥
 * @return 加密后的数据
 */
public static EncryptedData encrypt(byte[] plainBytes, byte[] iv, KeyParameter aesKey) throws RuntimeException {
    HexUtil.checkNotNull(plainBytes);
    HexUtil.checkNotNull(aesKey);
    try {
        if (iv == null) {
            iv = EncryptedData.DEFAULT_IV;
            //SECURE_RANDOM.nextBytes(iv);
        }
        ParametersWithIV keyWithIv = new ParametersWithIV(aesKey, iv);
        // Encrypt using AES.
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(true, keyWithIv);
        byte[] encryptedBytes = new byte[cipher.getOutputSize(plainBytes.length)];
        final int length1 = cipher.processBytes(plainBytes, 0, plainBytes.length, encryptedBytes, 0);
        final int length2 = cipher.doFinal(encryptedBytes, length1);

        return new EncryptedData(iv, Arrays.copyOf(encryptedBytes, length1 + length2));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #8
Source File: BCStrongAESEncryption.java    From Hive2Hive with MIT License 6 votes vote down vote up
private static byte[] processAESCipher(boolean encrypt, byte[] data, SecretKey key, byte[] initVector)
		throws DataLengthException, IllegalStateException, InvalidCipherTextException {
	// seat up engine, block cipher mode and padding
	AESEngine aesEngine = new AESEngine();
	CBCBlockCipher cbc = new CBCBlockCipher(aesEngine);
	PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(cbc);

	// apply parameters
	CipherParameters parameters = new ParametersWithIV(new KeyParameter(key.getEncoded()), initVector);
	cipher.init(encrypt, parameters);

	// process ciphering
	byte[] output = new byte[cipher.getOutputSize(data.length)];

	int bytesProcessed1 = cipher.processBytes(data, 0, data.length, output, 0);
	int bytesProcessed2 = cipher.doFinal(output, bytesProcessed1);
	byte[] result = new byte[bytesProcessed1 + bytesProcessed2];
	System.arraycopy(output, 0, result, 0, result.length);
	return result;
}
 
Example #9
Source File: BurstCryptoImpl.java    From burstkit4j with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] aesEncrypt(byte[] plaintext, byte[] signingKey, byte[] nonce) {
    if (signingKey.length != 32) {
        throw new IllegalArgumentException("Key length must be 32 bytes");
    }
    try {
        for (int i = 0; i < 32; i++) {
            signingKey[i] ^= nonce[i];
        }
        byte[] key = getSha256().digest(signingKey);
        byte[] iv = new byte[16];
        secureRandom.nextBytes(iv);
        PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));
        CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
        aes.init(true, ivAndKey);
        byte[] output = new byte[aes.getOutputSize(plaintext.length)];
        int ciphertextLength = aes.processBytes(plaintext, 0, plaintext.length, output, 0);
        ciphertextLength += aes.doFinal(output, ciphertextLength);
        byte[] result = new byte[iv.length + ciphertextLength];
        System.arraycopy(iv, 0, result, 0, iv.length);
        System.arraycopy(output, 0, result, iv.length, ciphertextLength);
        return result;
    } catch (InvalidCipherTextException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
 
Example #10
Source File: AESEncrypt.java    From nuls-v2 with MIT License 6 votes vote down vote up
/**
 * 数据通过KeyParameter解密
 *
 * @param dataToDecrypt 需要解密的数据
 * @param aesKey        秘钥
 * @return 解密后的数据
 */
public static byte[] decrypt(EncryptedData dataToDecrypt, KeyParameter aesKey) throws CryptoException {
    HexUtil.checkNotNull(dataToDecrypt);
    HexUtil.checkNotNull(aesKey);

    try {
        ParametersWithIV keyWithIv = new ParametersWithIV(new KeyParameter(aesKey.getKey()), dataToDecrypt.getInitialisationVector());

        // Decrypt the validator.
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(false, keyWithIv);

        byte[] cipherBytes = dataToDecrypt.getEncryptedBytes();
        byte[] decryptedBytes = new byte[cipher.getOutputSize(cipherBytes.length)];
        final int length1 = cipher.processBytes(cipherBytes, 0, cipherBytes.length, decryptedBytes, 0);
        final int length2 = cipher.doFinal(decryptedBytes, length1);

        return Arrays.copyOf(decryptedBytes, length1 + length2);
    } catch (Exception e) {
        throw new CryptoException();
    }
}
 
Example #11
Source File: AESCBC.java    From InflatableDonkey with MIT License 6 votes vote down vote up
public static byte[] decryptAESCBC(byte[] key, byte[] iv, byte[] data) {
    // AES CBC PKCS7 decrypt
    try {
        CipherParameters cipherParameters = new ParametersWithIV(new KeyParameter(key), iv);
        PaddedBufferedBlockCipher cipher
                = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()), new PKCS7Padding());
        cipher.init(false, cipherParameters);

        byte[] buffer = new byte[cipher.getOutputSize(data.length)];

        int pos = cipher.processBytes(data, 0, data.length, buffer, 0);
        pos += cipher.doFinal(buffer, pos);

        return Arrays.copyOf(buffer, pos);

    } catch (DataLengthException | IllegalStateException | InvalidCipherTextException ex) {
        throw new IllegalArgumentException("decrypt failed", ex);
    }
}
 
Example #12
Source File: DPAESCBCCipher.java    From InflatableDonkey with MIT License 6 votes vote down vote up
@Override
public int processBlock(byte[] in, int inOff, byte[] out, int outOff) throws DataLengthException, IllegalStateException {
    if (key == null) {
        throw new IllegalStateException("not initialised");
    }

    if (offset == 0) {
        byte[] iv = ivGenerator.apply(index);
        ParametersWithIV parameters = new ParametersWithIV(key, iv);
        cipher.init(forEncryption, parameters);
    }

    offset += getBlockSize();
    if (offset == blockLength) {
        index++;
        offset = 0;
    }
    return cipher.processBlock(in, inOff, out, outOff);
}
 
Example #13
Source File: Framer.java    From besu with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new framer out of the handshake secrets derived during the cryptographic handshake.
 *
 * @param secrets The handshake secrets.
 */
public Framer(final HandshakeSecrets secrets) {
  this.secrets = secrets;

  final KeyParameter aesKey = new KeyParameter(secrets.getAesSecret());
  final KeyParameter macKey = new KeyParameter(secrets.getMacSecret());

  encryptor = new SICBlockCipher(new AESEngine());
  encryptor.init(true, new ParametersWithIV(aesKey, IV));

  decryptor = new SICBlockCipher(new AESEngine());
  decryptor.init(false, new ParametersWithIV(aesKey, IV));

  macEncryptor = new AESEngine();
  macEncryptor.init(true, macKey);
}
 
Example #14
Source File: CredStashBouncyCastleCrypto.java    From jcredstash with Apache License 2.0 6 votes vote down vote up
private byte[] encryptOrDecrypt(byte[] key, byte[] contents, boolean forEncryption) {

        // Credstash uses standard AES
        BlockCipher engine = new AESFastEngine();

        // Credstash uses CTR mode
        StreamBlockCipher cipher = new SICBlockCipher(engine);

        cipher.init(forEncryption, new ParametersWithIV(new KeyParameter(key), INITIALIZATION_VECTOR));

        byte[] resultBytes = new byte[contents.length];
        int contentsOffset = 0;
        int resultOffset = 0;
        cipher.processBytes(contents, contentsOffset, contents.length, resultBytes, resultOffset);
        return resultBytes;
    }
 
Example #15
Source File: FileDecrypter.java    From LiquidDonkey with MIT License 5 votes vote down vote up
ParametersWithIV deriveIvKey(ByteString key) {
    byte[] hash = new byte[digest.getDigestSize()];
    
    digest.reset();
    digest.update(key.toByteArray(), 0, key.size());
    digest.doFinal(hash, 0);

    return new ParametersWithIV(
            new KeyParameter(Arrays.copyOfRange(hash, 0, 16)),
            new byte[16]);
}
 
Example #16
Source File: AESCrypto.java    From shadowsocks-vertx with Apache License 2.0 5 votes vote down vote up
@Override
protected StreamCipher createCipher(byte[] iv, boolean encrypt)
{
    StreamBlockCipher c = getCipher(encrypt);
    ParametersWithIV parameterIV = new ParametersWithIV(new KeyParameter(mKey), iv, 0, mIVLength);
    c.init(encrypt, parameterIV);
    return c;
}
 
Example #17
Source File: AESEngineNoPadding.java    From sambox with Apache License 2.0 5 votes vote down vote up
private void init(byte[] key, byte[] iv)
{
    cipher.reset();
    if (nonNull(iv))
    {
        cipher.init(true, new ParametersWithIV(new KeyParameter(key), iv));
    }
    else
    {
        cipher.init(true, new KeyParameter(key));
    }
}
 
Example #18
Source File: AESCipher.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/** Creates a new instance of AESCipher */
public AESCipher(boolean forEncryption, byte[] key, byte[] iv) {
    BlockCipher aes = new AESFastEngine();
    BlockCipher cbc = new CBCBlockCipher(aes);
    bp = new PaddedBufferedBlockCipher(cbc);
    KeyParameter kp = new KeyParameter(key);
    ParametersWithIV piv = new ParametersWithIV(kp, iv);
    bp.init(forEncryption, piv);
}
 
Example #19
Source File: FileDecrypter.java    From LiquidDonkey with MIT License 5 votes vote down vote up
byte[] deriveIv(ParametersWithIV ivKey, int block) {
    byte[] blockHash = blockHash(block);
    byte[] iv = new byte[0x10];
    cbcAes.init(true, ivKey);
    cbcAes.processBytes(blockHash, 0, blockHash.length, iv, 0);
    return iv;
}
 
Example #20
Source File: BlowfishCipher.java    From AgentX with Apache License 2.0 5 votes vote down vote up
@Override
protected void _init(boolean isEncrypt, byte[] iv) {
    String keyStr = new String(key.getEncoded());
    ParametersWithIV params = new ParametersWithIV(
            new KeyParameter(KeyHelper.generateKeyDigest(keyLength, keyStr)), iv
    );
    cipher.init(isEncrypt, params);
}
 
Example #21
Source File: GeoWaveEncryption.java    From geowave with Apache License 2.0 5 votes vote down vote up
private PaddedBufferedBlockCipher getCipher(final boolean encrypt) {
  final PaddedBufferedBlockCipher cipher =
      new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
  final CipherParameters ivAndKey =
      new ParametersWithIV(new KeyParameter(getKey().getEncoded()), salt);
  cipher.init(encrypt, ivAndKey);
  return cipher;
}
 
Example #22
Source File: ChaCha20Encryptor.java    From archistar-smc with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public byte[] encrypt(byte[] data, byte[] randomKeyBytes) throws IOException, InvalidKeyException,
        InvalidAlgorithmParameterException, InvalidCipherTextException {

    ChaChaEngine cipher = new ChaChaEngine();
    cipher.init(true, new ParametersWithIV(new KeyParameter(randomKeyBytes), randomIvBytes));

    byte[] result = new byte[data.length];
    cipher.processBytes(data, 0, data.length, result, 0);
    return result;
}
 
Example #23
Source File: ChaCha20Encryptor.java    From archistar-smc with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public byte[] decrypt(byte[] data, byte[] randomKeyBytes)
        throws InvalidKeyException, InvalidAlgorithmParameterException, IOException,
        IllegalStateException, InvalidCipherTextException {

    ChaChaEngine cipher = new ChaChaEngine();
    cipher.init(false, new ParametersWithIV(new KeyParameter(randomKeyBytes), randomIvBytes));

    byte[] result = new byte[data.length];
    cipher.processBytes(data, 0, data.length, result, 0);
    return result;
}
 
Example #24
Source File: ChaCha20Encryptor.java    From archistar-smc with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Special method to decrypt partial data
 *
 * @param data to decrypt
 * @param randomKeyBytes key to use
 * @param startingByte the starting position within the (complete) data
 * @return decrypted data
 */
public byte[] decrypt(byte[] data, byte[] randomKeyBytes, long startingByte) {

    ChaChaEngine cipher = new ChaChaEngine();
    cipher.init(false, new ParametersWithIV(new KeyParameter(randomKeyBytes), randomIvBytes));
    cipher.skip(startingByte);
    byte[] result = new byte[data.length];
    cipher.processBytes(data, 0, data.length, result, 0);
    return result;
}
 
Example #25
Source File: AESEncryptor.java    From archistar-smc with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public byte[] encrypt(byte[] data, byte[] randomKeyBytes) throws IOException, InvalidKeyException,
        InvalidAlgorithmParameterException, InvalidCipherTextException {

    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
    cipher.init(true, new ParametersWithIV(new KeyParameter(randomKeyBytes), randomIvBytes));
    return cipherData(cipher, data);
}
 
Example #26
Source File: AESEncryptor.java    From archistar-smc with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public byte[] decrypt(byte[] data, byte[] randomKeyBytes)
        throws InvalidKeyException, InvalidAlgorithmParameterException, IOException, IllegalStateException, InvalidCipherTextException {

    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
    cipher.init(false, new ParametersWithIV(new KeyParameter(randomKeyBytes), randomIvBytes));
    return cipherData(cipher, data);
}
 
Example #27
Source File: AesCodec.java    From JLilyPad with GNU General Public License v3.0 5 votes vote down vote up
public AesCodec(byte[] iv) {
	this.aesCipherDecoder.init(false, new ParametersWithIV(new KeyParameter(iv), iv, 0, 16));
	this.aesCipherEncoder.init(true, new ParametersWithIV(new KeyParameter(iv), iv, 0, 16));
	this.decoderIn = new byte[bufferSize];
	this.decoderOut = new byte[this.aesCipherDecoder.getOutputSize(bufferSize)];
	this.encoderIn = new byte[bufferSize];
	this.encoderOut = new byte[this.aesCipherEncoder.getOutputSize(bufferSize)];
}
 
Example #28
Source File: RLPxConnection.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
RLPxConnection(
    Bytes32 aesSecret,
    Bytes32 macSecret,
    Bytes32 token,
    Bytes egressMac,
    Bytes ingressMac,
    SECP256K1.PublicKey publicKey,
    SECP256K1.PublicKey peerPublicKey) {
  this.aesSecret = aesSecret;
  this.macSecret = macSecret;
  this.token = token;

  KeyParameter macKey = new KeyParameter(macSecret.toArrayUnsafe());
  macEncryptionEngine = new AESEngine();
  macEncryptionEngine.init(true, macKey);

  updateEgress(egressMac);
  updateIngress(ingressMac);
  this.publicKey = publicKey;
  this.peerPublicKey = peerPublicKey;

  KeyParameter aesKey = new KeyParameter(aesSecret.toArrayUnsafe());

  byte[] IV = new byte[16];
  Arrays.fill(IV, (byte) 0);

  decryptionCipher = new SICBlockCipher(new AESEngine());
  decryptionCipher.init(false, new ParametersWithIV(aesKey, IV));

  encryptionCipher = new SICBlockCipher(new AESEngine());
  encryptionCipher.init(true, new ParametersWithIV(aesKey, IV));
}
 
Example #29
Source File: AesCipher.java    From AgentX with Apache License 2.0 5 votes vote down vote up
@Override
protected void _init(boolean isEncrypt, byte[] iv) {
    String keyStr = new String(key.getEncoded());
    ParametersWithIV params = new ParametersWithIV(
            new KeyParameter(KeyHelper.generateKeyDigest(keyLength, keyStr)), iv
    );
    cipher.init(isEncrypt, params);
}
 
Example #30
Source File: RLPxConnectionFactory.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
private static EthereumIESEncryptionEngine forDecryption(
    SecretKey privateKey,
    PublicKey ephemeralPublicKey,
    Bytes iv,
    Bytes commonMac) {
  CipherParameters pubParam = new ECPublicKeyParameters(ephemeralPublicKey.asEcPoint(), CURVE);
  CipherParameters privParam = new ECPrivateKeyParameters(privateKey.bytes().toUnsignedBigInteger(), CURVE);

  BasicAgreement agreement = new ECDHBasicAgreement();
  agreement.init(privParam);
  byte[] agreementValue =
      BigIntegers.asUnsignedByteArray(agreement.getFieldSize(), agreement.calculateAgreement(pubParam));

  IESWithCipherParameters iesWithCipherParameters = new IESWithCipherParameters(new byte[0], new byte[0], 128, 128);

  EthereumIESEncryptionEngine.ECIESHandshakeKDFFunction kdf =
      new EthereumIESEncryptionEngine.ECIESHandshakeKDFFunction(1, new SHA256Digest());
  kdf.init(new KDFParameters(agreementValue, iesWithCipherParameters.getDerivationV()));
  EthereumIESEncryptionEngine engine = new EthereumIESEncryptionEngine(
      agreement,
      kdf,
      new HMac(new SHA256Digest()),
      commonMac.toArrayUnsafe(),
      new BufferedBlockCipher(new SICBlockCipher(new AESEngine())));
  ParametersWithIV cipherParameters = new ParametersWithIV(iesWithCipherParameters, iv.toArrayUnsafe());
  engine.init(false, privParam, pubParam, cipherParameters);
  return engine;
}