public async Task <byte[]> ReceivePacket() { var message = new byte[4]; await _socket.Receive(message); var pLen = (message[2] << 8) | message[1]; Array.Resize(ref message, pLen + 4); await _socket.Receive(message, 4, pLen); return(ClientCipher.Decrypt(message, _key)); }
public Packet(byte[] message, byte key) { Id = BitConverter.ToInt16(new byte[] { message[5], message[6] }, 0); MessageCrypted = new byte[message.Length]; Buffer.BlockCopy(message, 0, MessageCrypted, 0, message.Length); //Copia mensagem recebida criptografada Message = ClientCipher.Decrypt(message, key); _stream = new MemoryStream(Message); _stream.Seek(2, SeekOrigin.Current); //Seek Inicial Reader = new PangyaBinaryReader(_stream); }
public void TestCryptoGood() { var testCases = new List <(byte key, byte[] plain, byte[] cipher)> { (0, new byte[] { 0x00, 0x00, 0x02, 0x0D, 0x00, 0x68, 0x65, 0x79, 0x6C, 0x6C, 0x6F, 0x77, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64, 0x3D, 0x1F, 0x00, 0x00, 0x09, 0x00, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30, 0x2E, 0x31 }, new byte[] { 0x34, 0x22, 0x00, 0x00, 0xF0, 0x00, 0x00, 0x02, 0xC6, 0x00, 0x68, 0x67, 0x74, 0x6C, 0x04, 0x0A, 0x0E, 0x4C, 0x1B, 0x00, 0x05, 0x4C, 0x13, 0x52, 0x6D, 0x6C, 0x64, 0x34, 0x1F, 0x31, 0x32, 0x3E, 0x2E, 0x01, 0x1C, 0x07, 0x00, 0x01 }), (5, new byte[] { 0x01, 0x00, 0x04, 0x00, 0x6a, 0x6f, 0x68, 0x6e, 0x20, 0x00, 0x30, 0x39, 0x38, 0x46, 0x36, 0x42, 0x43, 0x44, 0x34, 0x36, 0x32, 0x31, 0x44, 0x33, 0x37, 0x33, 0x43, 0x41, 0x44, 0x45, 0x34, 0x45, 0x38, 0x33, 0x32, 0x36, 0x32, 0x37, 0x42, 0x34, 0x46, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, new byte[] { 0x07, 0x3c, 0x00, 0x00, 0x5b, 0x01, 0x00, 0x04, 0x47, 0x6b, 0x6f, 0x6c, 0x6e, 0x4a, 0x6f, 0x58, 0x57, 0x18, 0x46, 0x06, 0x7b, 0x7b, 0x02, 0x02, 0x74, 0x71, 0x75, 0x70, 0x05, 0x05, 0x02, 0x07, 0x72, 0x73, 0x76, 0x77, 0x04, 0x7c, 0x76, 0x06, 0x73, 0x0a, 0x04, 0x70, 0x02, 0x74, 0x01, 0x42, 0x34, 0x46, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }), (5, new byte[] { 0x03, 0x00, 0xea, 0x4e, 0x00, 0x00 }, new byte[] { 0x11, 0x07, 0x00, 0x00, 0x6e, 0x03, 0x00, 0xea, 0xcf, 0x03, 0x00 }) }; foreach (var(key, plain, cipher) in testCases) { CollectionAssert.AreEqual(plain, ClientCipher.Decrypt(cipher, key), "decrypt failed"); CollectionAssert.AreEqual(cipher, ClientCipher.Encrypt(plain, key, cipher[0]), "encrypt failed"); } }
public void TestInvalidBuffer() { ClientCipher.Decrypt(new byte[] { }, 0x00); }
public void TestInvalidKeyDecrypt() { ClientCipher.Decrypt(new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00 }, 0x10); }