Example usage for org.bouncycastle.crypto.modes SICBlockCipher getBlockSize

List of usage examples for org.bouncycastle.crypto.modes SICBlockCipher getBlockSize

Introduction

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

Prototype

public int getBlockSize() 

Source Link

Usage

From source file:org.bunkr.core.crypto.CipherBuilder.java

License:Open Source License

/**
 * Build a block cipher for encrypting or decrypting the target file.
 *
 * Intelligently create the correct cipher and initialize it correctly from the encryptionData present on the
 * target file. If the goal is encryption, the encryption data for the file is reinitialized from random.
 *
 * @param file the target FileInventoryItem
 * @param encrypting boolean indicating encryption (true) or decryption (false)
 * @return a BlockCipher// ww  w .j av a 2  s .c om
 */
public static BlockCipher buildCipherForFile(FileInventoryItem file, boolean encrypting) {
    Encryption alg = file.getEncryptionAlgorithm();

    if (alg.c.equals(Algorithms.SYMMETRIC_CIPHER.AES)) {
        if (alg.m.equals(Algorithms.SYMMETRIC_MODE.CTR)) {
            SICBlockCipher fileCipher = new SICBlockCipher(new AESEngine());
            byte[] edata = file.getEncryptionData();
            if (encrypting) {
                edata = new byte[alg.keyByteLength + fileCipher.getBlockSize()];
                RandomMaker.fill(edata);
                file.setEncryptionData(edata);
            }
            byte[] ekey = Arrays.copyOfRange(edata, 0, alg.keyByteLength);
            byte[] eiv = Arrays.copyOfRange(edata, alg.keyByteLength,
                    alg.keyByteLength + fileCipher.getBlockSize());
            fileCipher.init(encrypting, new ParametersWithIV(new KeyParameter(ekey), eiv));
            return fileCipher;
        }
    }

    else if (alg.c.equals(Algorithms.SYMMETRIC_CIPHER.TWOFISH)) {
        if (alg.m.equals(Algorithms.SYMMETRIC_MODE.CTR)) {
            SICBlockCipher fileCipher = new SICBlockCipher(new TwofishEngine());
            byte[] edata = file.getEncryptionData();
            if (encrypting) {
                edata = new byte[alg.keyByteLength + fileCipher.getBlockSize()];
                RandomMaker.fill(edata);
                file.setEncryptionData(edata);
            }
            byte[] ekey = Arrays.copyOfRange(edata, 0, alg.keyByteLength);
            byte[] eiv = Arrays.copyOfRange(edata, alg.keyByteLength,
                    alg.keyByteLength + fileCipher.getBlockSize());
            fileCipher.init(encrypting, new ParametersWithIV(new KeyParameter(ekey), eiv));
            return fileCipher;
        }
    }

    throw new IllegalArgumentException(String.format("Unsupported algorithm: %s", alg));
}

From source file:org.bunkr.core.crypto.CipherBuilder.java

License:Open Source License

/**
 * Simple version of buildCipherForFile, this time without the encryption data manipulation or file object.
 *
 * @param alg the encryption algorithm being used
 * @param key encryption key data bytes/*from  w ww  .  jav  a2  s .c  om*/
 * @param iv initialization vector data bytes
 * @return a BlockCipher
 */
public static BlockCipher buildCipher(Encryption alg, boolean encrypting, byte[] key, byte[] iv) {
    if (key.length != alg.keyByteLength)
        throw new IllegalArgumentException(String.format("Supplied key length %s != required key length %s",
                key.length, alg.keyByteLength));
    if (alg.c.equals(Algorithms.SYMMETRIC_CIPHER.AES)) {
        SICBlockCipher fileCipher = new SICBlockCipher(new AESEngine());
        if (iv.length != fileCipher.getBlockSize())
            throw new IllegalArgumentException(String.format("Supplied iv length %s != required iv length %s",
                    iv.length, fileCipher.getBlockSize()));
        if (alg.m.equals(Algorithms.SYMMETRIC_MODE.CTR)) {
            fileCipher.init(encrypting, new ParametersWithIV(new KeyParameter(key), iv));
            return fileCipher;
        }
    }

    else if (alg.c.equals(Algorithms.SYMMETRIC_CIPHER.TWOFISH)) {
        SICBlockCipher fileCipher = new SICBlockCipher(new TwofishEngine());
        if (iv.length != fileCipher.getBlockSize())
            throw new IllegalArgumentException(String.format("Supplied iv length %s != required iv length %s",
                    iv.length, fileCipher.getBlockSize()));
        if (alg.m.equals(Algorithms.SYMMETRIC_MODE.CTR)) {
            fileCipher.init(encrypting, new ParametersWithIV(new KeyParameter(key), iv));
            return fileCipher;
        }
    }

    throw new IllegalArgumentException(String.format("Unsupported algorithm: %s", alg));
}