Java tutorial
//package com.java2s; //License from project: Open Source License import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.Key; import java.security.spec.AlgorithmParameterSpec; import javax.crypto.Cipher; import javax.crypto.spec.PBEParameterSpec; public class Main { /** * Initializes the Cipher for use. */ public static void initCipher(Cipher cipher, int mode, Key secretKey) { initCipher(cipher, mode, secretKey, null); } /** * Initializes the Cipher for use. */ public static void initCipher(Cipher cipher, int mode, Key secretKey, byte[] salt, int iterationCount) { initCipher(cipher, mode, secretKey, new PBEParameterSpec(salt, iterationCount)); } /** * Initializes the Cipher for use. */ public static void initCipher(Cipher cipher, int mode, Key secretKey, AlgorithmParameterSpec parameterSpec) { try { if (parameterSpec != null) { cipher.init(mode, secretKey, parameterSpec); } else { cipher.init(mode, secretKey); } } catch (InvalidKeyException e) { throw new IllegalArgumentException("Unable to initialize due to invalid secret key", e); } catch (InvalidAlgorithmParameterException e) { throw new IllegalStateException("Unable to initialize due to invalid decryption parameter spec", e); } } }