using System.IO.Compression; using System.Text; using SCHALE.Common.Crypto; using SCHALE.Common.NetworkProtocol; using Serilog; namespace MX.Core.Crypto { /// Kesunorin public class PacketCryptManager { // private static readonly short PROTOCOL_HEAD_RESERVE = 8; private readonly XORCryptor _cryptor = new(); private readonly FastCRC _checke = new(); private SCHALE.Common.Crypto.ProtocolConverter _converter = new(); public static PacketCryptManager Instance = new(); public byte[] RequestToBinary(Protocol protocol, string json) { byte[] compressedData = Compress(json); _cryptor.Encrypt(compressedData, 0, compressedData.Length); _checke.GetCRC(compressedData, 0, compressedData.Length, out uint crc); Log.Information("CRC: " + crc); Log.Information("protocol: " + protocol); int protocolConverter = _converter.TypeConversion(crc % 99, protocol); Log.Information("protocolConverter: " + protocolConverter); byte[][] data = [ BitConverter.GetBytes(crc), BitConverter.GetBytes(protocolConverter), compressedData ]; byte[] result = data.SelectMany(_ => _).ToArray(); return result; } protected byte[] Compress(string text) { byte[] inputBytes = Encoding.UTF8.GetBytes(text); using var memoryStream = new MemoryStream(); using (var gzipStream = new GZipStream(memoryStream, CompressionMode.Compress, true)) { gzipStream.Write(inputBytes, 0, inputBytes.Length); } byte[] compressedData = memoryStream.ToArray(); byte[][] data = [BitConverter.GetBytes(inputBytes.Length), compressedData]; byte[] result = data.SelectMany(_ => _).ToArray(); return result; } } }