Skip to content
Snippets Groups Projects
Commit bedcfb11 authored by Christian Wulf's avatar Christian Wulf
Browse files

refactored CipherStage

parent 9f7e2f58
No related branches found
No related tags found
No related merge requests found
#FindBugs User Preferences
#Tue Apr 21 16:50:39 CEST 2015
#Wed Apr 22 08:29:19 CEST 2015
detector_threshold=3
effort=max
excludefilter0=.fbExcludeFilterFile|true
......
......@@ -20,7 +20,9 @@ import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
......@@ -30,19 +32,20 @@ import javax.crypto.spec.SecretKeySpec;
import teetime.framework.AbstractConsumerStage;
import teetime.framework.OutputPort;
public final class CipherByteArray extends AbstractConsumerStage<byte[]> {
public final class CipherStage extends AbstractConsumerStage<byte[]> {
private final OutputPort<byte[]> outputPort = this.createOutputPort();
private Cipher cipher = null;
private Cipher cipher;
public enum CipherMode {
ENCRYPT, DECRYPT
}
public CipherByteArray(final String password, final CipherMode mode) {
final byte[] salt = { 't', 'e', 's', 't' };
SecretKeySpec skeyspec = null;
public CipherStage(final String password, final CipherMode mode) {
this(password, mode, new byte[] { 't', 'e', 's', 't' });
}
public CipherStage(final String password, final CipherMode mode, final byte[] salt) {
final KeySpec keySpec = new PBEKeySpec(password.toCharArray(),
salt,
1024, 128);
......@@ -58,7 +61,7 @@ public final class CipherByteArray extends AbstractConsumerStage<byte[]> {
throw new IllegalStateException(e1);
}
skeyspec = new SecretKeySpec(secretKey.getEncoded(), "AES");
final SecretKeySpec skeyspec = new SecretKeySpec(secretKey.getEncoded(), "AES");
try {
this.cipher = Cipher.getInstance(skeyspec.getAlgorithm());
......@@ -68,12 +71,9 @@ public final class CipherByteArray extends AbstractConsumerStage<byte[]> {
throw new IllegalStateException(e);
}
final int convertedMode = (mode == CipherMode.ENCRYPT) ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE;
try {
if (mode == CipherMode.ENCRYPT) {
this.cipher.init(Cipher.ENCRYPT_MODE, skeyspec);
} else {
this.cipher.init(Cipher.DECRYPT_MODE, skeyspec);
}
this.cipher.init(convertedMode, skeyspec);
} catch (final InvalidKeyException e) {
throw new IllegalStateException(e);
}
......@@ -81,16 +81,14 @@ public final class CipherByteArray extends AbstractConsumerStage<byte[]> {
@Override
protected void execute(final byte[] element) {
byte[] output = null;
try {
output = this.cipher.doFinal(element);
} catch (final Exception e) {
e.printStackTrace();
byte[] output = this.cipher.doFinal(element);
this.outputPort.send(output);
} catch (IllegalBlockSizeException e) {
throw new IllegalStateException(e);
} catch (BadPaddingException e) {
throw new IllegalStateException(e);
}
this.outputPort.send(output);
}
public OutputPort<byte[]> getOutputPort() {
......
......@@ -18,8 +18,8 @@ package teetime.examples.cipher;
import java.io.File;
import teetime.framework.AnalysisConfiguration;
import teetime.stage.CipherByteArray;
import teetime.stage.CipherByteArray.CipherMode;
import teetime.stage.CipherStage;
import teetime.stage.CipherStage.CipherMode;
import teetime.stage.InitialElementProducer;
import teetime.stage.ZipByteArray;
import teetime.stage.ZipByteArray.ZipMode;
......@@ -34,10 +34,10 @@ public class CipherConfiguration extends AnalysisConfiguration {
final InitialElementProducer<File> init = new InitialElementProducer<File>(input);
final File2ByteArray f2b = new File2ByteArray();
final CipherByteArray enc = new CipherByteArray(password, CipherMode.ENCRYPT);
final CipherStage enc = new CipherStage(password, CipherMode.ENCRYPT);
final ZipByteArray comp = new ZipByteArray(ZipMode.COMP);
final ZipByteArray decomp = new ZipByteArray(ZipMode.DECOMP);
final CipherByteArray decrypt = new CipherByteArray(password, CipherMode.DECRYPT);
final CipherStage decrypt = new CipherStage(password, CipherMode.DECRYPT);
final ByteArrayFileWriter writer = new ByteArrayFileWriter(output);
connectIntraThreads(init.getOutputPort(), f2b.getInputPort());
......
......@@ -19,8 +19,8 @@ import java.io.File;
import teetime.framework.AnalysisConfiguration;
import teetime.stage.ByteArray2String;
import teetime.stage.CipherByteArray;
import teetime.stage.CipherByteArray.CipherMode;
import teetime.stage.CipherStage;
import teetime.stage.CipherStage.CipherMode;
import teetime.stage.Counter;
import teetime.stage.InitialElementProducer;
import teetime.stage.ZipByteArray;
......@@ -38,7 +38,7 @@ public class TokenizerConfiguration extends AnalysisConfiguration {
final InitialElementProducer<File> init = new InitialElementProducer<File>(input);
final File2ByteArray f2b = new File2ByteArray();
final ZipByteArray decomp = new ZipByteArray(ZipMode.DECOMP);
final CipherByteArray decrypt = new CipherByteArray(password, CipherMode.DECRYPT);
final CipherStage decrypt = new CipherStage(password, CipherMode.DECRYPT);
final ByteArray2String b2s = new ByteArray2String();
final Tokenizer tokenizer = new Tokenizer(" ");
this.counter = new Counter<String>();
......
......@@ -24,17 +24,17 @@ import java.util.List;
import org.junit.Test;
import teetime.stage.CipherByteArray.CipherMode;
import teetime.stage.CipherStage.CipherMode;
/**
* @author Nils Christian Ehmke
*/
public class CipherByteArrayTest {
public class CipherStageTest {
@Test
public void decryptShouldInvertEncryption() {
final CipherByteArray encryptStage = new CipherByteArray("somePassword", CipherMode.ENCRYPT);
final CipherByteArray decryptStage = new CipherByteArray("somePassword", CipherMode.DECRYPT);
final CipherStage encryptStage = new CipherStage("somePassword", CipherMode.ENCRYPT);
final CipherStage decryptStage = new CipherStage("somePassword", CipherMode.DECRYPT);
final byte[] input = new byte[] { 1, 2, 3, 4, 5 };
final List<byte[]> encryptedResult = new ArrayList<byte[]>();
......
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