try {
// Password cannot be null.
if (this.password == null) {
throw new EncryptionInitializationException(
"Password not set for Password Based Encryptor");
}
// Normalize password to NFC form
final char[] normalizedPassword = Normalizer.normalizeToNfc(this.password);
/*
* Encryption and decryption Ciphers are created the usual way.
*/
PBEKeySpec pbeKeySpec = new PBEKeySpec(normalizedPassword);
// We don't need the char[] passwords anymore -> clean!
cleanPassword(this.password);
cleanPassword(normalizedPassword);
if (this.provider != null) {
SecretKeyFactory factory =
SecretKeyFactory.getInstance(
this.algorithm,
this.provider);
this.key = factory.generateSecret(pbeKeySpec);
this.encryptCipher =
Cipher.getInstance(this.algorithm, this.provider);
this.decryptCipher =
Cipher.getInstance(this.algorithm, this.provider);
} else if (this.providerName != null) {
SecretKeyFactory factory =
SecretKeyFactory.getInstance(
this.algorithm,
this.providerName);
this.key = factory.generateSecret(pbeKeySpec);
this.encryptCipher =
Cipher.getInstance(this.algorithm, this.providerName);
this.decryptCipher =
Cipher.getInstance(this.algorithm, this.providerName);
} else {
SecretKeyFactory factory =
SecretKeyFactory.getInstance(this.algorithm);
this.key = factory.generateSecret(pbeKeySpec);
this.encryptCipher = Cipher.getInstance(this.algorithm);
this.decryptCipher = Cipher.getInstance(this.algorithm);
}
} catch (EncryptionInitializationException e) {
throw e;
} catch (Throwable t) {
throw new EncryptionInitializationException(t);
}
// The salt size for the chosen algorithm is set to be equal
// to the algorithm's block size (if it is a block algorithm).