| 9 | |
| 10 | // AES(CFB) Decrypt |
| 11 | public final class Decrypt2 implements Codec { |
| 12 | private final @NotNull Object aesCrypt; |
| 13 | private final byte[] in = new byte[BLOCK_SIZE]; |
| 14 | private final byte[] out = new byte[BLOCK_SIZE]; |
| 15 | private Codec sink; |
| 16 | private int sinkIndex; |
| 17 | private int writeIndex; |
| 18 | |
| 19 | /** |
| 20 | * @param sink 如果为null,则需要reset才能开始解密 |
| 21 | * @param key 长度只支持16,24,32字节. 不能为null |
| 22 | * @param iv 长度必须至少Encrypt2.BLOCK_SIZE. 如果为null,则需要reset才能开始解密 |
| 23 | */ |
| 24 | public Decrypt2(@Nullable Codec sink, byte @NotNull [] key, byte @Nullable [] iv) throws CodecException { |
| 25 | this.sink = sink; |
| 26 | try { |
| 27 | aesCrypt = mhCryptCtor.invoke(); |
| 28 | mhCryptInit.invoke(aesCrypt, false, "AES", key); |
| 29 | if (iv != null) { |
| 30 | System.arraycopy(iv, 0, out, 0, BLOCK_SIZE); |
| 31 | mhCryptEncrypt.invoke(aesCrypt, out, 0, out, 0); |
| 32 | } |
| 33 | } catch (Error e) { |
| 34 | throw e; |
| 35 | } catch (Throwable e) { // MethodHandle.invoke |
| 36 | throw new CodecException(e); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @param sink 不能为null |
| 42 | * @param iv 长度必须至少Encrypt2.BLOCK_SIZE. 不能为null |
| 43 | */ |
| 44 | public void reset(@NotNull Codec sink, byte @NotNull [] iv) { |
| 45 | System.arraycopy(iv, 0, out, 0, BLOCK_SIZE); |
| 46 | this.sink = sink; |
| 47 | sinkIndex = 0; |
| 48 | writeIndex = 0; |
| 49 | try { |
| 50 | mhCryptEncrypt.invoke(aesCrypt, out, 0, out, 0); |
| 51 | } catch (Error e) { |
| 52 | throw e; |
| 53 | } catch (Throwable e) { // MethodHandle.invoke |
| 54 | throw new CodecException(e); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public void update(byte c) throws CodecException { |
| 60 | in[writeIndex] = c; |
| 61 | out[writeIndex++] ^= c; |
| 62 | if (writeIndex == BLOCK_SIZE) { |
| 63 | writeIndex = 0; |
| 64 | sink.update(out, sinkIndex, BLOCK_SIZE - sinkIndex); |
| 65 | sinkIndex = 0; |
| 66 | try { |
| 67 | mhCryptEncrypt.invoke(aesCrypt, in, 0, out, 0); |
| 68 | } catch (Error e) { |
nothing calls this directly
no outgoing calls
no test coverage detected