Example usage for org.bouncycastle.crypto.modes CBCBlockCipher CBCBlockCipher

List of usage examples for org.bouncycastle.crypto.modes CBCBlockCipher CBCBlockCipher

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.modes CBCBlockCipher CBCBlockCipher.

Prototype

public CBCBlockCipher(BlockCipher cipher) 

Source Link

Document

Basic constructor.

Usage

From source file:org.cryptacular.spec.BufferedBlockCipherSpec.java

License:Open Source License

/**
 * Creates a new buffered block cipher from the specification in this
 * instance.// w  w w .java2  s. co m
 *
 * @return  New buffered block cipher instance.
 */
@Override
public BufferedBlockCipher newInstance() {
    BlockCipher cipher = getBlockCipherSpec().newInstance();

    switch (mode) {

    case "CBC":
        cipher = new CBCBlockCipher(cipher);
        break;

    case "OFB":
        cipher = new OFBBlockCipher(cipher, cipher.getBlockSize());
        break;

    case "CFB":
        cipher = new CFBBlockCipher(cipher, cipher.getBlockSize());
        break;

    default:
        break;
    }

    if (padding != null) {
        return new PaddedBufferedBlockCipher(cipher, getPadding(padding));
    }
    return new BufferedBlockCipher(cipher);
}

From source file:org.cryptacular.util.CipherUtilTest.java

License:Open Source License

@DataProvider(name = "block-cipher")
public Object[][] getBlockCipherData() {
    return new Object[][] { new Object[] {
            // Plaintext is NOT multiple of block size
            "Able was I ere I saw elba.", new CBCBlockCipher(new AESEngine()), new RBGNonce(16), },
            // Plaintext is multiple of block size
            new Object[] { "Four score and seven years ago, our forefathers ",
                    new CBCBlockCipher(new BlowfishEngine()), new RBGNonce(8), },
            // OFB
            new Object[] { "Have you passed through this night?", new OFBBlockCipher(new BlowfishEngine(), 64),
                    new LongCounterNonce(), },
            // CFB
            new Object[] {
                    "I went to the woods because I wished to live deliberately, to "
                            + "front only the essential facts of life",
                    new CFBBlockCipher(new AESEngine(), 128), new RBGNonce(16), }, };
}

From source file:org.cryptacular.util.CipherUtilTest.java

License:Open Source License

@Test(dataProvider = "plaintext-files")
public void testBlockCipherEncryptDecryptStream(final String path) throws Exception {
    final BlockCipher cipher = new CBCBlockCipher(new AESEngine());
    final SecretKey key = SecretKeyGenerator.generate(cipher);
    final Nonce nonce = new CounterNonce("vt-crypt", 1);
    final File file = new File(path);
    final String expected = new String(StreamUtil.readAll(file));
    final ByteArrayOutputStream tempOut = new ByteArrayOutputStream();
    CipherUtil.encrypt(cipher, key, nonce, StreamUtil.makeStream(file), tempOut);

    final ByteArrayInputStream tempIn = new ByteArrayInputStream(tempOut.toByteArray());
    final ByteArrayOutputStream actual = new ByteArrayOutputStream();
    CipherUtil.decrypt(cipher, key, tempIn, actual);
    assertEquals(new String(actual.toByteArray()), expected);
}

From source file:org.debux.webmotion.server.BouncyCastleTest.java

License:Open Source License

@Test
public void testEncryptRijndael()
        throws DataLengthException, IllegalStateException, InvalidCipherTextException {
    BlockCipher engine = new RijndaelEngine(256);
    BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine),
            new ZeroBytePadding());

    byte[] keyBytes = "0123456789abcdef0123456789abcdef".getBytes();
    cipher.init(true, new KeyParameter(keyBytes));

    byte[] input = "value".getBytes();
    byte[] cipherText = new byte[cipher.getOutputSize(input.length)];

    int cipherLength = cipher.processBytes(input, 0, input.length, cipherText, 0);
    cipher.doFinal(cipherText, cipherLength);

    String result = new String(Base64.encode(cipherText));
    log.debug("result : " + result);
    AssertJUnit.assertNotNull(result);/*from  w  w  w.java 2s  .c o m*/
}

From source file:org.debux.webmotion.server.BouncyCastleTest.java

License:Open Source License

@Test
public void testDecryptRijndael()
        throws DataLengthException, IllegalStateException, InvalidCipherTextException {
    BlockCipher engine = new RijndaelEngine(256);
    BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine),
            new ZeroBytePadding());

    byte[] keyBytes = "0123456789abcdef0123456789abcdef".getBytes();
    cipher.init(false, new KeyParameter(keyBytes));

    byte[] output = Base64.decode("Ij7J7G33H5xE9K5vaTiEypPnjJPuDdZ0C9QyvcIj/ZI=".getBytes());
    byte[] cipherText = new byte[cipher.getOutputSize(output.length)];

    int cipherLength = cipher.processBytes(output, 0, output.length, cipherText, 0);
    int outputLength = cipher.doFinal(cipherText, cipherLength);
    outputLength += cipherLength;//  w w  w  . ja v  a2  s  .  c o m

    byte[] resultBytes = cipherText;
    if (outputLength != output.length) {
        resultBytes = new byte[outputLength];
        System.arraycopy(cipherText, 0, resultBytes, 0, outputLength);
    }

    String result = new String(resultBytes);
    log.debug("result : " + result);
    AssertJUnit.assertEquals("value", result);
}

From source file:org.fnppl.opensdx.security.SymmetricKey.java

License:Open Source License

public SymmetricKey(byte[] key_bytes, byte[] iv) {
    this.keyBytes = key_bytes;
    this.initVector = iv;

    CBCBlockCipher aesCBC = new CBCBlockCipher(new AESEngine());
    KeyParameter kp = new KeyParameter(keyBytes);
    ParametersWithIV aesCBCParams = new ParametersWithIV(kp, initVector);

    aesCipher = new PaddedBufferedBlockCipher(aesCBC, new PKCS7Padding());
    aesCipher.init(true, aesCBCParams);//from  ww  w . j a v  a 2 s.com

    blockSize = aesCipher.getBlockSize();
}

From source file:org.fnppl.opensdx.security.SymmetricKey.java

License:Open Source License

public void decrypt(InputStream in, OutputStream out) throws Exception {
    //      if(key.length!=initvector.length || key.length!=keybits/8) {
    //         throw new Exception("invalid params");
    //      }/*w w w.ja  va2s  .  co  m*/

    CBCBlockCipher aesCBC = new CBCBlockCipher(new AESEngine());

    KeyParameter kp = new KeyParameter(keyBytes);
    ParametersWithIV aesCBCParams = new ParametersWithIV(kp, initVector);

    PaddedBufferedBlockCipher aesCipher = new PaddedBufferedBlockCipher(aesCBC, new PKCS7Padding());

    aesCipher.init(false, aesCBCParams);
    // aesCipher.init(true, aesCBCParams); //TODO pad block corrupted error when false. WHY??

    int read = -1;
    byte[] buff = new byte[128 / 8];//blocksize
    while ((read = in.read(buff)) != -1) {
        byte[] ou = new byte[buff.length];
        //         System.err.println("read: "+read);

        int rg = aesCipher.processBytes(buff, 0, read, ou, 0);
        out.write(ou, 0, rg);
        //         System.err.println("rg: "+rg);
    }

    buff = new byte[2 * 128 / 8];//blocksize
    read = aesCipher.doFinal(buff, 0);
    out.write(buff, 0, read);
}

From source file:org.freenetproject.freemail.RTSFetcher.java

License:Open Source License

private byte[] decrypt_rts(File rtsmessage) throws IOException, InvalidCipherTextException {
    // initialise our ciphers
    RSAKeyParameters ourprivkey = AccountManager.getPrivateKey(account.getProps());
    AsymmetricBlockCipher deccipher = new RSAEngine();
    deccipher.init(false, ourprivkey);/* ww  w .  j a v a  2  s .com*/

    PaddedBufferedBlockCipher aescipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()),
            new PKCS7Padding());

    // first n bytes will be an encrypted RSA block containting the
    // AES IV and Key. Read that.
    byte[] encrypted_params = new byte[deccipher.getInputBlockSize()];
    int read = 0;
    FileInputStream fis = new FileInputStream(rtsmessage);
    try {
        while (read < encrypted_params.length) {
            read += fis.read(encrypted_params, read, encrypted_params.length - read);
            if (read < 0)
                break;
        }

        if (read < 0) {
            fis.close();
            throw new InvalidCipherTextException("RTS Message too short");
        }

        byte[] aes_iv_and_key = deccipher.processBlock(encrypted_params, 0, encrypted_params.length);

        KeyParameter kp = new KeyParameter(aes_iv_and_key, aescipher.getBlockSize(),
                aes_iv_and_key.length - aescipher.getBlockSize());
        ParametersWithIV kpiv = new ParametersWithIV(kp, aes_iv_and_key, 0, aescipher.getBlockSize());
        try {
            aescipher.init(false, kpiv);
        } catch (IllegalArgumentException iae) {
            fis.close();
            throw new InvalidCipherTextException(iae.getMessage());
        }

        byte[] plaintext = new byte[aescipher.getOutputSize((int) rtsmessage.length() - read)];

        int ptbytes = 0;
        while (read < rtsmessage.length()) {
            byte[] buf = new byte[(int) rtsmessage.length() - read];

            int thisread = fis.read(buf, 0, (int) rtsmessage.length() - read);
            ptbytes += aescipher.processBytes(buf, 0, thisread, plaintext, ptbytes);
            read += thisread;
        }

        try {
            aescipher.doFinal(plaintext, ptbytes);
        } catch (DataLengthException dle) {
            throw new InvalidCipherTextException(dle.getMessage());
        }

        return plaintext;
    } finally {
        fis.close();
    }
}

From source file:org.jdownloader.container.C.java

License:Open Source License

private String decryptCCF5(InputStream inputStream) throws Exception {
    final String[][] CCF50 = (String[][]) getClass().forName(new String(
            HexFormatter.hexToByteArray("6F72672E6A646F776E6C6F616465722E636F6E7461696E65722E436F6E666967"),
            "UTF-8")).getMethod("CCF50").invoke(null);
    final KeyParameter keyParam1 = new KeyParameter(HexFormatter.hexToByteArray(CCF50[0][0]));
    final CipherParameters cipherParams1 = new ParametersWithIV(keyParam1,
            HexFormatter.hexToByteArray(CCF50[0][1]));
    final BufferedBlockCipher cipher1 = new BufferedBlockCipher(new CBCBlockCipher(new RijndaelEngine()));
    cipher1.reset();/* w  w  w  .j av a2 s .com*/
    cipher1.init(false, cipherParams1);

    final KeyParameter keyParam11 = new KeyParameter(HexFormatter.hexToByteArray(CCF50[0][0]));
    final CipherParameters cipherParams11 = new ParametersWithIV(keyParam11,
            HexFormatter.hexToByteArray(CCF50[0][1]));
    final BufferedBlockCipher cipher11 = new BufferedBlockCipher(new CBCBlockCipher(new RijndaelEngine()));
    cipher11.reset();
    cipher11.init(false, cipherParams11);

    final KeyParameter keyParam2 = new KeyParameter(HexFormatter.hexToByteArray(CCF50[1][0]));
    final CipherParameters cipherParams2 = new ParametersWithIV(keyParam2,
            HexFormatter.hexToByteArray(CCF50[1][1]));
    final BufferedBlockCipher cipher2 = new BufferedBlockCipher(new CBCBlockCipher(new RijndaelEngine()));
    cipher2.reset();
    cipher2.init(false, cipherParams2);

    final InputStream is = new CipherInputStream(
            new CipherInputStream(new CipherInputStream(inputStream, cipher11), cipher2), cipher1);
    String d = new String(IO.readBytes(is), "UTF-8");
    return d;
}

From source file:org.jdownloader.container.C.java

License:Open Source License

private String decryptCCF07_10(InputStream inputStream, String key, String iv) throws Exception {
    final KeyParameter keyParam1 = new KeyParameter(HexFormatter.hexToByteArray(key));
    final CipherParameters cipherParams1 = new ParametersWithIV(keyParam1, HexFormatter.hexToByteArray(iv));
    final BufferedBlockCipher cipher1 = new BufferedBlockCipher(new CBCBlockCipher(new RijndaelEngine()));
    cipher1.reset();//from ww w  . ja v a  2 s  .co  m
    cipher1.init(false, cipherParams1);

    final InputStream is = new CipherInputStream(inputStream, cipher1);
    String d = new String(IO.readBytes(is), "UTF-8");
    return d;
}