1
0
mirror of https://github.com/lidgren/lidgren-network-gen3.git synced 2026-05-16 15:16:33 +09:00

Net encryption generalized to support more algorithms

This commit is contained in:
lidgren
2011-04-16 22:37:15 +00:00
parent 6976177f94
commit a4503d22e1
11 changed files with 317 additions and 63 deletions

View File

@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Lidgren.Network
{
/// <summary>
/// Example class; not very good encryption
/// </summary>
public class NetXorEncryption : INetEncryption
{
private byte[] m_key;
public NetXorEncryption(byte[] key)
{
m_key = key;
}
public NetXorEncryption(string key)
{
m_key = Encoding.ASCII.GetBytes(key);
}
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;
}
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;
}
}
}