namespace Server; using System.Security.Cryptography; using System.Text; using MassTransit.Mediator; using Microsoft.Extensions.Logging; using NetCoreServer; public class ChannelSession : TcpSession { private static readonly byte[] Key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7 } .Reverse().ToArray(); private static readonly byte[] Iv = new byte[] { 0xfe, 220, 0xba, 0x98, 0x76, 0x54, 50, 0x10, 15, 30, 0x2d, 60, 0x4b, 90, 0x69, 120 }.Reverse().ToArray(); private readonly ICryptoTransform decryptor; private readonly ICryptoTransform encryptor; private readonly ILogger logger; private readonly IMediator mediator; public ChannelSession(TcpServer server, IMediator mediator, ILogger logger) : base(server) { this.mediator = mediator; this.logger = logger; var aes = Aes.Create(); aes.Key = Key; aes.IV = Iv; aes.Padding = PaddingMode.None; aes.Mode = CipherMode.ECB; this.decryptor = aes.CreateDecryptor(aes.Key, aes.IV); this.encryptor = aes.CreateEncryptor(aes.Key, aes.IV); } protected override void OnReceived(byte[] buffer, long offset, long size) { try { using (var ms = new MemoryStream(Decrypt(buffer))) using (var cs = new CryptoStream(ms, this.decryptor, CryptoStreamMode.Read)) { cs.Read(buffer); } base.OnReceived(buffer, offset, size); } catch (CryptographicException ex) { this.logger.LogError("An error has occured while decrypting: {ErrorMessage}", ex.Message); this.logger.LogError("Default buffer message: {Message}", Encoding.ASCII.GetString(buffer)); } } private static byte[] Decrypt(byte[] buffer) { for (var i = 0; i < buffer.Length; ++i) { buffer[i] = (byte)(buffer[i] ^ i ^ (3 * (0xFE - i))); } return buffer; } }