1
0
mirror of https://github.com/lidgren/lidgren-network-gen3.git synced 2026-05-14 22:26:29 +09:00
Files
lidgren-network-gen3/Lidgren.Network/Encryption/NetXorEncryption.cs
2014-10-10 07:35:17 +00:00

61 lines
1.3 KiB
C#

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