public void init(boolean forEncryption, CipherParameters params)
        throws IllegalArgumentException {

        if (params instanceof ParametersForThreefish){
            ParametersForThreefish pft = (ParametersForThreefish)params;

            stateSize = pft.getStateSize();
            setCipher(stateSize);
            if (cipher == null)
                throw new IllegalArgumentException("Threefish: unsupported state size: " + stateSize);

            byte[] key = ((KeyParameter)pft.getParameters()).getKey();
            if (key.length != (stateSize / 8))
                throw new IllegalArgumentException("Threefish: key length does not match state size: " + key.length);

            long[] tweak = pft.getTweak();
            if (tweak == null)
                throw new IllegalArgumentException("Threefish: tweak data not set");
            cipher.setTweak(tweak);
            
            // Get a long array for cipher key and moves the byte key buffer to it
            long[] keyLong = new long[stateSize / 64];
            for (int i = 0; i < keyLong.length; i++)
                keyLong[i] = ByteLong.GetUInt64(key, i * 8);

            cipher.setKey(keyLong);

            this.forEncryption = forEncryption;
            
            // Allocate buffers
            cipherIn = new long[stateSize / 64];
            cipherOut = new long[stateSize / 64];
            
            return;
        }
        throw new IllegalArgumentException("Threfish: invalid parameter passed to init - " + params.getClass().getName());
    }