diff --git a/proxy/src/main/java/net/md_5/bungee/EncryptionUtil.java b/proxy/src/main/java/net/md_5/bungee/EncryptionUtil.java index 28a1df1a..8f6494e0 100644 --- a/proxy/src/main/java/net/md_5/bungee/EncryptionUtil.java +++ b/proxy/src/main/java/net/md_5/bungee/EncryptionUtil.java @@ -1,20 +1,20 @@ package net.md_5.bungee; import java.security.GeneralSecurityException; -import java.security.InvalidKeyException; import java.security.Key; +import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; +import java.security.PublicKey; +import java.security.spec.X509EncodedKeySpec; import java.util.Arrays; import java.util.Random; -import javax.crypto.BadPaddingException; import javax.crypto.Cipher; -import javax.crypto.IllegalBlockSizeException; -import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; +import lombok.Getter; import net.md_5.bungee.packet.PacketFCEncryptionResponse; import net.md_5.bungee.packet.PacketFDEncryptionRequest; @@ -26,14 +26,22 @@ public class EncryptionUtil private static final Random random = new Random(); public static KeyPair keys; + @Getter + private static SecretKey secret = new SecretKeySpec( new byte[ 16 ], "AES" ); - public static PacketFDEncryptionRequest encryptRequest() throws NoSuchAlgorithmException + static { - if ( keys == null ) + try { keys = KeyPairGenerator.getInstance( "RSA" ).generateKeyPair(); + } catch ( NoSuchAlgorithmException ex ) + { + throw new ExceptionInInitializerError( ex ); } + } + public static PacketFDEncryptionRequest encryptRequest() + { String hash = ( BungeeCord.getInstance().config.isOnlineMode() ) ? Long.toString( random.nextLong(), 16 ) : "-"; byte[] pubKey = keys.getPublic().getEncoded(); byte[] verify = new byte[ 4 ]; @@ -41,7 +49,7 @@ public class EncryptionUtil return new PacketFDEncryptionRequest( hash, pubKey, verify ); } - public static SecretKey getSecret(PacketFCEncryptionResponse resp, PacketFDEncryptionRequest request) throws BadPaddingException, IllegalBlockSizeException, IllegalStateException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException + public static SecretKey getSecret(PacketFCEncryptionResponse resp, PacketFDEncryptionRequest request) throws GeneralSecurityException { Cipher cipher = Cipher.getInstance( "RSA" ); cipher.init( Cipher.DECRYPT_MODE, keys.getPrivate() ); @@ -65,4 +73,16 @@ public class EncryptionUtil cip.init( opMode, shared, new IvParameterSpec( shared.getEncoded() ) ); return cip; } + + public static PublicKey getPubkey(PacketFDEncryptionRequest request) throws GeneralSecurityException + { + return KeyFactory.getInstance( "RSA" ).generatePublic( new X509EncodedKeySpec( request.publicKey ) ); + } + + public static byte[] encrypt(Key key, byte[] b) throws GeneralSecurityException + { + Cipher hasher = Cipher.getInstance( "RSA" ); + hasher.init( Cipher.ENCRYPT_MODE, key ); + return hasher.doFinal( b ); + } }