From 3b907372733476666b3b334f5272d9b0cf914335 Mon Sep 17 00:00:00 2001 From: md_5 Date: Tue, 12 Mar 2013 11:21:39 +1100 Subject: [PATCH] Update CipherCodec to try and fix issues. --- .../net/md_5/bungee/netty/CipherCodec.java | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) 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 0295a6b0..7e7756fc 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 ); } }