Use native Java ciphers for encryption / decryption - now that we have our buffer issues sorted this should be a small speed boost.

This commit is contained in:
md_5 2013-02-21 17:13:04 +11:00
parent 0f8f8cb289
commit f1f10521e2
3 changed files with 10 additions and 24 deletions

View File

@ -34,11 +34,6 @@
<artifactId>mysql-connector-java</artifactId> <artifactId>mysql-connector-java</artifactId>
<version>5.1.22</version> <version>5.1.22</version>
</dependency> </dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.47</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -6,13 +6,13 @@ import java.io.InputStreamReader;
import java.math.BigInteger; import java.math.BigInteger;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import java.security.Key; import java.security.Key;
import java.security.KeyPair; import java.security.KeyPair;
import java.security.KeyPairGenerator; import java.security.KeyPairGenerator;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.util.Arrays; import java.util.Arrays;
import java.util.Random; import java.util.Random;
import javax.crypto.BadPaddingException; import javax.crypto.BadPaddingException;
@ -20,15 +20,10 @@ import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException; import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException; import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import net.md_5.bungee.packet.PacketFCEncryptionResponse; import net.md_5.bungee.packet.PacketFCEncryptionResponse;
import net.md_5.bungee.packet.PacketFDEncryptionRequest; import net.md_5.bungee.packet.PacketFDEncryptionRequest;
import org.bouncycastle.crypto.BufferedBlockCipher;
import org.bouncycastle.crypto.engines.AESFastEngine;
import org.bouncycastle.crypto.modes.CFBBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
/** /**
* Class containing all encryption related methods for the proxy. * Class containing all encryption related methods for the proxy.
@ -39,11 +34,6 @@ public class EncryptionUtil
private static final Random random = new Random(); private static final Random random = new Random();
private static KeyPair keys; private static KeyPair keys;
static
{
Security.addProvider( new BouncyCastleProvider() );
}
public static PacketFDEncryptionRequest encryptRequest() throws NoSuchAlgorithmException public static PacketFDEncryptionRequest encryptRequest() throws NoSuchAlgorithmException
{ {
if ( keys == null ) if ( keys == null )
@ -100,10 +90,10 @@ public class EncryptionUtil
return "YES".equals( reply ); return "YES".equals( reply );
} }
public static BufferedBlockCipher getCipher(boolean forEncryption, Key shared) public static Cipher getCipher(int opMode, Key shared) throws InvalidAlgorithmParameterException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException
{ {
BufferedBlockCipher cip = new BufferedBlockCipher( new CFBBlockCipher( new AESFastEngine(), 8 ) ); Cipher cip = Cipher.getInstance( "AES/CFB8/NoPadding" );
cip.init( forEncryption, new ParametersWithIV( new KeyParameter( shared.getEncoded() ), shared.getEncoded() ) ); cip.init( opMode, shared, new IvParameterSpec( shared.getEncoded() ) );
return cip; return cip;
} }
} }

View File

@ -7,6 +7,9 @@ import java.net.InetSocketAddress;
import java.net.Socket; import java.net.Socket;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
import lombok.Getter; import lombok.Getter;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
@ -30,8 +33,6 @@ import net.md_5.bungee.packet.PacketFFKick;
import net.md_5.bungee.packet.PacketHandler; import net.md_5.bungee.packet.PacketHandler;
import net.md_5.bungee.packet.PacketStream; import net.md_5.bungee.packet.PacketStream;
import net.md_5.mendax.PacketDefinitions; import net.md_5.mendax.PacketDefinitions;
import org.bouncycastle.crypto.io.CipherInputStream;
import org.bouncycastle.crypto.io.CipherOutputStream;
public class InitialHandler extends PacketHandler implements Runnable, PendingConnection public class InitialHandler extends PacketHandler implements Runnable, PendingConnection
{ {
@ -143,8 +144,8 @@ public class InitialHandler extends PacketHandler implements Runnable, PendingCo
} }
stream.write( new PacketFCEncryptionResponse() ); stream.write( new PacketFCEncryptionResponse() );
stream = new PacketStream( new CipherInputStream( socket.getInputStream(), EncryptionUtil.getCipher( false, shared ) ), stream = new PacketStream( new CipherInputStream( socket.getInputStream(), EncryptionUtil.getCipher( Cipher.DECRYPT_MODE, shared ) ),
new CipherOutputStream( socket.getOutputStream(), EncryptionUtil.getCipher( true, shared ) ), stream.getProtocol() ); new CipherOutputStream( socket.getOutputStream(), EncryptionUtil.getCipher( Cipher.ENCRYPT_MODE, shared ) ), stream.getProtocol() );
thisState = State.LOGIN; thisState = State.LOGIN;
} }