1
0
mirror of https://github.com/lidgren/lidgren-network-gen3.git synced 2026-05-06 10:21:09 +09:00

XTEA fixed; Message encryption working

This commit is contained in:
lidgren
2010-05-23 10:48:58 +00:00
parent 046ca06ec6
commit e63c52bd7d
7 changed files with 72 additions and 73 deletions

View File

@@ -2,6 +2,7 @@
using System.Text;
using Lidgren.Network;
using System.Security;
namespace UnitTests
{
@@ -14,20 +15,23 @@ namespace UnitTests
//
NetXtea xtea = new NetXtea(NetSha.Hash(Encoding.ASCII.GetBytes("TopSecret")));
byte[] test = new byte[16];
NetRandom.Instance.NextBytes(test);
byte[] original = new byte[16];
NetRandom.Instance.NextBytes(original);
byte[] encrypted = new byte[test.Length];
xtea.EncryptBlock(test, 0, encrypted, 0);
byte[] encrypted = new byte[original.Length];
xtea.EncryptBlock(original, 0, encrypted, 0);
xtea.EncryptBlock(original, 8, encrypted, 8);
byte[] decrypted = new byte[test.Length];
byte[] decrypted = new byte[original.Length];
xtea.DecryptBlock(encrypted, 0, decrypted, 0);
xtea.DecryptBlock(encrypted, 8, decrypted, 8);
// compare!
for (int i = 0; i < test.Length; i++)
if (test[i] != decrypted[i])
for (int i = 0; i < original.Length; i++)
if (original[i] != decrypted[i])
throw new NetException("XTEA fail!");
Console.WriteLine("XTEA OK");
NetOutgoingMessage om = peer.CreateMessage();
om.Write("Hallon");
@@ -35,16 +39,11 @@ namespace UnitTests
om.Write(5, 5);
om.Write(true);
om.Write("kokos");
Console.WriteLine("Pre encryption: " + NetUtility.ToHexString(om.PeekDataBuffer()));
om.Encrypt(xtea);
Console.WriteLine("Post encryption: " + NetUtility.ToHexString(om.PeekDataBuffer()));
// convert to incoming message
NetIncomingMessage im = Program.CreateIncomingMessage(om.PeekDataBuffer(), om.LengthBits);
Console.WriteLine("Pre decryption: " + NetUtility.ToHexString(im.PeekDataBuffer()));
im.Decrypt(xtea);
Console.WriteLine("Post decryption: " + NetUtility.ToHexString(im.PeekDataBuffer()));
if (im.ReadString() != "Hallon")
throw new NetException("fail");
@@ -57,7 +56,7 @@ namespace UnitTests
if (im.ReadString() != "kokos")
throw new NetException("fail");
Console.WriteLine("Message encryption OK");
byte[] salt = NetUtility.ToByteArray("e6fb7e23f001f3e6c081"); // s
byte[] verifier = NetSRP.ComputePasswordVerifier("user", "password", salt);