diff --git a/.settings/edu.umd.cs.findbugs.core.prefs b/.settings/edu.umd.cs.findbugs.core.prefs index d18c2f7f1a5303ffe60d11472e3c995368bea247..f67d8103e32a74af27ae80b5620f73cd4417f0f3 100644 --- a/.settings/edu.umd.cs.findbugs.core.prefs +++ b/.settings/edu.umd.cs.findbugs.core.prefs @@ -1,5 +1,5 @@ #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 diff --git a/src/main/java/teetime/stage/CipherByteArray.java b/src/main/java/teetime/stage/CipherStage.java similarity index 71% rename from src/main/java/teetime/stage/CipherByteArray.java rename to src/main/java/teetime/stage/CipherStage.java index 6fbacf19724580297decbbc14a7ae7cf98b53897..78d2ef2ade608c214bf9be7e06a643c22f6aebba 100644 --- a/src/main/java/teetime/stage/CipherByteArray.java +++ b/src/main/java/teetime/stage/CipherStage.java @@ -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() { diff --git a/src/test/java/teetime/examples/cipher/CipherConfiguration.java b/src/test/java/teetime/examples/cipher/CipherConfiguration.java index 64dbbb0bd3918397ec0c31134dca9346bc116575..fca97df2eca0b734b77004a5927bb8738f4dfff9 100644 --- a/src/test/java/teetime/examples/cipher/CipherConfiguration.java +++ b/src/test/java/teetime/examples/cipher/CipherConfiguration.java @@ -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()); diff --git a/src/test/java/teetime/examples/tokenizer/TokenizerConfiguration.java b/src/test/java/teetime/examples/tokenizer/TokenizerConfiguration.java index 129cdd4fca5665eebcb832534c024ae8ad5d4b0f..e6b0f9d7e4e26f34295dd3ebe7a09e7f11020927 100644 --- a/src/test/java/teetime/examples/tokenizer/TokenizerConfiguration.java +++ b/src/test/java/teetime/examples/tokenizer/TokenizerConfiguration.java @@ -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>(); diff --git a/src/test/java/teetime/stage/CipherByteArrayTest.java b/src/test/java/teetime/stage/CipherStageTest.java similarity index 85% rename from src/test/java/teetime/stage/CipherByteArrayTest.java rename to src/test/java/teetime/stage/CipherStageTest.java index ee1dcd68f8306d65799e4301153f04dded0c7049..4f2d8799d3fc17f8c41662563c7a4f3609c54505 100644 --- a/src/test/java/teetime/stage/CipherByteArrayTest.java +++ b/src/test/java/teetime/stage/CipherStageTest.java @@ -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[]>();