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

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

Introduction

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

Prototype

public int getBlockSize() 

Source Link

Usage

From source file:snodes.net.Packet.java

License:Open Source License

/**
 * Encrypts the packet using the given key.
 *
 * @param bytes// w w  w.  j a  v  a2  s  .  c o m
 *     The packet data.
 * @param key
 *     The packet key.
 * @return
 *     The encrypted packet data.
 * @throws IllegalArgumentException
 *     If <tt>key</tt> is too short.
 */
private static byte[] encrypt(byte[] bytes, Key key) throws IllegalArgumentException {
    logger.fine("Encrypting packet with key " + key);

    KeyParameter fishkey = new KeyParameter(key.toByteArray());
    TwofishEngine twofish = new TwofishEngine();
    int blocksize = twofish.getBlockSize();
    int blocks = (int) Math.ceil((double) bytes.length / (double) blocksize);

    twofish.init(true, fishkey); // true = encrypt
    byte[] encrypted = new byte[blocks * blocksize]; // Leave room for padding

    for (int i = 0; i < blocks; i++) {
        byte[] block = new byte[blocksize];
        int offset = i * blocksize;
        int written = 0;

        // Pad if not enough bytes -- otherwise, exception is thrown
        if (bytes.length - offset < blocksize) {
            BlockCipherPadding pad = new PKCS7Padding();
            int padLen = bytes.length - offset;
            byte[] write = new byte[blocksize];
            int padding = 0;

            System.arraycopy(bytes, offset, write, 0, padLen);

            pad.init(new SecureRandom());
            padding = pad.addPadding(write, padLen);

            written = twofish.processBlock(write, 0, block, 0);
        } else {
            written = twofish.processBlock(bytes, offset, block, 0);
        }

        System.arraycopy(block, 0, encrypted, offset, written);
    }

    return encrypted;
}

From source file:snodes.net.Packet.java

License:Open Source License

/**
 * Decrypts the packet using the given key.
 *
 * @param bytes/* w  w w  .  ja v  a  2  s  .  c  om*/
 *     The packet data.
 * @param len
 *     The length of the data array.
 * @param key
 *     The key previously used to encrypt the data.
 * @return
 *     The decrypted packet data.
 */
private static byte[] decrypt(byte[] bytes, int len, Key key) {
    logger.finest("Initial decrypt size: " + len + " bytes");

    KeyParameter fishkey = new KeyParameter(key.toByteArray());
    TwofishEngine twofish = new TwofishEngine();
    byte[] decrypted = new byte[len];
    int blocksize = twofish.getBlockSize();
    int blocks = (int) Math.ceil((double) len / (double) blocksize);

    assert len % blocksize == 0 : "len is not a multiple of blocksize";
    twofish.init(false, fishkey); // false = decrypt

    //logger.finest("Decrypting " + len + " bytes (" + blocks + " blocks)");
    for (int i = 0; i < blocks; i++) {
        byte[] block = new byte[blocksize];
        int offset = i * blocksize;
        twofish.processBlock(bytes, offset, block, 0);

        System.arraycopy(block, 0, decrypted, offset, blocksize);
    }

    return decrypted;
}