Example usage for org.bouncycastle.crypto.engines AESEngine getBlockSize

List of usage examples for org.bouncycastle.crypto.engines AESEngine getBlockSize

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.engines AESEngine getBlockSize.

Prototype

public int getBlockSize() 

Source Link

Usage

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

License:Open Source License

@Test // big packet encryption
public void test12() throws Throwable {

    AESEngine engine = new AESEngine();
    SICBlockCipher ctrEngine = new SICBlockCipher(engine);

    byte[] keyBytes = Hex.decode("a4627abc2a3c25315bff732cb22bc128f203912dd2a840f31e66efb27a47d2b1");
    byte[] ivBytes = new byte[16];
    byte[] payload = Hex.decode(
            "0109efc76519b683d543db9d0991bcde99cc9a3d14b1d0ecb8e9f1f66f31558593d746eaa112891b04ef7126e1dce17c9ac92ebf39e010f0028b8ec699f56f5d0c0d00");
    byte[] cipherText = Hex.decode(
            "f9fab4e9dd9fc3e5d0d0d16da254a2ac24df81c076e3214e2c57da80a46e6ae4752f4b547889fa692b0997d74f36bb7c047100ba71045cb72cfafcc7f9a251762cdf8f");

    KeyParameter key = new KeyParameter(keyBytes);
    ParametersWithIV params = new ParametersWithIV(key, ivBytes);

    ctrEngine.init(true, params);//ww  w  .  j  ava  2s .  co  m

    byte[] in = payload;
    byte[] out = new byte[in.length];

    int i = 0;

    while (i < in.length) {
        ctrEngine.processBlock(in, i, out, i);
        i += engine.getBlockSize();
        if (in.length - i < engine.getBlockSize())
            break;
    }

    // process left bytes
    if (in.length - i > 0) {
        byte[] tmpBlock = new byte[16];
        System.arraycopy(in, i, tmpBlock, 0, in.length - i);
        ctrEngine.processBlock(tmpBlock, 0, tmpBlock, 0);
        System.arraycopy(tmpBlock, 0, out, i, in.length - i);
    }

    log.info("cipher: {}", Hex.toHexString(out));

    assertEquals(Hex.toHexString(cipherText), Hex.toHexString(out));
}

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

License:Open Source License

/**
 * Decrypt cipher by AES in SIC(also know as CTR) mode
 *
 * @param cipher -proper cipher/*from w  w  w .j av  a  2s.c o  m*/
 * @return decrypted cipher, equal length to the cipher.
 */
public byte[] decryptAES(byte[] cipher) {

    if (priv == null) {
        throw new MissingPrivateKeyException();
    }

    AESEngine engine = new AESEngine();
    SICBlockCipher ctrEngine = new SICBlockCipher(engine);

    KeyParameter key = new KeyParameter(BigIntegers.asUnsignedByteArray(priv));
    ParametersWithIV params = new ParametersWithIV(key, new byte[16]);

    ctrEngine.init(false, params);

    int i = 0;
    byte[] out = new byte[cipher.length];
    while (i < cipher.length) {
        ctrEngine.processBlock(cipher, i, out, i);
        i += engine.getBlockSize();
        if (cipher.length - i < engine.getBlockSize()) {
            break;
        }
    }

    // process left bytes
    if (cipher.length - i > 0) {
        byte[] tmpBlock = new byte[16];
        System.arraycopy(cipher, i, tmpBlock, 0, cipher.length - i);
        ctrEngine.processBlock(tmpBlock, 0, tmpBlock, 0);
        System.arraycopy(tmpBlock, 0, out, i, cipher.length - i);
    }

    return out;
}

From source file:org.ethereum.net.rlpx.FrameCodec.java

License:Open Source License

public FrameCodec(EncryptionHandshake.Secrets secrets) {
    this.mac = secrets.mac;
    AESEngine encCipher = new AESEngine();
    enc = new SICBlockCipher(encCipher);
    enc.init(true, new ParametersWithIV(new KeyParameter(secrets.aes), new byte[encCipher.getBlockSize()]));
    AESEngine decCipher = new AESEngine();
    dec = new SICBlockCipher(decCipher);
    dec.init(false, new ParametersWithIV(new KeyParameter(secrets.aes), new byte[decCipher.getBlockSize()]));
    egressMac = secrets.egressMac;// w ww.j av a2  s .  co  m
    ingressMac = secrets.ingressMac;
}