Refactor encrypt util class in preparation for forge support.
This commit is contained in:
parent
6b504d9160
commit
6236cff658
@ -1,20 +1,20 @@
|
|||||||
package net.md_5.bungee;
|
package net.md_5.bungee;
|
||||||
|
|
||||||
import java.security.GeneralSecurityException;
|
import java.security.GeneralSecurityException;
|
||||||
import java.security.InvalidKeyException;
|
|
||||||
import java.security.Key;
|
import java.security.Key;
|
||||||
|
import java.security.KeyFactory;
|
||||||
import java.security.KeyPair;
|
import java.security.KeyPair;
|
||||||
import java.security.KeyPairGenerator;
|
import java.security.KeyPairGenerator;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.PublicKey;
|
||||||
|
import java.security.spec.X509EncodedKeySpec;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import javax.crypto.BadPaddingException;
|
|
||||||
import javax.crypto.Cipher;
|
import javax.crypto.Cipher;
|
||||||
import javax.crypto.IllegalBlockSizeException;
|
|
||||||
import javax.crypto.NoSuchPaddingException;
|
|
||||||
import javax.crypto.SecretKey;
|
import javax.crypto.SecretKey;
|
||||||
import javax.crypto.spec.IvParameterSpec;
|
import javax.crypto.spec.IvParameterSpec;
|
||||||
import javax.crypto.spec.SecretKeySpec;
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
|
import lombok.Getter;
|
||||||
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;
|
||||||
|
|
||||||
@ -26,14 +26,22 @@ public class EncryptionUtil
|
|||||||
|
|
||||||
private static final Random random = new Random();
|
private static final Random random = new Random();
|
||||||
public static KeyPair keys;
|
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();
|
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 ) : "-";
|
String hash = ( BungeeCord.getInstance().config.isOnlineMode() ) ? Long.toString( random.nextLong(), 16 ) : "-";
|
||||||
byte[] pubKey = keys.getPublic().getEncoded();
|
byte[] pubKey = keys.getPublic().getEncoded();
|
||||||
byte[] verify = new byte[ 4 ];
|
byte[] verify = new byte[ 4 ];
|
||||||
@ -41,7 +49,7 @@ public class EncryptionUtil
|
|||||||
return new PacketFDEncryptionRequest( hash, pubKey, verify );
|
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 cipher = Cipher.getInstance( "RSA" );
|
||||||
cipher.init( Cipher.DECRYPT_MODE, keys.getPrivate() );
|
cipher.init( Cipher.DECRYPT_MODE, keys.getPrivate() );
|
||||||
@ -65,4 +73,16 @@ public class EncryptionUtil
|
|||||||
cip.init( opMode, shared, new IvParameterSpec( shared.getEncoded() ) );
|
cip.init( opMode, shared, new IvParameterSpec( shared.getEncoded() ) );
|
||||||
return cip;
|
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 );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user