public void EncryptDecrypt_ToClient_Succeeds()
        {
            var version   = (ushort)55;
            var aesKey    = (ulong)0x52330F1BB4060813;
            var iv        = (uint)0;
            var encryptor = new MapleCipher(version, aesKey);

            encryptor.SetIv(iv);

            var packet = new PacketWriter();

            packet.WriteByte(1);
            packet.WriteShort(2);
            packet.WriteInt(4);
            packet.WriteLong(8);
            var originalPacket  = packet.ToArray();
            var encryptedPacket = encryptor.Encrypt(packet.ToArray().AsSpan(), true);

            var decryptor = new MapleCipher(version, aesKey);

            decryptor.SetIv(iv);
            var decryptedPacket = decryptor.Decrypt(encryptedPacket.ToArray().AsSpan());

            Assert.AreEqual(originalPacket.ByteArrayToString(), decryptedPacket.ByteArrayToString());
        }
        public void CheckHeaderToServer_Valid_Succeeds()
        {
            var version        = (ushort)55;
            var aesKey         = (ulong)0x52330F1BB4060813;
            var iv             = (uint)0;
            var cryptoInstance = new MapleCipher(version, aesKey);

            cryptoInstance.SetIv(iv);

            var packet = new PacketWriter();

            packet.WriteByte(1);
            packet.WriteShort(2);
            packet.WriteInt(4);
            packet.WriteLong(8);
            var encryptedPacket = cryptoInstance.Encrypt(packet.ToArray().AsSpan(), false);
            var checkCrypto     = new MapleCipher(version, aesKey);

            checkCrypto.SetIv(iv);
            Assert.IsTrue(checkCrypto.CheckHeaderToServer(encryptedPacket));
        }
        public void GetPacketLength_ToServer_Succeeds()
        {
            var version        = (ushort)55;
            var aesKey         = (ulong)0x52330F1BB4060813;
            var iv             = (uint)0;
            var cryptoInstance = new MapleCipher(version, aesKey);

            cryptoInstance.SetIv(iv);

            var packet = new PacketWriter();

            packet.WriteByte(1);
            packet.WriteShort(2);
            packet.WriteInt(4);
            packet.WriteLong(8);
            var originalPacket  = packet.ToArray();
            var encryptedPacket = cryptoInstance.Encrypt(packet.ToArray().AsSpan(), false);
            var decryptedLength = MapleCipher.GetPacketLength(encryptedPacket);

            Assert.AreEqual(originalPacket.Length, decryptedLength);
        }
        public void EncryptDecryptProvider_ToClient_Succeeds()
        {
            var version   = (ushort)55;
            var aesKey    = (ulong)0x52330F1BB4060813;
            var iv        = (uint)0;
            var encryptor = new MapleCipher(version, aesKey);

            encryptor.SetIv(iv);

            var packet = new PacketWriter();

            packet.WriteByte(1);
            packet.WriteShort(2);
            packet.WriteInt(4);
            packet.WriteLong(8);
            var originalPacket  = packet.ToArray();
            var encryptedPacket = encryptor.Encrypt(packet.ToArray().AsSpan(), true);


            var waiter = new ManualResetEventSlim();

            var provider = new MapleCipherProvider(version, aesKey, toClient: false);

            provider.SetVectors(0, 0);
            var buffer = encryptedPacket.ToArray().AsMemory();

            provider.PacketFinished += received =>
            {
                Assert.AreEqual(originalPacket.Length, received.Length, "Packet length should match");
                Assert.AreEqual(originalPacket.ByteArrayToString(), received.ByteArrayToString());
                waiter.Set();
            };
            provider.Decrypt(buffer.Span);

            waiter.Wait();
        }