Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • web2/EJB-EncryptionBean
  • wilsonsf/EJB-EncryptionBean
2 results
Show changes
Commits on Source (7)
Showing with 120 additions and 58 deletions
File deleted
......@@ -15,6 +15,7 @@
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
......@@ -23,12 +24,18 @@
<attribute name="org.eclipse.jst.component.nondependency" value=""/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.jboss.ide.eclipse.as.core.server.runtime.runtimeTarget/WildFly 14.0 Runtime">
<attributes>
<attribute name="owner.project.facets" value="jst.ejb"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
......
......@@ -5,4 +5,5 @@ org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<runtime name="WildFly 12.0 Runtime"/>
<runtime name="WildFly 14.0 Runtime"/>
<fixed facet="jst.ejb"/>
<fixed facet="java"/>
<installed facet="java" version="1.8"/>
......
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>br.ufrn.imd.web2</groupId>
<artifactId>Encryption-Bean</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>ejb</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-ejb-plugin</artifactId>
<version>2.5</version>
<configuration>
<ejbVersion>3.2</ejbVersion>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>javax.ejb-api</artifactId>
<version>3.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
</dependencies>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>br.ufrn.imd.web2</groupId>
<artifactId>Encryption-Bean</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>ejb</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-ejb-plugin</artifactId>
<version>2.5</version>
<configuration>
<ejbVersion>3.2</ejbVersion>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/javax/javaee-api -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
</dependency>
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>javax.ejb-api</artifactId>
<version>3.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
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;
......@@ -27,7 +31,6 @@ import org.apache.commons.codec.binary.Base64;
import br.ufrn.imd.web2.encryption.EncryptionException;
import br.ufrn.imd.web2.encryption.EncryptionLocal;
import br.ufrn.imd.web2.encryption.EncryptionRemote;
//import br.ufrn.imd.web2.encryption.org;
@Stateless(name = EncryptionBean.EJB_NAME)
@Local(EncryptionLocal.class)
......@@ -43,7 +46,7 @@ public class EncryptionBean implements EncryptionLocal, EncryptionRemote {
* Name we'll assign to this EJB, will be referenced in the corresponding
* META-INF/ejb-jar.xml file
*/
static final String EJB_NAME = "EncryptionEJB";
static final String EJB_NAME = "EncryptionBean";
/**
* Name of the environment entry representing the ciphers' passphrase supplied
......@@ -142,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");
......@@ -163,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);
......
......@@ -8,7 +8,7 @@
<enterprise-beans>
<session>
<ejb-name>EncryptionBean</ejb-name>
<env-entry>
<env-entry-name>ciphersPassphrase</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
......
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<runtime name="WildFly 12.0 Runtime"/>
<runtime name="WildFly 14.0 Runtime"/>
<fixed facet="jst.ear"/>
<installed facet="jst.ear" version="7.0"/>
</faceted-project>
......@@ -21,6 +21,12 @@
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.jboss.ide.eclipse.as.core.server.runtime.runtimeTarget/WildFly 14.0 Runtime">
<attributes>
<attribute name="owner.project.facets" value="jst.web"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
......
......@@ -5,4 +5,5 @@ org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<runtime name="WildFly 12.0 Runtime"/>
<runtime name="WildFly 14.0 Runtime"/>
<fixed facet="jst.web"/>
<fixed facet="java"/>
<fixed facet="wst.jsdt.web"/>
......