Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
E
EJB-EncryptionBean
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Model registry
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
web2
EJB-EncryptionBean
Commits
36fc8cea
Commit
36fc8cea
authored
5 years ago
by
Carlos Eduardo da Silva
Browse files
Options
Downloads
Patches
Plain Diff
Fixing @PostConstruct method.
Fixed the @PostConstruct init method for not triggering checked exception.
parent
cf94c50a
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Encryption-Bean/src/main/java/br/ufrn/imd/web2/encryption/EncryptionBean.java
+46
-7
46 additions, 7 deletions
...main/java/br/ufrn/imd/web2/encryption/EncryptionBean.java
with
46 additions
and
7 deletions
Encryption-Bean/src/main/java/br/ufrn/imd/web2/encryption/EncryptionBean.java
+
46
−
7
View file @
36fc8cea
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
Runtime
Exception
* 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
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment