| 7 | import org.jetbrains.annotations.NotNull; |
| 8 | |
| 9 | public class Decrypt implements Codec { |
| 10 | private final @NotNull Codec sink; |
| 11 | private final @NotNull Cipher cipher; |
| 12 | private final @NotNull ByteBuffer ivr; |
| 13 | private final @NotNull ByteBuffer ivw; |
| 14 | private final byte @NotNull [] iv; |
| 15 | private final byte @NotNull [] in; |
| 16 | private final byte @NotNull [] out; |
| 17 | private int count = 0; |
| 18 | |
| 19 | public Decrypt(@NotNull Codec sink, byte @NotNull [] key) throws CodecException { |
| 20 | this.sink = sink; |
| 21 | iv = new byte[16]; |
| 22 | in = new byte[16]; |
| 23 | out = new byte[16]; |
| 24 | System.arraycopy(Digest.md5(key), 0, iv, 0, 16); |
| 25 | ivr = ByteBuffer.wrap(iv); |
| 26 | ivw = ByteBuffer.wrap(iv); |
| 27 | try { |
| 28 | cipher = Cipher.getInstance("AES/ECB/NoPadding"); |
| 29 | cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(iv, "AES")); |
| 30 | } catch (Exception e) { |
| 31 | throw new CodecException(e); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | private void succeed() { |
| 36 | try { |
| 37 | cipher.update(ivr, ivw); |
| 38 | ivr.clear(); |
| 39 | ivw.clear(); |
| 40 | } catch (ShortBufferException e) { |
| 41 | // skip |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public void update(byte c) throws CodecException { |
| 47 | if (count < 0) { |
| 48 | sink.update((byte)(iv[count + 16] ^ c)); |
| 49 | iv[count++ + 16] = c; |
| 50 | return; |
| 51 | } |
| 52 | in[count++] = c; |
| 53 | if (count < 16) |
| 54 | return; |
| 55 | succeed(); |
| 56 | for (int i = 0; i < 16; i++) { |
| 57 | out[i] = (byte)(iv[i] ^ in[i]); |
| 58 | iv[i] = in[i]; |
| 59 | } |
| 60 | sink.update(out, 0, 16); |
| 61 | count = 0; |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void update(byte @NotNull [] data, int off, int len) throws CodecException { |
| 66 | int i = off; |
nothing calls this directly
no outgoing calls
no test coverage detected