Skip to content

Commit 14d0fb0

Browse files
committed
Move AesEncryptLayer to samples
1 parent ba0e642 commit 14d0fb0

File tree

4 files changed

+58
-134
lines changed

4 files changed

+58
-134
lines changed
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
using System;
1+
using LiteNetLib.Layers;
2+
using System;
23
using System.Security.Cryptography;
34

4-
namespace LiteNetLib.Layers
5+
namespace LibSample
56
{
67
/// <summary>
78
/// Uses AES encryption in CBC mode. Make sure you handle your key properly.

LibSample/AesEncryptionTest.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Linq;
3+
using System.Security.Cryptography;
4+
using System.Text;
5+
6+
namespace LibSample
7+
{
8+
class AesEncryptionTest
9+
{
10+
public void Run() => AesLayerEncryptDecrypt();
11+
12+
private void AesLayerEncryptDecrypt()
13+
{
14+
var keyGen = RandomNumberGenerator.Create();
15+
byte[] key = new byte[AesEncryptLayer.KeySizeInBytes];
16+
keyGen.GetBytes(key);
17+
const string testData = "This text is long enough to need multiple blocks to encrypt";
18+
19+
var outboudLayer = new AesEncryptLayer(key);
20+
byte[] outbound = Encoding.ASCII.GetBytes(testData);
21+
int lengthOfPacket = outbound.Length;
22+
int start = 0;
23+
int length = outbound.Length;
24+
outboudLayer.ProcessOutBoundPacket(ref outbound, ref start, ref length);
25+
26+
int minLenth = lengthOfPacket + AesEncryptLayer.BlockSizeInBytes;
27+
int maxLength = lengthOfPacket + outboudLayer.ExtraPacketSizeForLayer;
28+
if (length < minLenth || length > maxLength)
29+
{
30+
throw new Exception("Packet length out of bounds");
31+
}
32+
33+
var inboundLayer = new AesEncryptLayer(key);
34+
//Copy array so we dont read and write to same array
35+
byte[] inboundData = new byte[outbound.Length];
36+
outbound.CopyTo(inboundData, 0);
37+
inboundLayer.ProcessInboundPacket(ref inboundData, ref length);
38+
39+
Console.WriteLine(Encoding.ASCII.GetString(inboundData, 0, length));
40+
byte[] expectedPlaintext = Encoding.ASCII.GetBytes(testData);
41+
42+
var isEqualLength = expectedPlaintext.Length == length;
43+
var areContentEqual = expectedPlaintext.SequenceEqual(inboundData);
44+
if (isEqualLength && areContentEqual)
45+
{
46+
Console.WriteLine("Test complete");
47+
}
48+
else
49+
{
50+
throw new Exception("Test failed, decrypted data not equal to original");
51+
}
52+
}
53+
}
54+
}

LibSample/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ static void Main(string[] args)
2626
//new SerializerBenchmark().Run();
2727
new SpeedBench().Run();
2828
//new PacketProcessorExample().Run();
29+
//new AesEncryptionTest().Run();
2930
}
3031
}
3132
}

LiteNetLib.Tests/EncryptionLayerTest.cs

Lines changed: 0 additions & 132 deletions
This file was deleted.

0 commit comments

Comments
 (0)