From c9a4b152a0f4678d343f53bc3741fce8855eabab Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Thu, 1 Mar 2018 14:18:58 +0300 Subject: [PATCH] #814 CcAes fixed --- .../org/takes/facets/auth/codecs/CcAes.java | 22 ++++++------------- .../takes/facets/auth/codecs/CcAesTest.java | 11 ++++++++++ 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/takes/facets/auth/codecs/CcAes.java b/src/main/java/org/takes/facets/auth/codecs/CcAes.java index be4ca32e1..74df4c980 100644 --- a/src/main/java/org/takes/facets/auth/codecs/CcAes.java +++ b/src/main/java/org/takes/facets/auth/codecs/CcAes.java @@ -27,6 +27,7 @@ import java.nio.charset.Charset; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; +import java.security.Key; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.spec.AlgorithmParameterSpec; @@ -112,9 +113,7 @@ public Identity decode(final byte[] bytes) throws IOException { private byte[] encrypt(final byte[] bytes) throws IOException { try { return this.create(Cipher.ENCRYPT_MODE).doFinal(bytes); - } catch (final BadPaddingException ex) { - throw new IOException(ex); - } catch (final IllegalBlockSizeException ex) { + } catch (final BadPaddingException | IllegalBlockSizeException ex) { throw new IOException(ex); } } @@ -159,10 +158,8 @@ private static byte[] withCorrectBlockSize(final byte[] key) { private byte[] decrypt(final byte[] bytes) throws IOException { try { return this.create(Cipher.DECRYPT_MODE).doFinal(bytes); - } catch (final BadPaddingException ex) { - throw new IOException(ex); - } catch (final IllegalBlockSizeException ex) { - throw new IOException(ex); + } catch (final BadPaddingException | IllegalBlockSizeException ex) { + throw new DecodingException(ex); } } @@ -176,19 +173,14 @@ private byte[] decrypt(final byte[] bytes) throws IOException { private Cipher create(final int mode) throws IOException { try { - final SecretKeySpec secret = new SecretKeySpec( + final Key secret = new SecretKeySpec( CcAes.withCorrectBlockSize(this.key), "AES" ); final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(mode, secret, this.spec); return cipher; - } catch (final InvalidKeyException ex) { - throw new IOException(ex); - } catch (final NoSuchAlgorithmException ex) { - throw new IOException(ex); - } catch (final NoSuchPaddingException ex) { - throw new IOException(ex); - } catch (final InvalidAlgorithmParameterException ex) { + } catch (final InvalidKeyException | NoSuchAlgorithmException + | NoSuchPaddingException | InvalidAlgorithmParameterException ex) { throw new IOException(ex); } } diff --git a/src/test/java/org/takes/facets/auth/codecs/CcAesTest.java b/src/test/java/org/takes/facets/auth/codecs/CcAesTest.java index 94bec2ebd..9928f75cb 100644 --- a/src/test/java/org/takes/facets/auth/codecs/CcAesTest.java +++ b/src/test/java/org/takes/facets/auth/codecs/CcAesTest.java @@ -68,4 +68,15 @@ public byte[] encode(final Identity identity) Matchers.equalTo(plain) ); } + + /** + * CcAES can throw the right exception. + * @throws Exception any unexpected exception to throw + */ + @Test(expected = DecodingException.class) + public void throwsRightWhenBroken() throws Exception { + new CcAes( + new CcPlain(), "0123456701234567" + ).decode("broken input".getBytes()); + } }