if (padding == null) padding = "NoPadding";
try {
// Ensure the JCE policies files allow for this sized key
if (Cipher.getMaxAllowedKeyLength(cipherAlgorithm.jceId) < keySizeInBytes*8) {
throw new EncryptedDocumentException("Export Restrictions in place - please install JCE Unlimited Strength Jurisdiction Policy files");
}
Cipher cipher;
if (cipherAlgorithm == CipherAlgorithm.rc4) {
cipher = Cipher.getInstance(cipherAlgorithm.jceId);
} else if (cipherAlgorithm.needsBouncyCastle) {
registerBouncyCastle();
cipher = Cipher.getInstance(cipherAlgorithm.jceId + "/" + chain.jceId + "/" + padding, "BC");
} else {
cipher = Cipher.getInstance(cipherAlgorithm.jceId + "/" + chain.jceId + "/" + padding);
}
if (vec == null) {
cipher.init(cipherMode, key);
} else {
AlgorithmParameterSpec aps;
if (cipherAlgorithm == CipherAlgorithm.rc2) {
aps = new RC2ParameterSpec(key.getEncoded().length*8, vec);
} else {
aps = new IvParameterSpec(vec);
}
cipher.init(cipherMode, key, aps);
}
return cipher;
} catch (GeneralSecurityException e) {
throw new EncryptedDocumentException(e);
}
}