forked from RevenantX/LiteNetLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAesEncryptionTest.cs
More file actions
57 lines (49 loc) · 2.11 KB
/
AesEncryptionTest.cs
File metadata and controls
57 lines (49 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
namespace LibSample
{
class AesEncryptionTest : IExample
{
public void Run() => AesLayerEncryptDecrypt();
private void AesLayerEncryptDecrypt()
{
IPEndPoint emptyEndPoint = null;
var keyGen = RandomNumberGenerator.Create();
byte[] key = new byte[AesEncryptLayer.KeySizeInBytes];
keyGen.GetBytes(key);
const string testData = "This text is long enough to need multiple blocks to encrypt";
var outboudLayer = new AesEncryptLayer(key);
byte[] outbound = Encoding.ASCII.GetBytes(testData);
int lengthOfPacket = outbound.Length;
int start = 0;
int length = outbound.Length;
outboudLayer.ProcessOutBoundPacket(ref emptyEndPoint, ref outbound, ref start, ref length);
int minLenth = lengthOfPacket + AesEncryptLayer.BlockSizeInBytes;
int maxLength = lengthOfPacket + outboudLayer.ExtraPacketSizeForLayer;
if (length < minLenth || length > maxLength)
{
throw new Exception("Packet length out of bounds");
}
var inboundLayer = new AesEncryptLayer(key);
//Copy array so we dont read and write to same array
byte[] inboundData = new byte[outbound.Length];
outbound.CopyTo(inboundData, 0);
inboundLayer.ProcessInboundPacket(ref emptyEndPoint, ref inboundData, ref length);
Console.WriteLine(Encoding.ASCII.GetString(inboundData, 0, length));
byte[] expectedPlaintext = Encoding.ASCII.GetBytes(testData);
var isEqualLength = expectedPlaintext.Length == length;
var areContentEqual = expectedPlaintext.SequenceEqual(inboundData);
if (isEqualLength && areContentEqual)
{
Console.WriteLine("Test complete");
}
else
{
throw new Exception("Test failed, decrypted data not equal to original");
}
}
}
}