Finish implementing some encryption in the network protocols with AES

This commit is contained in:
zontreck 2025-01-06 02:28:54 -07:00
parent 38eb7c6acd
commit 84cef345eb
10 changed files with 263 additions and 391 deletions

View file

@ -4,8 +4,7 @@ import 'dart:io';
import 'dart:typed_data';
import 'package:libac_dart/encryption/aes.dart';
import 'package:libac_dart/encryption/rsa.dart';
import 'package:libac_dart/encryption/xxtea.dart';
import 'package:libac_dart/encryption/xtea.dart';
import '../nbt/NbtIo.dart';
import '../nbt/Stream.dart';
@ -14,9 +13,8 @@ import '../nbt/impl/CompoundTag.dart';
import '../nbt/impl/StringTag.dart';
enum EncryptionType {
RSA(value: 3),
AES(value: 2),
XXTEA(value: 1),
XTEA(value: 1),
NONE(value: 0);
final int value;
@ -26,16 +24,12 @@ enum EncryptionType {
switch (val) {
case 1:
{
return EncryptionType.XXTEA;
return EncryptionType.XTEA;
}
case 2:
{
return EncryptionType.AES;
}
case 3:
{
return EncryptionType.RSA;
}
default:
{
return EncryptionType.NONE;
@ -49,8 +43,6 @@ class PacketServer {
static bool shouldRestart = true;
static EncryptionType encryptionType = EncryptionType.NONE;
static Uint8List AESKey = Uint8List(0);
static RSAPrivateKey rsaPrivateKey = RSAPrivateKey(BigInt.zero, BigInt.zero);
static RSAPublicKey rsaPublicKey = RSAPublicKey(BigInt.zero, BigInt.zero);
static String PSK = "";
static String TEA_SALT = "Harbinger 01/05/2025 @ 03:59:17 AM";
@ -115,16 +107,17 @@ class PacketServer {
List<int> remainingBytes = layer.readBytes(numBytes);
if (ENCType == EncryptionType.AES) {
AES aes = await AES.useKey(AESKey);
remainingBytes = aes.decrypt(Uint8List.fromList(remainingBytes));
} else if (ENCType == EncryptionType.RSA) {
RSA rsa = await RSA.fromKeyPair(rsaPrivateKey, rsaPublicKey);
remainingBytes = rsa.decrypt(Uint8List.fromList(remainingBytes));
} else if (ENCType == EncryptionType.XXTEA) {
XXTEA xtea = await XXTEA.fromPassword(PSK, salt: TEA_SALT);
int ivLen = layer.readInt();
List<int> ivBytes = layer.readBytes(ivLen);
AESData encData = AESData(iv: ivBytes, data: remainingBytes);
AESCipher aes = await AESCipher.useKey(AESKey);
remainingBytes = await aes.decrypt(encData);
} else if (ENCType == EncryptionType.XTEA) {
XTEA xtea = await XTEA(PSK);
remainingBytes =
xtea.decryptBytes(Uint8List.fromList(remainingBytes));
xtea.decipher(Uint8List.fromList(remainingBytes));
}
CompoundTag tag =
@ -153,14 +146,15 @@ class PacketServer {
// Encryption Subroutine
if (ENCType == EncryptionType.AES) {
AES aes = AES.useKey(AESKey);
nbtData = aes.encrypt(nbtData);
} else if (ENCType == EncryptionType.RSA) {
RSA rsa = RSA.fromKeyPair(rsaPrivateKey, rsaPublicKey);
nbtData = rsa.encrypt(nbtData);
} else if (ENCType == EncryptionType.XXTEA) {
XXTEA tea = await XXTEA.fromPassword(PSK, salt: TEA_SALT);
nbtData = tea.encryptBytes(nbtData);
AESCipher aes = AESCipher.useKey(AESKey);
var encData = await aes.encrypt(nbtData);
nbtData = Uint8List.fromList(encData.data);
layer.writeInt(encData.iv.length);
layer.writeBytes(encData.iv);
} else if (ENCType == EncryptionType.XTEA) {
XTEA tea = await XTEA(PSK);
nbtData = Uint8List.fromList(tea.encipher(nbtData));
}
layer.writeLong(nbtData.lengthInBytes);