}
if (this.saltGenerator.includePlainSaltInEncryptionResults()) {
// Check that the received message is bigger than the salt
if (encryptedMessage.length <= this.saltSizeBytes) {
throw new EncryptionOperationNotPossibleException();
}
}
try {
// If we are using a salt generator which specifies the salt
// to be included into the encrypted message itself, get it from
// there. If not, the salt is supposed to be fixed and thus the
// salt generator can be safely asked for it again.
byte[] salt = null;
if (this.saltGenerator.includePlainSaltInEncryptionResults()) {
salt = ArrayUtils.subarray(
encryptedMessage, 0, this.saltSizeBytes);
} else {
salt = this.saltGenerator.generateSalt(this.saltSizeBytes);
}
/*
* Perform decryption using the Cipher
*/
PBEParameterSpec parameterSpec =
new PBEParameterSpec(salt, this.keyObtentionIterations);
byte[] decryptedMessage = null;
// If we are using a salt generator which specifies the salt
// to be included into the encrypted message itself, we need to
// extract the part of the encrypted message which really belongs
// to the encryption result, and not the prepended salt.
byte[] encryptedMessageKernel = null;
if (this.saltGenerator.includePlainSaltInEncryptionResults()) {
encryptedMessageKernel =
ArrayUtils.subarray(encryptedMessage, this.saltSizeBytes,
encryptedMessage.length);
} else {
encryptedMessageKernel = encryptedMessage;
}
synchronized (this.decryptCipher) {
this.decryptCipher.init(
Cipher.DECRYPT_MODE, this.key, parameterSpec);
decryptedMessage =
this.decryptCipher.doFinal(encryptedMessageKernel);
}
// Return the results
return decryptedMessage;
} catch (InvalidKeyException e) {
// The problem could be not having the unlimited strength policies
// installed, so better give a usefull error message.
handleInvalidKeyException(e);
throw new EncryptionOperationNotPossibleException();
} catch (Exception e) {
// If decryption fails, it is more secure not to return any
// information about the cause in nested exceptions. Simply fail.
throw new EncryptionOperationNotPossibleException();
}
}