diff --git a/mod/common/src/main/java/gjum/minecraft/mapsync/common/net/encryption/EncryptionDecoder.java b/mod/common/src/main/java/gjum/minecraft/mapsync/common/net/encryption/EncryptionDecoder.java index d456051a..52154ab6 100644 --- a/mod/common/src/main/java/gjum/minecraft/mapsync/common/net/encryption/EncryptionDecoder.java +++ b/mod/common/src/main/java/gjum/minecraft/mapsync/common/net/encryption/EncryptionDecoder.java @@ -1,31 +1,44 @@ package gjum.minecraft.mapsync.common.net.encryption; +import gjum.minecraft.mapsync.common.net.Packet; import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToMessageDecoder; - -import javax.crypto.Cipher; -import javax.crypto.ShortBufferException; -import javax.crypto.spec.IvParameterSpec; import java.security.GeneralSecurityException; import java.security.Key; import java.util.List; +import javax.crypto.Cipher; +import javax.crypto.ShortBufferException; +import javax.crypto.spec.IvParameterSpec; +import org.apache.commons.lang3.ArrayUtils; +import org.jetbrains.annotations.NotNull; public class EncryptionDecoder extends MessageToMessageDecoder { - private final EncryptionTranslator decryptionCodec; + private final Cipher cipher; - public EncryptionDecoder(Key key) { + public EncryptionDecoder( + final @NotNull Key key + ) { try { - Cipher cipher = Cipher.getInstance("AES/CFB8/NoPadding"); - cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(key.getEncoded())); - decryptionCodec = new EncryptionTranslator(cipher); - } catch (GeneralSecurityException e) { - throw new RuntimeException(e); + this.cipher = Cipher.getInstance("AES/CFB8/NoPadding"); + this.cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(key.getEncoded())); + } + catch (final GeneralSecurityException thrown) { + throw new IllegalStateException(thrown); } } @Override - protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws ShortBufferException { - out.add(decryptionCodec.decipher(ctx, in)); + protected void decode( + final ChannelHandlerContext ctx, + final ByteBuf in, + final List out + ) throws ShortBufferException { + final byte[] receivedBytes = Packet.readByteArrayOfSize(in, in.readableBytes()); + final byte[] decryptedBytes = this.cipher.update(receivedBytes); + if (ArrayUtils.getLength(decryptedBytes) > 0) { + out.add(Unpooled.wrappedBuffer(decryptedBytes)); + } } } diff --git a/mod/common/src/main/java/gjum/minecraft/mapsync/common/net/encryption/EncryptionEncoder.java b/mod/common/src/main/java/gjum/minecraft/mapsync/common/net/encryption/EncryptionEncoder.java index ef59d71f..09804296 100644 --- a/mod/common/src/main/java/gjum/minecraft/mapsync/common/net/encryption/EncryptionEncoder.java +++ b/mod/common/src/main/java/gjum/minecraft/mapsync/common/net/encryption/EncryptionEncoder.java @@ -1,30 +1,42 @@ package gjum.minecraft.mapsync.common.net.encryption; +import gjum.minecraft.mapsync.common.net.Packet; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToByteEncoder; - +import java.security.GeneralSecurityException; +import java.security.Key; import javax.crypto.Cipher; import javax.crypto.ShortBufferException; import javax.crypto.spec.IvParameterSpec; -import java.security.GeneralSecurityException; -import java.security.Key; +import org.apache.commons.lang3.ArrayUtils; +import org.jetbrains.annotations.NotNull; public class EncryptionEncoder extends MessageToByteEncoder { - private final EncryptionTranslator encryptionCodec; + private final Cipher cipher; - public EncryptionEncoder(Key key) { + public EncryptionEncoder( + final @NotNull Key key + ) { try { - Cipher cipher = Cipher.getInstance("AES/CFB8/NoPadding"); - cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(key.getEncoded())); - encryptionCodec = new EncryptionTranslator(cipher); - } catch (GeneralSecurityException e) { - throw new RuntimeException(e); + this.cipher = Cipher.getInstance("AES/CFB8/NoPadding"); + this.cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(key.getEncoded())); } - } + catch (final GeneralSecurityException thrown) { + throw new IllegalStateException(thrown); + } + } @Override - protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws ShortBufferException { - encryptionCodec.encipher(in, out); + protected void encode( + final ChannelHandlerContext ctx, + final ByteBuf in, + final ByteBuf out + ) throws ShortBufferException { + final byte[] sendingBytes = Packet.readByteArrayOfSize(in, in.readableBytes()); + final byte[] encryptedBytes = this.cipher.update(sendingBytes); + if (ArrayUtils.getLength(encryptedBytes) > 0) { + out.writeBytes(encryptedBytes); + } } } diff --git a/mod/common/src/main/java/gjum/minecraft/mapsync/common/net/encryption/EncryptionTranslator.java b/mod/common/src/main/java/gjum/minecraft/mapsync/common/net/encryption/EncryptionTranslator.java deleted file mode 100644 index 080afeca..00000000 --- a/mod/common/src/main/java/gjum/minecraft/mapsync/common/net/encryption/EncryptionTranslator.java +++ /dev/null @@ -1,48 +0,0 @@ -package gjum.minecraft.mapsync.common.net.encryption; - -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; - -import javax.crypto.Cipher; -import javax.crypto.ShortBufferException; - -public class EncryptionTranslator { - private final Cipher cipher; - private byte[] inputBuffer = new byte[0]; - private byte[] outputBuffer = new byte[0]; - - protected EncryptionTranslator(Cipher cipher) { - this.cipher = cipher; - } - - private byte[] bufToBytes(ByteBuf buf) { - int i = buf.readableBytes(); - - if (this.inputBuffer.length < i) { - this.inputBuffer = new byte[i]; - } - - buf.readBytes(this.inputBuffer, 0, i); - return this.inputBuffer; - } - - protected ByteBuf decipher(ChannelHandlerContext ctx, ByteBuf buffer) throws ShortBufferException { - int i = buffer.readableBytes(); - byte[] bytes = this.bufToBytes(buffer); - ByteBuf bytebuf = ctx.alloc().heapBuffer(this.cipher.getOutputSize(i)); - bytebuf.writerIndex(this.cipher.update(bytes, 0, i, bytebuf.array(), bytebuf.arrayOffset())); - return bytebuf; - } - - protected void encipher(ByteBuf in, ByteBuf out) throws ShortBufferException { - int i = in.readableBytes(); - byte[] bytes = this.bufToBytes(in); - int j = this.cipher.getOutputSize(i); - - if (this.outputBuffer.length < j) { - this.outputBuffer = new byte[j]; - } - - out.writeBytes(this.outputBuffer, 0, this.cipher.update(bytes, 0, i, this.outputBuffer)); - } -}