I want to create an utility object that has a specified secret key and cipher algorithm/transformation, to make it easier to do enc/decryption with that key, and I would also like to specify the size of the IVs to be used in the constructor, but I want it to fail upon initialization if the given size is invalid, to avoid having to deal with exceptions every time and just assume they won’t happen later.
One way to know this would be, in the constructor, to simply initialize the cipher with an IV of the given size and throw the exception on failure:
public SecretCrypt(SecretKey key, String cipherAlgorithm, int ivSize) throws InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException {
this.key = key;
Cipher.getInstance(cipherAlgorithm).init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(new byte[ivSize]));
this.cipherAlgorithm = cipherAlgorithm;
this.ivSize = ivSize;
}
but (I honestly don’t know but) this seems more expensive than it could be, and I expect this class to be instantiated somewhat frequently, and would like a cheaper approach.