diff --git a/proxy/src/main/java/net/md_5/bungee/netty/CipherCodec.java b/proxy/src/main/java/net/md_5/bungee/netty/CipherCodec.java index 0295a6b0e..7e7756fc8 100644 --- a/proxy/src/main/java/net/md_5/bungee/netty/CipherCodec.java +++ b/proxy/src/main/java/net/md_5/bungee/netty/CipherCodec.java @@ -4,7 +4,6 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ByteToByteCodec; import javax.crypto.Cipher; -import javax.crypto.ShortBufferException; /** * This class is a complete solution for encrypting and decoding bytes in a @@ -35,16 +34,24 @@ public class CipherCodec extends ByteToByteCodec cipher( decrypt, in, out ); } - private void cipher(Cipher cipher, ByteBuf in, ByteBuf out) throws ShortBufferException + private void cipher(Cipher cipher, ByteBuf in, ByteBuf out) throws Exception { - int available = in.readableBytes(); - int outputSize = cipher.getOutputSize( available ); - if ( out.capacity() < outputSize ) + try { - out.capacity( outputSize ); + int available = in.readableBytes(); + int outputSize = cipher.getOutputSize( available ); + int writerIndex = out.writerIndex(); + if ( out.capacity() + writerIndex < outputSize ) + { + out.capacity( outputSize + writerIndex ); + } + int processed = cipher.update( in.nioBuffer(), out.nioBuffer( out.writerIndex(), outputSize ) ); + in.readerIndex( in.readerIndex() + processed ); + out.writerIndex( writerIndex + processed ); + } catch ( Exception ex ) + { + ex.printStackTrace(); + throw ex; } - int processed = cipher.update( in.nioBuffer(), out.nioBuffer( out.writerIndex(), outputSize ) ); - in.readerIndex( in.readerIndex() + processed ); - out.writerIndex( out.writerIndex() + processed ); } }