1
0
mirror of https://github.com/lidgren/lidgren-network-gen3.git synced 2026-05-16 07:06: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>
/// AES encryption
/// </summary>
public class NetAESEncryption : INetEncryption
public class NetAESEncryption : NetEncryption
{
private readonly byte[] m_key;
private readonly byte[] m_iv;
@@ -52,7 +52,8 @@ namespace Lidgren.Network
/// <summary>
/// NetAESEncryption constructor
/// </summary>
public NetAESEncryption(byte[] key, byte[] iv)
public NetAESEncryption(NetPeer peer, byte[] key, byte[] iv)
: base(peer)
{
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)));
@@ -68,7 +69,8 @@ namespace Lidgren.Network
/// <summary>
/// NetAESEncryption constructor
/// </summary>
public NetAESEncryption(string key, int bitsize)
public NetAESEncryption(NetPeer peer, string key, int bitsize)
: base(peer)
{
if (!s_keysizes.Contains(bitsize))
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>
/// NetAESEncryption constructor
/// </summary>
public NetAESEncryption(string key)
: this(key, s_keysizes[0])
public NetAESEncryption(NetPeer peer, string key)
: this(peer, key, s_keysizes[0])
{
}
/// <summary>
/// Encrypt outgoing message
/// </summary>
public bool Encrypt(NetOutgoingMessage msg)
public override bool Encrypt(NetOutgoingMessage msg)
{
#if !IOS && !__ANDROID__ && !UNITY_4_5
try
{
// nested usings are fun!
using (AesCryptoServiceProvider aesCryptoServiceProvider = new AesCryptoServiceProvider { KeySize = m_bitSize, Mode = CipherMode.CBC })
{
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.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 true;
@@ -135,7 +141,7 @@ namespace Lidgren.Network
/// <summary>
/// Decrypt incoming message
/// </summary>
public bool Decrypt(NetIncomingMessage msg)
public override bool Decrypt(NetIncomingMessage msg)
{
#if !IOS && !__ANDROID__ && !UNITY_4_5
try
@@ -150,14 +156,19 @@ namespace Lidgren.Network
{
cryptoStream.Write(msg.m_data, 0, msg.m_data.Length);
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 true;

View File

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

View File

@@ -9,7 +9,7 @@ namespace Lidgren.Network
/// <summary>
/// DES encryption
/// </summary>
public class NetDESEncryption : INetEncryption
public class NetDESEncryption : NetEncryption
{
private readonly byte[] m_key;
private readonly byte[] m_iv;
@@ -51,13 +51,14 @@ namespace Lidgren.Network
/// <summary>
/// NetDESEncryption constructor
/// </summary>
public NetDESEncryption(byte[] key, byte[] iv)
public NetDESEncryption(NetPeer peer, byte[] key, byte[] iv)
: base(peer)
{
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)));
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_iv = iv;
@@ -67,7 +68,8 @@ namespace Lidgren.Network
/// <summary>
/// NetDESEncryption constructor
/// </summary>
public NetDESEncryption(string key, int bitsize)
public NetDESEncryption(NetPeer peer, string key, int bitsize)
: base(peer)
{
if (!s_keysizes.Contains(bitsize))
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>
/// NetDESEncryption constructor
/// </summary>
public NetDESEncryption(string key)
: this(key, s_keysizes[0])
public NetDESEncryption(NetPeer peer, string key)
: this(peer, key, s_keysizes[0])
{
}
/// <summary>
/// Encrypt outgoing message
/// </summary>
public bool Encrypt(NetOutgoingMessage msg)
public override bool Encrypt(NetOutgoingMessage msg)
{
try
{
// nested usings are fun!
using (DESCryptoServiceProvider desCryptoServiceProvider = new DESCryptoServiceProvider { KeySize = m_bitSize, Mode = CipherMode.CBC })
{
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.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 true;
@@ -130,11 +136,10 @@ namespace Lidgren.Network
/// <summary>
/// Decrypt incoming message
/// </summary>
public bool Decrypt(NetIncomingMessage msg)
public override bool Decrypt(NetIncomingMessage msg)
{
try
{
// nested usings are fun!
using (DESCryptoServiceProvider desCryptoServiceProvider = new DESCryptoServiceProvider { KeySize = m_bitSize, Mode = CipherMode.CBC })
{
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.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 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>
/// RC2 encryption
/// </summary>
public class NetRC2Encryption : INetEncryption
public class NetRC2Encryption : NetEncryption
{
private readonly byte[] m_key;
private readonly byte[] m_iv;
@@ -51,7 +51,8 @@ namespace Lidgren.Network
/// <summary>
/// NetRC2Encryption constructor
/// </summary>
public NetRC2Encryption(byte[] key, byte[] iv)
public NetRC2Encryption(NetPeer peer, byte[] key, byte[] iv)
: base(peer)
{
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)));
@@ -67,11 +68,12 @@ namespace Lidgren.Network
/// <summary>
/// NetRC2Encryption constructor
/// </summary>
public NetRC2Encryption(string key, int bitsize)
public NetRC2Encryption(NetPeer peer, string key, int bitsize)
: base(peer)
{
if (!s_keysizes.Contains(bitsize))
throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(s_keysizes)));
byte[] entropy = Encoding.UTF32.GetBytes(key);
// I know hardcoding salts is bad, but in this case I think it is acceptable.
HMACSHA512 hmacsha512 = new HMACSHA512(Convert.FromBase64String("i88NEiez3c50bHqr3YGasDc4p8jRrxJAaiRiqixpvp4XNAStP5YNoC2fXnWkURtkha6M8yY901Gj07IRVIRyGL=="));
@@ -92,20 +94,20 @@ namespace Lidgren.Network
/// <summary>
/// NetRC2Encryption constructor
/// </summary>
/// <param name="peer"></param>
/// <param name="key"></param>
public NetRC2Encryption(string key)
: this(key, s_keysizes[0])
public NetRC2Encryption(NetPeer peer, string key)
: this(peer, key, s_keysizes[0])
{
}
/// <summary>
/// Encrypt outgoing message
/// </summary>
public bool Encrypt(NetOutgoingMessage msg)
public override bool Encrypt(NetOutgoingMessage msg)
{
try
{
// nested usings are fun!
using (RC2CryptoServiceProvider rc2CryptoServiceProvider = new RC2CryptoServiceProvider { KeySize = m_bitSize, Mode = CipherMode.CBC })
{
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.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 true;
@@ -131,7 +138,7 @@ namespace Lidgren.Network
/// <summary>
/// Decrypt incoming message
/// </summary>
public bool Decrypt(NetIncomingMessage msg)
public override bool Decrypt(NetIncomingMessage msg)
{
try
{
@@ -145,14 +152,19 @@ namespace Lidgren.Network
{
cryptoStream.Write(msg.m_data, 0, msg.m_data.Length);
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 true;

View File

@@ -9,7 +9,7 @@ namespace Lidgren.Network
/// <summary>
/// Triple DES encryption
/// </summary>
public class NetTripleDESEncryption : INetEncryption
public class NetTripleDESEncryption : NetEncryption
{
private readonly byte[] m_key;
private readonly byte[] m_iv;
@@ -19,7 +19,6 @@ namespace Lidgren.Network
static NetTripleDESEncryption()
{
TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
List<int> temp = new List<int>();
foreach (KeySizes keysize in tripleDES.LegalKeySizes)
@@ -51,7 +50,8 @@ namespace Lidgren.Network
/// <summary>
/// NetTriplsDESEncryption constructor
/// </summary>
public NetTripleDESEncryption(byte[] key, byte[] iv)
public NetTripleDESEncryption(NetPeer peer, byte[] key, byte[] iv)
: base(peer)
{
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)));
@@ -67,7 +67,8 @@ namespace Lidgren.Network
/// <summary>
/// NetTriplsDESEncryption constructor
/// </summary>
public NetTripleDESEncryption(string key, int bitsize)
public NetTripleDESEncryption(NetPeer peer, string key, int bitsize)
: base(peer)
{
if (!s_keysizes.Contains(bitsize))
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>
/// NetTriplsDESEncryption constructor
/// </summary>
public NetTripleDESEncryption(string key)
: this(key, s_keysizes[0])
public NetTripleDESEncryption(NetPeer peer, string key)
: this(peer, key, s_keysizes[0])
{
}
/// <summary>
/// Encrypt outgoing message
/// </summary>
public bool Encrypt(NetOutgoingMessage msg)
public override bool Encrypt(NetOutgoingMessage msg)
{
try
{
// nested usings are fun!
using (TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new TripleDESCryptoServiceProvider { KeySize = m_bitSize, Mode = CipherMode.CBC })
{
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.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 true;
@@ -130,7 +135,7 @@ namespace Lidgren.Network
/// <summary>
/// Decrypt incoming message
/// </summary>
public bool Decrypt(NetIncomingMessage msg)
public override bool Decrypt(NetIncomingMessage msg)
{
try
{
@@ -144,14 +149,19 @@ namespace Lidgren.Network
{
cryptoStream.Write(msg.m_data, 0, msg.m_data.Length);
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 true;

View File

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

View File

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