/** * Gets the IV being used. Only works with algorithms that support IVs. * @return Returns the iv as a IvParameterSpec */ public IvParameterSpec getIV(){ if(!type.hasIV()){ throw new UnsupportedTypeException(type); } return iv; } }
/** * Changes the current iv to the provided iv and initializes the cipher instances with * the new iv. Only works with algorithms that support IVs, not RijndaelPCFB. * @param iv The new iv to use as IvParameterSpec * @throws InvalidAlgorithmParameterException */ public void setIV(IvParameterSpec iv) throws InvalidAlgorithmParameterException{ if(!type.hasIV()){ throw new UnsupportedTypeException(type); } this.iv = iv; try { encryptCipher.init(Cipher.ENCRYPT_MODE, this.key, this.iv); decryptCipher.init(Cipher.DECRYPT_MODE, this.key, this.iv); } catch (InvalidKeyException e) { throw new IllegalArgumentException(e); } }
/** * Generates a new IV to be used and initializes the cipher instances with * the new iv. Only works with algorithms that support IVs, not RijndaelPCFB. * @return The generated IV */ public IvParameterSpec genIV(){ if(!type.hasIV()){ throw new UnsupportedTypeException(type); } this.iv = KeyGenUtils.genIV(type.ivSize); try { encryptCipher.init(Cipher.ENCRYPT_MODE, this.key, this.iv); decryptCipher.init(Cipher.DECRYPT_MODE, this.key, this.iv); } catch (InvalidKeyException e) { throw new IllegalArgumentException(e); // Definitely a bug ... } catch (InvalidAlgorithmParameterException e) { throw new IllegalArgumentException(e); // Definitely a bug ... } return iv; }