using System; using System.Collections.Generic; using System.Text; namespace Lidgren.Network { /// /// Example class; not very good encryption /// public class NetXorEncryption : NetEncryption { private byte[] m_key; /// /// NetXorEncryption constructor /// public NetXorEncryption(NetPeer peer, byte[] key) : base(peer) { m_key = key; } public override void SetKey(byte[] data, int offset, int count) { m_key = new byte[count]; Array.Copy(data, offset, m_key, 0, count); } /// /// NetXorEncryption constructor /// public NetXorEncryption(NetPeer peer, string key) : base(peer) { m_key = Encoding.UTF8.GetBytes(key); } /// /// Encrypt an outgoing message /// public override bool Encrypt(NetOutgoingMessage msg) { int numBytes = msg.LengthBytes; for (int i = 0; i < numBytes; i++) { int offset = i % m_key.Length; msg.m_data[i] = (byte)(msg.m_data[i] ^ m_key[offset]); } return true; } /// /// Decrypt an incoming message /// public override bool Decrypt(NetIncomingMessage msg) { int numBytes = msg.LengthBytes; for (int i = 0; i < numBytes; i++) { int offset = i % m_key.Length; msg.m_data[i] = (byte)(msg.m_data[i] ^ m_key[offset]); } return true; } } }