Skip to content

Commit

Permalink
#814 CcAes fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Mar 1, 2018
1 parent 277f0a8 commit c9a4b15
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
22 changes: 7 additions & 15 deletions src/main/java/org/takes/facets/auth/codecs/CcAes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/org/takes/facets/auth/codecs/CcAesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit c9a4b15

Please sign in to comment.