You've already forked lidgren-network-gen3
mirror of
https://github.com/lidgren/lidgren-network-gen3.git
synced 2026-05-07 19:01:09 +09:00
Various encryption algorithms added; thanks shawn andrew rose
This commit is contained in:
175
Lidgren.Network/Encryption/NetAESEncryption.cs
Normal file
175
Lidgren.Network/Encryption/NetAESEncryption.cs
Normal file
@@ -0,0 +1,175 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace Lidgren.Network
|
||||
{
|
||||
/// <summary>
|
||||
/// AES encryption
|
||||
/// </summary>
|
||||
public class NetAESEncryption : INetEncryption
|
||||
{
|
||||
private readonly byte[] m_key;
|
||||
private readonly byte[] m_iv;
|
||||
private readonly int m_bitSize;
|
||||
private static readonly List<int> m_keysizes;
|
||||
private static readonly List<int> m_blocksizes;
|
||||
|
||||
static NetAESEncryption()
|
||||
{
|
||||
|
||||
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
|
||||
List<int> temp = new List<int>();
|
||||
foreach (KeySizes keysize in aes.LegalKeySizes)
|
||||
{
|
||||
for (int i = keysize.MinSize; i <= keysize.MaxSize; i += keysize.SkipSize)
|
||||
{
|
||||
if (!temp.Contains(i))
|
||||
temp.Add(i);
|
||||
if (i == keysize.MaxSize)
|
||||
break;
|
||||
}
|
||||
}
|
||||
m_keysizes = temp;
|
||||
temp = new List<int>();
|
||||
foreach (KeySizes keysize in aes.LegalBlockSizes)
|
||||
{
|
||||
for (int i = keysize.MinSize; i <= keysize.MaxSize; i += keysize.SkipSize)
|
||||
{
|
||||
|
||||
if (!temp.Contains(i))
|
||||
temp.Add(i);
|
||||
if (i == keysize.MaxSize)
|
||||
break;
|
||||
}
|
||||
}
|
||||
m_blocksizes = temp;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// NetAESEncryption constructor
|
||||
/// </summary>
|
||||
public NetAESEncryption(byte[] key, byte[] iv)
|
||||
{
|
||||
if (!m_keysizes.Contains(key.Length * 8))
|
||||
{
|
||||
string lengths = m_keysizes.Aggregate("", (current, i) => current + string.Format("{0}, ", i));
|
||||
lengths = lengths.Remove(lengths.Length - 3);
|
||||
throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", lengths));
|
||||
}
|
||||
|
||||
if (!m_blocksizes.Contains(iv.Length * 8))
|
||||
{
|
||||
string lengths = m_blocksizes.Aggregate("", (current, i) => current + string.Format("{0}, ", i));
|
||||
lengths = lengths.Remove(lengths.Length - 3);
|
||||
throw new NetException(string.Format("Not a valid iv size. (Valid values are: {0})", lengths));
|
||||
}
|
||||
m_key = key;
|
||||
m_iv = iv;
|
||||
m_bitSize = m_key.Length * 8;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// NetAESEncryption constructor
|
||||
/// </summary>
|
||||
public NetAESEncryption(string key, int bitsize)
|
||||
{
|
||||
if (!m_keysizes.Contains(bitsize))
|
||||
{
|
||||
string lengths = m_keysizes.Aggregate("", (current, i) => current + string.Format("{0}, ", i));
|
||||
lengths = lengths.Remove(lengths.Length - 3);
|
||||
throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", lengths));
|
||||
}
|
||||
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=="));
|
||||
hmacsha512.Initialize();
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
entropy = hmacsha512.ComputeHash(entropy);
|
||||
}
|
||||
int keylen = bitsize / 8;
|
||||
m_key = new byte[keylen];
|
||||
Buffer.BlockCopy(entropy, 0, m_key, 0, keylen);
|
||||
m_iv = new byte[m_blocksizes[0] / 8];
|
||||
|
||||
Buffer.BlockCopy(entropy, entropy.Length - m_iv.Length - 1, m_iv, 0, m_iv.Length);
|
||||
m_bitSize = bitsize;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// NetAESEncryption constructor
|
||||
/// </summary>
|
||||
public NetAESEncryption(string key)
|
||||
: this(key, m_keysizes.Max())
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Encrypt outgoing message
|
||||
/// </summary>
|
||||
public bool Encrypt(NetOutgoingMessage msg)
|
||||
{
|
||||
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))
|
||||
{
|
||||
using (MemoryStream memoryStream = new MemoryStream())
|
||||
{
|
||||
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoTransform,
|
||||
CryptoStreamMode.Write))
|
||||
{
|
||||
cryptoStream.Write(msg.m_data, 0, msg.m_data.Length);
|
||||
}
|
||||
msg.m_data = memoryStream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decrypt incoming message
|
||||
/// </summary>
|
||||
public bool Decrypt(NetIncomingMessage msg)
|
||||
{
|
||||
try
|
||||
{
|
||||
// nested usings are fun!
|
||||
using (AesCryptoServiceProvider aesCryptoServiceProvider = new AesCryptoServiceProvider { KeySize = m_bitSize, Mode = CipherMode.CBC })
|
||||
{
|
||||
using (ICryptoTransform cryptoTransform = aesCryptoServiceProvider.CreateDecryptor(m_key, m_iv))
|
||||
{
|
||||
using (MemoryStream memoryStream = new MemoryStream())
|
||||
{
|
||||
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoTransform,
|
||||
CryptoStreamMode.Write))
|
||||
{
|
||||
cryptoStream.Write(msg.m_data, 0, msg.m_data.Length);
|
||||
}
|
||||
msg.m_data = memoryStream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
175
Lidgren.Network/Encryption/NetDESEncryption.cs
Normal file
175
Lidgren.Network/Encryption/NetDESEncryption.cs
Normal file
@@ -0,0 +1,175 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace Lidgren.Network
|
||||
{
|
||||
/// <summary>
|
||||
/// DES encryption
|
||||
/// </summary>
|
||||
public class NetDESEncryption : INetEncryption
|
||||
{
|
||||
private readonly byte[] m_key;
|
||||
private readonly byte[] m_iv;
|
||||
private readonly int m_bitSize;
|
||||
private static readonly List<int> m_keysizes;
|
||||
private static readonly List<int> m_blocksizes;
|
||||
|
||||
static NetDESEncryption()
|
||||
{
|
||||
|
||||
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
|
||||
List<int> temp = new List<int>();
|
||||
foreach (KeySizes keysize in des.LegalKeySizes)
|
||||
{
|
||||
for (int i = keysize.MinSize; i <= keysize.MaxSize; i += keysize.SkipSize)
|
||||
{
|
||||
if (!temp.Contains(i))
|
||||
temp.Add(i);
|
||||
if (i == keysize.MaxSize)
|
||||
break;
|
||||
}
|
||||
}
|
||||
m_keysizes = temp;
|
||||
temp = new List<int>();
|
||||
foreach (KeySizes keysize in des.LegalBlockSizes)
|
||||
{
|
||||
for (int i = keysize.MinSize; i <= keysize.MaxSize; i += keysize.SkipSize)
|
||||
{
|
||||
|
||||
if (!temp.Contains(i))
|
||||
temp.Add(i);
|
||||
if (i == keysize.MaxSize)
|
||||
break;
|
||||
}
|
||||
}
|
||||
m_blocksizes = temp;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// NetDESEncryption constructor
|
||||
/// </summary>
|
||||
public NetDESEncryption(byte[] key, byte[] iv)
|
||||
{
|
||||
if (!m_keysizes.Contains(key.Length * 8))
|
||||
{
|
||||
string lengths = m_keysizes.Aggregate("", (current, i) => current + string.Format("{0}, ", i));
|
||||
lengths = lengths.Remove(lengths.Length - 3);
|
||||
throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", lengths));
|
||||
}
|
||||
|
||||
if (!m_blocksizes.Contains(iv.Length * 8))
|
||||
{
|
||||
string lengths = m_blocksizes.Aggregate("", (current, i) => current + string.Format("{0}, ", i));
|
||||
lengths = lengths.Remove(lengths.Length - 3);
|
||||
throw new NetException(string.Format("Not a valid iv size. (Valid values are: {0})", lengths));
|
||||
}
|
||||
m_key = key;
|
||||
m_iv = iv;
|
||||
m_bitSize = m_key.Length * 8;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// NetDESEncryption constructor
|
||||
/// </summary>
|
||||
public NetDESEncryption(string key, int bitsize)
|
||||
{
|
||||
if (!m_keysizes.Contains(bitsize))
|
||||
{
|
||||
string lengths = m_keysizes.Aggregate("", (current, i) => current + string.Format("{0}, ", i));
|
||||
lengths = lengths.Remove(lengths.Length - 3);
|
||||
throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", lengths));
|
||||
}
|
||||
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=="));
|
||||
hmacsha512.Initialize();
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
entropy = hmacsha512.ComputeHash(entropy);
|
||||
}
|
||||
int keylen = bitsize / 8;
|
||||
m_key = new byte[keylen];
|
||||
Buffer.BlockCopy(entropy, 0, m_key, 0, keylen);
|
||||
m_iv = new byte[m_blocksizes[0] / 8];
|
||||
|
||||
Buffer.BlockCopy(entropy, entropy.Length - m_iv.Length - 1, m_iv, 0, m_iv.Length);
|
||||
m_bitSize = bitsize;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// NetDESEncryption constructor
|
||||
/// </summary>
|
||||
public NetDESEncryption(string key)
|
||||
: this(key, m_keysizes.Max())
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Encrypt outgoing message
|
||||
/// </summary>
|
||||
public 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))
|
||||
{
|
||||
using (MemoryStream memoryStream = new MemoryStream())
|
||||
{
|
||||
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoTransform,
|
||||
CryptoStreamMode.Write))
|
||||
{
|
||||
cryptoStream.Write(msg.m_data, 0, msg.m_data.Length);
|
||||
}
|
||||
msg.m_data = memoryStream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decrypt incoming message
|
||||
/// </summary>
|
||||
public 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))
|
||||
{
|
||||
using (MemoryStream memoryStream = new MemoryStream())
|
||||
{
|
||||
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoTransform,
|
||||
CryptoStreamMode.Write))
|
||||
{
|
||||
cryptoStream.Write(msg.m_data, 0, msg.m_data.Length);
|
||||
}
|
||||
msg.m_data = memoryStream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
176
Lidgren.Network/Encryption/NetRC2Encryption.cs
Normal file
176
Lidgren.Network/Encryption/NetRC2Encryption.cs
Normal file
@@ -0,0 +1,176 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace Lidgren.Network
|
||||
{
|
||||
/// <summary>
|
||||
/// RC2 encryption
|
||||
/// </summary>
|
||||
public class NetRC2Encryption : INetEncryption
|
||||
{
|
||||
private readonly byte[] m_key;
|
||||
private readonly byte[] m_iv;
|
||||
private readonly int m_bitSize;
|
||||
private static readonly List<int> m_keysizes;
|
||||
private static readonly List<int> m_blocksizes;
|
||||
|
||||
static NetRC2Encryption()
|
||||
{
|
||||
|
||||
RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider();
|
||||
List<int> temp = new List<int>();
|
||||
foreach (KeySizes keysize in rc2.LegalKeySizes)
|
||||
{
|
||||
for (int i = keysize.MinSize; i <= keysize.MaxSize; i += keysize.SkipSize)
|
||||
{
|
||||
if (!temp.Contains(i))
|
||||
temp.Add(i);
|
||||
if (i == keysize.MaxSize)
|
||||
break;
|
||||
}
|
||||
}
|
||||
m_keysizes = temp;
|
||||
temp = new List<int>();
|
||||
foreach (KeySizes keysize in rc2.LegalBlockSizes)
|
||||
{
|
||||
for (int i = keysize.MinSize; i <= keysize.MaxSize; i += keysize.SkipSize)
|
||||
{
|
||||
|
||||
if (!temp.Contains(i))
|
||||
temp.Add(i);
|
||||
if (i == keysize.MaxSize)
|
||||
break;
|
||||
}
|
||||
}
|
||||
m_blocksizes = temp;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// NetRC2Encryption constructor
|
||||
/// </summary>
|
||||
public NetRC2Encryption(byte[] key, byte[] iv)
|
||||
{
|
||||
if (!m_keysizes.Contains(key.Length * 8))
|
||||
{
|
||||
string lengths = m_keysizes.Aggregate("", (current, i) => current + string.Format("{0}, ", i));
|
||||
lengths = lengths.Remove(lengths.Length - 3);
|
||||
throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", lengths));
|
||||
}
|
||||
|
||||
if (!m_blocksizes.Contains(iv.Length * 8))
|
||||
{
|
||||
string lengths = m_blocksizes.Aggregate("", (current, i) => current + string.Format("{0}, ", i));
|
||||
lengths = lengths.Remove(lengths.Length - 3);
|
||||
throw new NetException(string.Format("Not a valid iv size. (Valid values are: {0})", lengths));
|
||||
}
|
||||
m_key = key;
|
||||
m_iv = iv;
|
||||
m_bitSize = m_key.Length * 8;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// NetRC2Encryption constructor
|
||||
/// </summary>
|
||||
public NetRC2Encryption(string key, int bitsize)
|
||||
{
|
||||
if (!m_keysizes.Contains(bitsize))
|
||||
{
|
||||
string lengths = m_keysizes.Aggregate("", (current, i) => current + string.Format("{0}, ", i));
|
||||
lengths = lengths.Remove(lengths.Length - 3);
|
||||
throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", lengths));
|
||||
}
|
||||
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=="));
|
||||
hmacsha512.Initialize();
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
entropy = hmacsha512.ComputeHash(entropy);
|
||||
}
|
||||
int keylen = bitsize / 8;
|
||||
m_key = new byte[keylen];
|
||||
Buffer.BlockCopy(entropy, 0, m_key, 0, keylen);
|
||||
m_iv = new byte[m_blocksizes[0] / 8];
|
||||
|
||||
Buffer.BlockCopy(entropy, entropy.Length - m_iv.Length - 1, m_iv, 0, m_iv.Length);
|
||||
m_bitSize = bitsize;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// NetRC2Encryption constructor
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
public NetRC2Encryption(string key)
|
||||
: this(key, m_keysizes.Max())
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Encrypt outgoing message
|
||||
/// </summary>
|
||||
public 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))
|
||||
{
|
||||
using (MemoryStream memoryStream = new MemoryStream())
|
||||
{
|
||||
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoTransform,
|
||||
CryptoStreamMode.Write))
|
||||
{
|
||||
cryptoStream.Write(msg.m_data, 0, msg.m_data.Length);
|
||||
}
|
||||
msg.m_data = memoryStream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decrypt incoming message
|
||||
/// </summary>
|
||||
public bool Decrypt(NetIncomingMessage msg)
|
||||
{
|
||||
try
|
||||
{
|
||||
// nested usings are fun!
|
||||
using (RC2CryptoServiceProvider rc2CryptoServiceProvider = new RC2CryptoServiceProvider { KeySize = m_bitSize, Mode = CipherMode.CBC })
|
||||
{
|
||||
using (ICryptoTransform cryptoTransform = rc2CryptoServiceProvider.CreateDecryptor(m_key, m_iv))
|
||||
{
|
||||
using (MemoryStream memoryStream = new MemoryStream())
|
||||
{
|
||||
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoTransform,
|
||||
CryptoStreamMode.Write))
|
||||
{
|
||||
cryptoStream.Write(msg.m_data, 0, msg.m_data.Length);
|
||||
}
|
||||
msg.m_data = memoryStream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
175
Lidgren.Network/Encryption/NetTripleDESEncryption.cs
Normal file
175
Lidgren.Network/Encryption/NetTripleDESEncryption.cs
Normal file
@@ -0,0 +1,175 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace Lidgren.Network
|
||||
{
|
||||
/// <summary>
|
||||
/// Triple DES encryption
|
||||
/// </summary>
|
||||
public class NetTripleDESEncryption : INetEncryption
|
||||
{
|
||||
private readonly byte[] m_key;
|
||||
private readonly byte[] m_iv;
|
||||
private readonly int m_bitSize;
|
||||
private static readonly List<int> m_keysizes;
|
||||
private static readonly List<int> m_blocksizes;
|
||||
|
||||
static NetTripleDESEncryption()
|
||||
{
|
||||
|
||||
TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
|
||||
List<int> temp = new List<int>();
|
||||
foreach (KeySizes keysize in tripleDES.LegalKeySizes)
|
||||
{
|
||||
for (int i = keysize.MinSize; i <= keysize.MaxSize; i += keysize.SkipSize)
|
||||
{
|
||||
if (!temp.Contains(i))
|
||||
temp.Add(i);
|
||||
if (i == keysize.MaxSize)
|
||||
break;
|
||||
}
|
||||
}
|
||||
m_keysizes = temp;
|
||||
temp = new List<int>();
|
||||
foreach (KeySizes keysize in tripleDES.LegalBlockSizes)
|
||||
{
|
||||
for (int i = keysize.MinSize; i <= keysize.MaxSize; i += keysize.SkipSize)
|
||||
{
|
||||
|
||||
if (!temp.Contains(i))
|
||||
temp.Add(i);
|
||||
if (i == keysize.MaxSize)
|
||||
break;
|
||||
}
|
||||
}
|
||||
m_blocksizes = temp;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// NetTriplsDESEncryption constructor
|
||||
/// </summary>
|
||||
public NetTripleDESEncryption(byte[] key, byte[] iv)
|
||||
{
|
||||
if (!m_keysizes.Contains(key.Length * 8))
|
||||
{
|
||||
string lengths = m_keysizes.Aggregate("", (current, i) => current + string.Format("{0}, ", i));
|
||||
lengths = lengths.Remove(lengths.Length - 3);
|
||||
throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", lengths));
|
||||
}
|
||||
|
||||
if (!m_blocksizes.Contains(iv.Length * 8))
|
||||
{
|
||||
string lengths = m_blocksizes.Aggregate("", (current, i) => current + string.Format("{0}, ", i));
|
||||
lengths = lengths.Remove(lengths.Length - 3);
|
||||
throw new NetException(string.Format("Not a valid iv size. (Valid values are: {0})", lengths));
|
||||
}
|
||||
m_key = key;
|
||||
m_iv = iv;
|
||||
m_bitSize = m_key.Length * 8;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// NetTriplsDESEncryption constructor
|
||||
/// </summary>
|
||||
public NetTripleDESEncryption(string key, int bitsize)
|
||||
{
|
||||
if (!m_keysizes.Contains(bitsize))
|
||||
{
|
||||
string lengths = m_keysizes.Aggregate("", (current, i) => current + string.Format("{0}, ", i));
|
||||
lengths = lengths.Remove(lengths.Length - 3);
|
||||
throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", lengths));
|
||||
}
|
||||
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=="));
|
||||
hmacsha512.Initialize();
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
entropy = hmacsha512.ComputeHash(entropy);
|
||||
}
|
||||
int keylen = bitsize / 8;
|
||||
m_key = new byte[keylen];
|
||||
Buffer.BlockCopy(entropy, 0, m_key, 0, keylen);
|
||||
m_iv = new byte[m_blocksizes[0] / 8];
|
||||
|
||||
Buffer.BlockCopy(entropy, entropy.Length - m_iv.Length - 1, m_iv, 0, m_iv.Length);
|
||||
m_bitSize = bitsize;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// NetTriplsDESEncryption constructor
|
||||
/// </summary>
|
||||
public NetTripleDESEncryption(string key)
|
||||
: this(key, m_keysizes.Max())
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Encrypt outgoing message
|
||||
/// </summary>
|
||||
public 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))
|
||||
{
|
||||
using (MemoryStream memoryStream = new MemoryStream())
|
||||
{
|
||||
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoTransform,
|
||||
CryptoStreamMode.Write))
|
||||
{
|
||||
cryptoStream.Write(msg.m_data, 0, msg.m_data.Length);
|
||||
}
|
||||
msg.m_data = memoryStream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decrypt incoming message
|
||||
/// </summary>
|
||||
public bool Decrypt(NetIncomingMessage msg)
|
||||
{
|
||||
try
|
||||
{
|
||||
// nested usings are fun!
|
||||
using (TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new TripleDESCryptoServiceProvider { KeySize = m_bitSize, Mode = CipherMode.CBC })
|
||||
{
|
||||
using (ICryptoTransform cryptoTransform = tripleDESCryptoServiceProvider.CreateDecryptor(m_key, m_iv))
|
||||
{
|
||||
using (MemoryStream memoryStream = new MemoryStream())
|
||||
{
|
||||
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoTransform,
|
||||
CryptoStreamMode.Write))
|
||||
{
|
||||
cryptoStream.Write(msg.m_data, 0, msg.m_data.Length);
|
||||
}
|
||||
msg.m_data = memoryStream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -72,9 +72,13 @@
|
||||
<Compile Include="NetConnectionStatus.cs" />
|
||||
<Compile Include="NetConstants.cs" />
|
||||
<Compile Include="NetDeliveryMethod.cs" />
|
||||
<Compile Include="Encryption\NetAESEncryption.cs" />
|
||||
<Compile Include="NetException.cs" />
|
||||
<Compile Include="Encryption\NetDESEncryption.cs" />
|
||||
<Compile Include="Encryption\NetRC2Encryption.cs" />
|
||||
<Compile Include="NetConnection.MTU.cs" />
|
||||
<Compile Include="NetFragmentationHelper.cs" />
|
||||
<Compile Include="Encryption\NetTripleDESEncryption.cs" />
|
||||
<Compile Include="NetIncomingMessage.cs" />
|
||||
<Compile Include="NetIncomingMessage.Peek.cs" />
|
||||
<Compile Include="NetIncomingMessage.Read.cs" />
|
||||
|
||||
@@ -12,12 +12,16 @@ namespace UnitTests
|
||||
public static void Run(NetPeer peer)
|
||||
{
|
||||
//
|
||||
// Test XTEA
|
||||
// Test encryption
|
||||
//
|
||||
List<INetEncryption> algos = new List<INetEncryption>();
|
||||
|
||||
algos.Add(new NetXorEncryption("TopSecret"));
|
||||
algos.Add(new NetXtea("TopSecret"));
|
||||
algos.Add(new NetAESEncryption("TopSecret"));
|
||||
algos.Add(new NetRC2Encryption("TopSecret"));
|
||||
algos.Add(new NetDESEncryption("TopSecret"));
|
||||
algos.Add(new NetTripleDESEncryption("TopSecret"));
|
||||
|
||||
foreach (var algo in algos)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user