Skip to content
Snippets Groups Projects
Commit 36fc8cea authored by Carlos Eduardo da Silva's avatar Carlos Eduardo da Silva
Browse files

Fixing @PostConstruct method.

Fixed the @PostConstruct init method for not triggering checked exception.
parent cf94c50a
No related branches found
No related tags found
No related merge requests found
package br.ufrn.imd.web2.encryption;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.util.concurrent.Future;
import java.util.logging.Logger;
......@@ -11,6 +14,7 @@ import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
......@@ -141,11 +145,11 @@ public class EncryptionBean implements EncryptionLocal, EncryptionRemote {
/**
* Initializes this service before it may handle requests
*
* @throws Exception
* @throws RuntimeException
* If some unexpected error occurred
*/
@PostConstruct
public void initialize() throws Exception {
public void initialize() {
// Log that we're here
log.info("Initializing, part of " + PostConstruct.class.getName() + " lifecycle");
......@@ -162,14 +166,49 @@ public class EncryptionBean implements EncryptionLocal, EncryptionRemote {
// Obtain key and param spec for the ciphers
final KeySpec ciphersKeySpec = new PBEKeySpec(ciphersPassphrase.toCharArray(), ciphersSalt,
ciphersIterationCount);
final SecretKey ciphersKey = SecretKeyFactory.getInstance(cipherAlgorithm).generateSecret(ciphersKeySpec);
SecretKey ciphersKey;
try {
ciphersKey = SecretKeyFactory.getInstance(cipherAlgorithm).generateSecret(ciphersKeySpec);
} catch (InvalidKeySpecException e1) {
throw new RuntimeException("Could not obtain the " + KeySpec.class.getSimpleName()
+ " for algorithm: " + cipherAlgorithm, e1);
} catch (NoSuchAlgorithmException e1) {
throw new RuntimeException("Could not obtain the " + KeySpec.class.getSimpleName()
+ " for algorithm: " + cipherAlgorithm, e1);
}
final AlgorithmParameterSpec paramSpec = new PBEParameterSpec(ciphersSalt, ciphersIterationCount);
// Create and init the ciphers
this.encryptionCipher = Cipher.getInstance(ciphersKey.getAlgorithm());
this.decryptionCipher = Cipher.getInstance(ciphersKey.getAlgorithm());
encryptionCipher.init(Cipher.ENCRYPT_MODE, ciphersKey, paramSpec);
decryptionCipher.init(Cipher.DECRYPT_MODE, ciphersKey, paramSpec);
try {
this.encryptionCipher = Cipher.getInstance(ciphersKey.getAlgorithm());
this.decryptionCipher = Cipher.getInstance(ciphersKey.getAlgorithm());
} catch (NoSuchAlgorithmException e1) {
throw new RuntimeException("Could not obtain the " + Cipher.class.getSimpleName()
+ " for algorithm: " + ciphersKey.getAlgorithm(), e1);
} catch (NoSuchPaddingException e1) {
throw new RuntimeException("Could not obtain the " + Cipher.class.getSimpleName()
+ " for algorithm: " + ciphersKey.getAlgorithm() + "due to unavailable padding scheme", e1);
}
try {
encryptionCipher.init(Cipher.ENCRYPT_MODE, ciphersKey, paramSpec);
} catch (InvalidKeyException e1) {
throw new RuntimeException("Could not initialize the encryption cipher " + Cipher.class.getSimpleName()
+ " due to invalid encryption key for algorithm: " + ciphersKey.getAlgorithm(), e1);
} catch (InvalidAlgorithmParameterException e1) {
throw new RuntimeException("Could not initialize the " + Cipher.class.getSimpleName()
+ " due to invalid algorithm: " + ciphersKey.getAlgorithm(), e1);
}
try {
decryptionCipher.init(Cipher.DECRYPT_MODE, ciphersKey, paramSpec);
} catch (InvalidKeyException e1) {
throw new RuntimeException("Could not initialize the decription cipher " + Cipher.class.getSimpleName()
+ " due to invalid encryption key for algorithm: " + ciphersKey.getAlgorithm(), e1);
} catch (InvalidAlgorithmParameterException e1) {
throw new RuntimeException("Could not initialize the decription cipher " + Cipher.class.getSimpleName()
+ " due to invalid algorithm: " + ciphersKey.getAlgorithm(), e1);
}
// Log
log.info("Initialized encryption cipher: " + this.encryptionCipher);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment