Code example for IvParameterSpec

0
        String ciphertext = "";
 
        SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
 
        Cipher cipher = Cipher.getInstance(CRYPTO_SPEC);
        IvParameterSpec ivSpec = new IvParameterSpec(iv);
        cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
 
        byte[] ciphertextBytes = cipher.doFinal(cleartext.getBytes());
        ciphertext = new String(Base64.encode(ciphertextBytes, Base64.DEFAULT));
 
        return ciphertext;
    } 
 
    /** 
     * Encrypts the provided byte array. 
     * @param cleartext The text to be encrypted. 
     * @param key The AES key to use for encrypting. 
     * @param iv The initialisation vector to use for encrypting. 
     * @return A byte array representing the encrypted text. 
     * @throws InvalidKeyException if the given key can't be used