1
0
mirror of https://github.com/lidgren/lidgren-network-gen3.git synced 2026-05-17 23:56:30 +09:00

Encryption fixed

This commit is contained in:
lidgren
2014-10-10 07:35:17 +00:00
parent e206ba210e
commit d38143cca7
14 changed files with 167 additions and 96 deletions

View File

@@ -1,21 +0,0 @@
using System;
using System.Collections.Generic;
namespace Lidgren.Network
{
/// <summary>
/// Interface for an encryption algorithm
/// </summary>
public interface INetEncryption
{
/// <summary>
/// Encrypt an outgoing message in place
/// </summary>
bool Encrypt(NetOutgoingMessage msg);
/// <summary>
/// Decrypt an incoming message in place
/// </summary>
bool Decrypt(NetIncomingMessage msg);
}
}

View File

@@ -9,7 +9,7 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// AES encryption /// AES encryption
/// </summary> /// </summary>
public class NetAESEncryption : INetEncryption public class NetAESEncryption : NetEncryption
{ {
private readonly byte[] m_key; private readonly byte[] m_key;
private readonly byte[] m_iv; private readonly byte[] m_iv;
@@ -52,7 +52,8 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// NetAESEncryption constructor /// NetAESEncryption constructor
/// </summary> /// </summary>
public NetAESEncryption(byte[] key, byte[] iv) public NetAESEncryption(NetPeer peer, byte[] key, byte[] iv)
: base(peer)
{ {
if (!s_keysizes.Contains(key.Length * 8)) if (!s_keysizes.Contains(key.Length * 8))
throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(s_keysizes))); throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(s_keysizes)));
@@ -68,7 +69,8 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// NetAESEncryption constructor /// NetAESEncryption constructor
/// </summary> /// </summary>
public NetAESEncryption(string key, int bitsize) public NetAESEncryption(NetPeer peer, string key, int bitsize)
: base(peer)
{ {
if (!s_keysizes.Contains(bitsize)) if (!s_keysizes.Contains(bitsize))
throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(s_keysizes))); throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(s_keysizes)));
@@ -93,20 +95,19 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// NetAESEncryption constructor /// NetAESEncryption constructor
/// </summary> /// </summary>
public NetAESEncryption(string key) public NetAESEncryption(NetPeer peer, string key)
: this(key, s_keysizes[0]) : this(peer, key, s_keysizes[0])
{ {
} }
/// <summary> /// <summary>
/// Encrypt outgoing message /// Encrypt outgoing message
/// </summary> /// </summary>
public bool Encrypt(NetOutgoingMessage msg) public override bool Encrypt(NetOutgoingMessage msg)
{ {
#if !IOS && !__ANDROID__ && !UNITY_4_5 #if !IOS && !__ANDROID__ && !UNITY_4_5
try try
{ {
// nested usings are fun!
using (AesCryptoServiceProvider aesCryptoServiceProvider = new AesCryptoServiceProvider { KeySize = m_bitSize, Mode = CipherMode.CBC }) using (AesCryptoServiceProvider aesCryptoServiceProvider = new AesCryptoServiceProvider { KeySize = m_bitSize, Mode = CipherMode.CBC })
{ {
using (ICryptoTransform cryptoTransform = aesCryptoServiceProvider.CreateEncryptor(m_key, m_iv)) using (ICryptoTransform cryptoTransform = aesCryptoServiceProvider.CreateEncryptor(m_key, m_iv))
@@ -116,14 +117,19 @@ namespace Lidgren.Network
{ {
cryptoStream.Write(msg.m_data, 0, msg.m_data.Length); cryptoStream.Write(msg.m_data, 0, msg.m_data.Length);
cryptoStream.Close(); cryptoStream.Close();
msg.m_data = memoryStream.ToArray();
m_peer.Recycle(msg.m_data);
var arr = memoryStream.ToArray();
msg.m_data = arr;
msg.m_bitLength = arr.Length * 8;
} }
} }
} }
} }
catch catch(Exception ex)
{ {
m_peer.LogWarning("Encryption failed: " + ex);
return false; return false;
} }
return true; return true;
@@ -135,7 +141,7 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// Decrypt incoming message /// Decrypt incoming message
/// </summary> /// </summary>
public bool Decrypt(NetIncomingMessage msg) public override bool Decrypt(NetIncomingMessage msg)
{ {
#if !IOS && !__ANDROID__ && !UNITY_4_5 #if !IOS && !__ANDROID__ && !UNITY_4_5
try try
@@ -150,14 +156,19 @@ namespace Lidgren.Network
{ {
cryptoStream.Write(msg.m_data, 0, msg.m_data.Length); cryptoStream.Write(msg.m_data, 0, msg.m_data.Length);
cryptoStream.Close(); cryptoStream.Close();
msg.m_data = memoryStream.ToArray();
m_peer.Recycle(msg.m_data);
var arr = memoryStream.ToArray();
msg.m_data = arr;
msg.m_bitLength = arr.Length * 8;
} }
} }
} }
} }
catch catch (Exception ex)
{ {
m_peer.LogWarning("Decryption failed: " + ex);
return false; return false;
} }
return true; return true;

View File

@@ -6,7 +6,7 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// Base for a non-threadsafe encryption class /// Base for a non-threadsafe encryption class
/// </summary> /// </summary>
public abstract class NetBlockEncryptionBase : INetEncryption public abstract class NetBlockEncryptionBase : NetEncryption
{ {
// temporary space for one block to avoid reallocating every time // temporary space for one block to avoid reallocating every time
private byte[] m_tmp; private byte[] m_tmp;
@@ -19,7 +19,8 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// NetBlockEncryptionBase constructor /// NetBlockEncryptionBase constructor
/// </summary> /// </summary>
public NetBlockEncryptionBase() public NetBlockEncryptionBase(NetPeer peer)
: base(peer)
{ {
m_tmp = new byte[BlockSize]; m_tmp = new byte[BlockSize];
} }
@@ -27,7 +28,7 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// Encrypt am outgoing message with this algorithm; no writing can be done to the message after encryption, or message will be corrupted /// Encrypt am outgoing message with this algorithm; no writing can be done to the message after encryption, or message will be corrupted
/// </summary> /// </summary>
public bool Encrypt(NetOutgoingMessage msg) public override bool Encrypt(NetOutgoingMessage msg)
{ {
int payloadBitLength = msg.LengthBits; int payloadBitLength = msg.LengthBits;
int numBytes = msg.LengthBytes; int numBytes = msg.LengthBytes;
@@ -55,7 +56,7 @@ namespace Lidgren.Network
/// </summary> /// </summary>
/// <param name="msg">message to decrypt</param> /// <param name="msg">message to decrypt</param>
/// <returns>true if successful; false if failed</returns> /// <returns>true if successful; false if failed</returns>
public bool Decrypt(NetIncomingMessage msg) public override bool Decrypt(NetIncomingMessage msg)
{ {
int numEncryptedBytes = msg.LengthBytes - 4; // last 4 bytes is true bit length int numEncryptedBytes = msg.LengthBytes - 4; // last 4 bytes is true bit length
int blockSize = BlockSize; int blockSize = BlockSize;

View File

@@ -9,7 +9,7 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// DES encryption /// DES encryption
/// </summary> /// </summary>
public class NetDESEncryption : INetEncryption public class NetDESEncryption : NetEncryption
{ {
private readonly byte[] m_key; private readonly byte[] m_key;
private readonly byte[] m_iv; private readonly byte[] m_iv;
@@ -51,13 +51,14 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// NetDESEncryption constructor /// NetDESEncryption constructor
/// </summary> /// </summary>
public NetDESEncryption(byte[] key, byte[] iv) public NetDESEncryption(NetPeer peer, byte[] key, byte[] iv)
: base(peer)
{ {
if (!s_keysizes.Contains(key.Length * 8)) if (!s_keysizes.Contains(key.Length * 8))
throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(s_keysizes))); throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(s_keysizes)));
if (!s_blocksizes.Contains(iv.Length * 8)) if (!s_blocksizes.Contains(iv.Length * 8))
throw new NetException(string.Format("Not a valid iv size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(s_blocksizes))); throw new NetException(string.Format("Not a valid iv size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(s_blocksizes)));
m_key = key; m_key = key;
m_iv = iv; m_iv = iv;
@@ -67,7 +68,8 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// NetDESEncryption constructor /// NetDESEncryption constructor
/// </summary> /// </summary>
public NetDESEncryption(string key, int bitsize) public NetDESEncryption(NetPeer peer, string key, int bitsize)
: base(peer)
{ {
if (!s_keysizes.Contains(bitsize)) if (!s_keysizes.Contains(bitsize))
throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(s_keysizes))); throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(s_keysizes)));
@@ -92,19 +94,18 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// NetDESEncryption constructor /// NetDESEncryption constructor
/// </summary> /// </summary>
public NetDESEncryption(string key) public NetDESEncryption(NetPeer peer, string key)
: this(key, s_keysizes[0]) : this(peer, key, s_keysizes[0])
{ {
} }
/// <summary> /// <summary>
/// Encrypt outgoing message /// Encrypt outgoing message
/// </summary> /// </summary>
public bool Encrypt(NetOutgoingMessage msg) public override bool Encrypt(NetOutgoingMessage msg)
{ {
try try
{ {
// nested usings are fun!
using (DESCryptoServiceProvider desCryptoServiceProvider = new DESCryptoServiceProvider { KeySize = m_bitSize, Mode = CipherMode.CBC }) using (DESCryptoServiceProvider desCryptoServiceProvider = new DESCryptoServiceProvider { KeySize = m_bitSize, Mode = CipherMode.CBC })
{ {
using (ICryptoTransform cryptoTransform = desCryptoServiceProvider.CreateEncryptor(m_key, m_iv)) using (ICryptoTransform cryptoTransform = desCryptoServiceProvider.CreateEncryptor(m_key, m_iv))
@@ -114,14 +115,19 @@ namespace Lidgren.Network
{ {
cryptoStream.Write(msg.m_data, 0, msg.m_data.Length); cryptoStream.Write(msg.m_data, 0, msg.m_data.Length);
cryptoStream.Close(); cryptoStream.Close();
msg.m_data = memoryStream.ToArray();
m_peer.Recycle(msg.m_data);
var arr = memoryStream.ToArray();
msg.m_data = arr;
msg.m_bitLength = arr.Length * 8;
} }
} }
} }
} }
catch catch (Exception ex)
{ {
m_peer.LogWarning("Encryption failed: " + ex);
return false; return false;
} }
return true; return true;
@@ -130,11 +136,10 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// Decrypt incoming message /// Decrypt incoming message
/// </summary> /// </summary>
public bool Decrypt(NetIncomingMessage msg) public override bool Decrypt(NetIncomingMessage msg)
{ {
try try
{ {
// nested usings are fun!
using (DESCryptoServiceProvider desCryptoServiceProvider = new DESCryptoServiceProvider { KeySize = m_bitSize, Mode = CipherMode.CBC }) using (DESCryptoServiceProvider desCryptoServiceProvider = new DESCryptoServiceProvider { KeySize = m_bitSize, Mode = CipherMode.CBC })
{ {
using (ICryptoTransform cryptoTransform = desCryptoServiceProvider.CreateDecryptor(m_key, m_iv)) using (ICryptoTransform cryptoTransform = desCryptoServiceProvider.CreateDecryptor(m_key, m_iv))
@@ -144,14 +149,19 @@ namespace Lidgren.Network
{ {
cryptoStream.Write(msg.m_data, 0, msg.m_data.Length); cryptoStream.Write(msg.m_data, 0, msg.m_data.Length);
cryptoStream.Close(); cryptoStream.Close();
msg.m_data = memoryStream.ToArray();
m_peer.Recycle(msg.m_data);
var arr = memoryStream.ToArray();
msg.m_data = arr;
msg.m_bitLength = arr.Length * 8;
} }
} }
} }
} }
catch catch (Exception ex)
{ {
m_peer.LogWarning("Decryption failed: " + ex);
return false; return false;
} }
return true; return true;

View File

@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
namespace Lidgren.Network
{
/// <summary>
/// Interface for an encryption algorithm
/// </summary>
public abstract class NetEncryption
{
/// <summary>
/// NetPeer
/// </summary>
protected NetPeer m_peer;
/// <summary>
/// Constructor
/// </summary>
public NetEncryption(NetPeer peer)
{
if (peer == null)
throw new NetException("Peer must not be null");
m_peer = peer;
}
/// <summary>
/// Encrypt an outgoing message in place
/// </summary>
public abstract bool Encrypt(NetOutgoingMessage msg);
/// <summary>
/// Decrypt an incoming message in place
/// </summary>
public abstract bool Decrypt(NetIncomingMessage msg);
}
}

View File

@@ -9,7 +9,7 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// RC2 encryption /// RC2 encryption
/// </summary> /// </summary>
public class NetRC2Encryption : INetEncryption public class NetRC2Encryption : NetEncryption
{ {
private readonly byte[] m_key; private readonly byte[] m_key;
private readonly byte[] m_iv; private readonly byte[] m_iv;
@@ -51,7 +51,8 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// NetRC2Encryption constructor /// NetRC2Encryption constructor
/// </summary> /// </summary>
public NetRC2Encryption(byte[] key, byte[] iv) public NetRC2Encryption(NetPeer peer, byte[] key, byte[] iv)
: base(peer)
{ {
if (!s_keysizes.Contains(key.Length * 8)) if (!s_keysizes.Contains(key.Length * 8))
throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(s_keysizes))); throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(s_keysizes)));
@@ -67,11 +68,12 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// NetRC2Encryption constructor /// NetRC2Encryption constructor
/// </summary> /// </summary>
public NetRC2Encryption(string key, int bitsize) public NetRC2Encryption(NetPeer peer, string key, int bitsize)
: base(peer)
{ {
if (!s_keysizes.Contains(bitsize)) if (!s_keysizes.Contains(bitsize))
throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(s_keysizes))); throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(s_keysizes)));
byte[] entropy = Encoding.UTF32.GetBytes(key); byte[] entropy = Encoding.UTF32.GetBytes(key);
// I know hardcoding salts is bad, but in this case I think it is acceptable. // I know hardcoding salts is bad, but in this case I think it is acceptable.
HMACSHA512 hmacsha512 = new HMACSHA512(Convert.FromBase64String("i88NEiez3c50bHqr3YGasDc4p8jRrxJAaiRiqixpvp4XNAStP5YNoC2fXnWkURtkha6M8yY901Gj07IRVIRyGL==")); HMACSHA512 hmacsha512 = new HMACSHA512(Convert.FromBase64String("i88NEiez3c50bHqr3YGasDc4p8jRrxJAaiRiqixpvp4XNAStP5YNoC2fXnWkURtkha6M8yY901Gj07IRVIRyGL=="));
@@ -92,20 +94,20 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// NetRC2Encryption constructor /// NetRC2Encryption constructor
/// </summary> /// </summary>
/// <param name="peer"></param>
/// <param name="key"></param> /// <param name="key"></param>
public NetRC2Encryption(string key) public NetRC2Encryption(NetPeer peer, string key)
: this(key, s_keysizes[0]) : this(peer, key, s_keysizes[0])
{ {
} }
/// <summary> /// <summary>
/// Encrypt outgoing message /// Encrypt outgoing message
/// </summary> /// </summary>
public bool Encrypt(NetOutgoingMessage msg) public override bool Encrypt(NetOutgoingMessage msg)
{ {
try try
{ {
// nested usings are fun!
using (RC2CryptoServiceProvider rc2CryptoServiceProvider = new RC2CryptoServiceProvider { KeySize = m_bitSize, Mode = CipherMode.CBC }) using (RC2CryptoServiceProvider rc2CryptoServiceProvider = new RC2CryptoServiceProvider { KeySize = m_bitSize, Mode = CipherMode.CBC })
{ {
using (ICryptoTransform cryptoTransform = rc2CryptoServiceProvider.CreateEncryptor(m_key, m_iv)) using (ICryptoTransform cryptoTransform = rc2CryptoServiceProvider.CreateEncryptor(m_key, m_iv))
@@ -115,14 +117,19 @@ namespace Lidgren.Network
{ {
cryptoStream.Write(msg.m_data, 0, msg.m_data.Length); cryptoStream.Write(msg.m_data, 0, msg.m_data.Length);
cryptoStream.Close(); cryptoStream.Close();
msg.m_data = memoryStream.ToArray();
m_peer.Recycle(msg.m_data);
var arr = memoryStream.ToArray();
msg.m_data = arr;
msg.m_bitLength = arr.Length * 8;
} }
} }
} }
} }
catch catch (Exception ex)
{ {
m_peer.LogWarning("Encryption failed: " + ex);
return false; return false;
} }
return true; return true;
@@ -131,7 +138,7 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// Decrypt incoming message /// Decrypt incoming message
/// </summary> /// </summary>
public bool Decrypt(NetIncomingMessage msg) public override bool Decrypt(NetIncomingMessage msg)
{ {
try try
{ {
@@ -145,14 +152,19 @@ namespace Lidgren.Network
{ {
cryptoStream.Write(msg.m_data, 0, msg.m_data.Length); cryptoStream.Write(msg.m_data, 0, msg.m_data.Length);
cryptoStream.Close(); cryptoStream.Close();
msg.m_data = memoryStream.ToArray();
m_peer.Recycle(msg.m_data);
var arr = memoryStream.ToArray();
msg.m_data = arr;
msg.m_bitLength = arr.Length * 8;
} }
} }
} }
} }
catch catch (Exception ex)
{ {
m_peer.LogWarning("Decryption failed: " + ex);
return false; return false;
} }
return true; return true;

View File

@@ -9,7 +9,7 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// Triple DES encryption /// Triple DES encryption
/// </summary> /// </summary>
public class NetTripleDESEncryption : INetEncryption public class NetTripleDESEncryption : NetEncryption
{ {
private readonly byte[] m_key; private readonly byte[] m_key;
private readonly byte[] m_iv; private readonly byte[] m_iv;
@@ -19,7 +19,6 @@ namespace Lidgren.Network
static NetTripleDESEncryption() static NetTripleDESEncryption()
{ {
TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider(); TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
List<int> temp = new List<int>(); List<int> temp = new List<int>();
foreach (KeySizes keysize in tripleDES.LegalKeySizes) foreach (KeySizes keysize in tripleDES.LegalKeySizes)
@@ -51,7 +50,8 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// NetTriplsDESEncryption constructor /// NetTriplsDESEncryption constructor
/// </summary> /// </summary>
public NetTripleDESEncryption(byte[] key, byte[] iv) public NetTripleDESEncryption(NetPeer peer, byte[] key, byte[] iv)
: base(peer)
{ {
if (!s_keysizes.Contains(key.Length * 8)) if (!s_keysizes.Contains(key.Length * 8))
throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(s_keysizes))); throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(s_keysizes)));
@@ -67,7 +67,8 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// NetTriplsDESEncryption constructor /// NetTriplsDESEncryption constructor
/// </summary> /// </summary>
public NetTripleDESEncryption(string key, int bitsize) public NetTripleDESEncryption(NetPeer peer, string key, int bitsize)
: base(peer)
{ {
if (!s_keysizes.Contains(bitsize)) if (!s_keysizes.Contains(bitsize))
throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(s_keysizes))); throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(s_keysizes)));
@@ -92,19 +93,18 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// NetTriplsDESEncryption constructor /// NetTriplsDESEncryption constructor
/// </summary> /// </summary>
public NetTripleDESEncryption(string key) public NetTripleDESEncryption(NetPeer peer, string key)
: this(key, s_keysizes[0]) : this(peer, key, s_keysizes[0])
{ {
} }
/// <summary> /// <summary>
/// Encrypt outgoing message /// Encrypt outgoing message
/// </summary> /// </summary>
public bool Encrypt(NetOutgoingMessage msg) public override bool Encrypt(NetOutgoingMessage msg)
{ {
try try
{ {
// nested usings are fun!
using (TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new TripleDESCryptoServiceProvider { KeySize = m_bitSize, Mode = CipherMode.CBC }) using (TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new TripleDESCryptoServiceProvider { KeySize = m_bitSize, Mode = CipherMode.CBC })
{ {
using (ICryptoTransform cryptoTransform = tripleDESCryptoServiceProvider.CreateEncryptor(m_key, m_iv)) using (ICryptoTransform cryptoTransform = tripleDESCryptoServiceProvider.CreateEncryptor(m_key, m_iv))
@@ -114,14 +114,19 @@ namespace Lidgren.Network
{ {
cryptoStream.Write(msg.m_data, 0, msg.m_data.Length); cryptoStream.Write(msg.m_data, 0, msg.m_data.Length);
cryptoStream.Close(); cryptoStream.Close();
msg.m_data = memoryStream.ToArray();
m_peer.Recycle(msg.m_data);
var arr = memoryStream.ToArray();
msg.m_data = arr;
msg.m_bitLength = arr.Length * 8;
} }
} }
} }
} }
catch catch (Exception ex)
{ {
m_peer.LogWarning("Encryption failed: " + ex);
return false; return false;
} }
return true; return true;
@@ -130,7 +135,7 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// Decrypt incoming message /// Decrypt incoming message
/// </summary> /// </summary>
public bool Decrypt(NetIncomingMessage msg) public override bool Decrypt(NetIncomingMessage msg)
{ {
try try
{ {
@@ -144,14 +149,19 @@ namespace Lidgren.Network
{ {
cryptoStream.Write(msg.m_data, 0, msg.m_data.Length); cryptoStream.Write(msg.m_data, 0, msg.m_data.Length);
cryptoStream.Close(); cryptoStream.Close();
msg.m_data = memoryStream.ToArray();
m_peer.Recycle(msg.m_data);
var arr = memoryStream.ToArray();
msg.m_data = arr;
msg.m_bitLength = arr.Length * 8;
} }
} }
} }
} }
catch catch (Exception ex)
{ {
m_peer.LogWarning("Decryption failed: " + ex);
return false; return false;
} }
return true; return true;

View File

@@ -7,14 +7,15 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// Example class; not very good encryption /// Example class; not very good encryption
/// </summary> /// </summary>
public class NetXorEncryption : INetEncryption public class NetXorEncryption : NetEncryption
{ {
private byte[] m_key; private byte[] m_key;
/// <summary> /// <summary>
/// NetXorEncryption constructor /// NetXorEncryption constructor
/// </summary> /// </summary>
public NetXorEncryption(byte[] key) public NetXorEncryption(NetPeer peer, byte[] key)
: base(peer)
{ {
m_key = key; m_key = key;
} }
@@ -22,7 +23,8 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// NetXorEncryption constructor /// NetXorEncryption constructor
/// </summary> /// </summary>
public NetXorEncryption(string key) public NetXorEncryption(NetPeer peer, string key)
: base(peer)
{ {
m_key = Encoding.UTF8.GetBytes(key); m_key = Encoding.UTF8.GetBytes(key);
} }
@@ -30,7 +32,7 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// Encrypt an outgoing message /// Encrypt an outgoing message
/// </summary> /// </summary>
public bool Encrypt(NetOutgoingMessage msg) public override bool Encrypt(NetOutgoingMessage msg)
{ {
int numBytes = msg.LengthBytes; int numBytes = msg.LengthBytes;
for (int i = 0; i < numBytes; i++) for (int i = 0; i < numBytes; i++)
@@ -44,7 +46,7 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// Decrypt an incoming message /// Decrypt an incoming message
/// </summary> /// </summary>
public bool Decrypt(NetIncomingMessage msg) public override bool Decrypt(NetIncomingMessage msg)
{ {
int numBytes = msg.LengthBytes; int numBytes = msg.LengthBytes;
for (int i = 0; i < numBytes; i++) for (int i = 0; i < numBytes; i++)

View File

@@ -44,7 +44,8 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// 16 byte key /// 16 byte key
/// </summary> /// </summary>
public NetXtea(byte[] key, int rounds) public NetXtea(NetPeer peer, byte[] key, int rounds)
: base(peer)
{ {
if (key.Length < c_keySize) if (key.Length < c_keySize)
throw new NetException("Key too short!"); throw new NetException("Key too short!");
@@ -73,16 +74,16 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// 16 byte key /// 16 byte key
/// </summary> /// </summary>
public NetXtea(byte[] key) public NetXtea(NetPeer peer, byte[] key)
: this(key, 32) : this(peer, key, 32)
{ {
} }
/// <summary> /// <summary>
/// String to hash for key /// String to hash for key
/// </summary> /// </summary>
public NetXtea(string key) public NetXtea(NetPeer peer, string key)
: this(NetUtility.CreateSHA1Hash(key), 32) : this(peer, NetUtility.CreateSHA1Hash(key), 32)
{ {
} }

View File

@@ -59,13 +59,21 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Encryption\INetEncryption.cs" /> <Compile Include="Encryption\NetEncryption.cs" />
<Compile Include="Encryption\NetAESEncryption.cs" /> <Compile Include="Encryption\NetAESEncryption.cs" />
<Compile Include="Encryption\NetBlockEncryptionBase.cs" /> <Compile Include="Encryption\NetBlockEncryptionBase.cs" />
<Compile Include="Encryption\NetDESEncryption.cs" /> <Compile Include="Encryption\NetDESEncryption.cs">
<Compile Include="Encryption\NetRC2Encryption.cs" /> <SubType>Code</SubType>
<Compile Include="Encryption\NetTripleDESEncryption.cs" /> </Compile>
<Compile Include="Encryption\NetXorEncryption.cs" /> <Compile Include="Encryption\NetRC2Encryption.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Encryption\NetTripleDESEncryption.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Encryption\NetXorEncryption.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Encryption\NetXteaEncryption.cs" /> <Compile Include="Encryption\NetXteaEncryption.cs" />
<Compile Include="NamespaceDoc.cs" /> <Compile Include="NamespaceDoc.cs" />
<Compile Include="NetBigInteger.cs" /> <Compile Include="NetBigInteger.cs" />

View File

@@ -558,6 +558,7 @@ namespace Lidgren.Network
{ {
// not enough data // not enough data
#if DEBUG #if DEBUG
throw new NetException(c_readOverflowError); throw new NetException(c_readOverflowError);
#else #else
m_readPosition = m_bitLength; m_readPosition = m_bitLength;

View File

@@ -90,7 +90,7 @@ namespace Lidgren.Network
/// </summary> /// </summary>
/// <param name="encryption">The encryption algorithm used to encrypt the message</param> /// <param name="encryption">The encryption algorithm used to encrypt the message</param>
/// <returns>true on success</returns> /// <returns>true on success</returns>
public bool Decrypt(INetEncryption encryption) public bool Decrypt(NetEncryption encryption)
{ {
return encryption.Decrypt(this); return encryption.Decrypt(this);
} }

View File

@@ -116,7 +116,7 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// Encrypt this message using the provided algorithm; no more writing can be done before sending it or the message will be corrupt! /// Encrypt this message using the provided algorithm; no more writing can be done before sending it or the message will be corrupt!
/// </summary> /// </summary>
public bool Encrypt(INetEncryption encryption) public bool Encrypt(NetEncryption encryption)
{ {
return encryption.Encrypt(this); return encryption.Encrypt(this);
} }

View File

@@ -185,7 +185,7 @@ namespace Lidgren.Network
/// <summary> /// <summary>
/// Create XTEA symmetrical encryption object from sessionValue /// Create XTEA symmetrical encryption object from sessionValue
/// </summary> /// </summary>
public static NetXtea CreateEncryption(byte[] sessionValue) public static NetXtea CreateEncryption(NetPeer peer, byte[] sessionValue)
{ {
var sha = GetHashAlgorithm(); var sha = GetHashAlgorithm();
var hash = sha.ComputeHash(sessionValue); var hash = sha.ComputeHash(sessionValue);
@@ -198,7 +198,7 @@ namespace Lidgren.Network
key[i] ^= hash[i + (j * 16)]; key[i] ^= hash[i + (j * 16)];
} }
return new NetXtea(key); return new NetXtea(peer, key);
} }
} }
} }