forked from GeyserMC/Geyser
Little changes
This commit is contained in:
parent
f1b19fc04e
commit
9a13b566cf
4 changed files with 117 additions and 6 deletions
|
@ -10,6 +10,12 @@
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>common</artifactId>
|
<artifactId>common</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>1.18.4</version>
|
||||||
|
<scope>providec</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.github.steveice10</groupId>
|
<groupId>com.github.steveice10</groupId>
|
||||||
<artifactId>opennbt</artifactId>
|
<artifactId>opennbt</artifactId>
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
package org.geysermc.floodgate.util;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Getter
|
||||||
|
public class BedrockData {
|
||||||
|
public static final int EXPECTED_LENGTH = 6;
|
||||||
|
private String version;
|
||||||
|
private String username;
|
||||||
|
private String xuid;
|
||||||
|
private int deviceId;
|
||||||
|
private String languageCode;
|
||||||
|
private int inputMode;
|
||||||
|
private int dataLength;
|
||||||
|
|
||||||
|
public BedrockData(String version, String username, String xuid, int deviceId, String languageCode, int inputMode) {
|
||||||
|
this(version, username, xuid, deviceId, languageCode, inputMode, EXPECTED_LENGTH);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BedrockData fromString(String data) {
|
||||||
|
String[] split = data.split("\0");
|
||||||
|
if (split.length != EXPECTED_LENGTH) return null;
|
||||||
|
return new BedrockData(split[0], split[1], split[2], Integer.parseInt(split[3]), split[4], Integer.parseInt(split[5]), split.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BedrockData fromRawData(byte[] data) {
|
||||||
|
return fromString(new String(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return version +'\0'+ username +'\0'+ xuid +'\0'+ deviceId +'\0'+ languageCode +'\0'+ inputMode;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,76 @@
|
||||||
|
package org.geysermc.floodgate.util;
|
||||||
|
|
||||||
|
import javax.crypto.*;
|
||||||
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.security.*;
|
||||||
|
import java.security.spec.EncodedKeySpec;
|
||||||
|
import java.security.spec.InvalidKeySpecException;
|
||||||
|
import java.security.spec.PKCS8EncodedKeySpec;
|
||||||
|
import java.security.spec.X509EncodedKeySpec;
|
||||||
|
import java.util.Base64;
|
||||||
|
|
||||||
|
public class EncryptionUtil {
|
||||||
|
public static String encrypt(Key key, String data) throws IllegalBlockSizeException,
|
||||||
|
InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
|
||||||
|
KeyGenerator generator = KeyGenerator.getInstance("AES");
|
||||||
|
generator.init(128);
|
||||||
|
SecretKey secretKey = generator.generateKey();
|
||||||
|
|
||||||
|
Cipher cipher = Cipher.getInstance("AES");
|
||||||
|
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
|
||||||
|
byte[] encryptedText = cipher.doFinal(data.getBytes());
|
||||||
|
|
||||||
|
cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
|
||||||
|
cipher.init(key instanceof PublicKey ? Cipher.PUBLIC_KEY : Cipher.PRIVATE_KEY, key);
|
||||||
|
return Base64.getEncoder().encodeToString(cipher.doFinal(secretKey.getEncoded())) + '\0' +
|
||||||
|
Base64.getEncoder().encodeToString(encryptedText);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String encryptBedrockData(Key key, BedrockData data) throws IllegalBlockSizeException,
|
||||||
|
InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
|
||||||
|
return encrypt(key, data.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] decrypt(Key key, String encryptedData) throws IllegalBlockSizeException,
|
||||||
|
InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
|
||||||
|
String[] split = encryptedData.split("\0");
|
||||||
|
if (split.length != 2) {
|
||||||
|
throw new IllegalArgumentException("Expected two arguments, got " + split.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
|
||||||
|
cipher.init(key instanceof PublicKey ? Cipher.PUBLIC_KEY : Cipher.PRIVATE_KEY, key);
|
||||||
|
byte[] decryptedKey = cipher.doFinal(Base64.getDecoder().decode(split[0]));
|
||||||
|
|
||||||
|
SecretKey secretKey = new SecretKeySpec(decryptedKey, 0, decryptedKey.length, "AES");
|
||||||
|
cipher = Cipher.getInstance("AES");
|
||||||
|
cipher.init(Cipher.DECRYPT_MODE, secretKey);
|
||||||
|
return cipher.doFinal(Base64.getDecoder().decode(split[1]));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BedrockData decryptBedrockData(Key key, String encryptedData) throws IllegalBlockSizeException,
|
||||||
|
InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
|
||||||
|
return BedrockData.fromRawData(decrypt(key, encryptedData));
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static <T extends Key> T getKeyFromFile(Path fileLocation, Class<T> keyType) throws
|
||||||
|
IOException, InvalidKeySpecException, NoSuchAlgorithmException {
|
||||||
|
boolean isPublicKey = keyType == PublicKey.class;
|
||||||
|
if (!isPublicKey && keyType != PrivateKey.class) {
|
||||||
|
throw new RuntimeException("I can only read public and private keys!");
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] key = Files.readAllBytes(fileLocation);
|
||||||
|
|
||||||
|
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||||
|
EncodedKeySpec keySpec = isPublicKey ? new X509EncodedKeySpec(key) : new PKCS8EncodedKeySpec(key);
|
||||||
|
return (T) (isPublicKey ?
|
||||||
|
keyFactory.generatePublic(keySpec) :
|
||||||
|
keyFactory.generatePrivate(keySpec)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -22,12 +22,6 @@
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.geysermc</groupId>
|
|
||||||
<artifactId>floodgate-common</artifactId>
|
|
||||||
<version>1.0-SNAPSHOT</version>
|
|
||||||
<scope>compile</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||||
<artifactId>jackson-dataformat-yaml</artifactId>
|
<artifactId>jackson-dataformat-yaml</artifactId>
|
||||||
|
|
Loading…
Reference in a new issue