using System; using System.Collections.Generic; using System.Text; namespace Lidgren.Network { /// /// Example class; not very good encryption /// public class NetXorEncryption : INetEncryption { private byte[] m_key; /// /// NetXorEncryption constructor /// public NetXorEncryption(byte[] key) { m_key = key; } /// /// NetXorEncryption constructor /// public NetXorEncryption(string key) { m_key = Encoding.ASCII.GetBytes(key); } /// /// Encrypt an outgoing message /// public 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 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; } } }