예제 #1
0
        public void TestEncryption()
        {
            var value = "Hello from unecrypted world";
            var aes = new AesEncryptor();

            var encryptedVal = aes.Encrypt(value);
            Assert.AreNotEqual(value, encryptedVal, "Value is not encrypted");

            // We need to trim \0 char from string as Aes is always creating block of 16 bytes
            var decryptedVal = Encoding.ASCII.GetString(aes.Decrypt(encryptedVal)).TrimEnd('\0');
            Assert.AreEqual(value, decryptedVal, "Value is not the same as before encryption");

            aes.Dispose();
        }
예제 #2
0
        public void TestAesEncryptorCreation()
        {
            var aes = new AesEncryptor();
            Assert.AreNotEqual(null, aes.Encryptors, "Encryptors not created");
            Assert.AreEqual(aes.Encryptors.Length, 32, "Invalid length of encryptors");

            var aesKey = new byte[16];
            var iVec = new byte[16];
            for (byte i = 0; i < 16; i++)
            {
                aesKey[i] = i;
                iVec[i] = (byte)(byte.MaxValue - i);
            }

            var aes2 = new AesEncryptor(aesKey, iVec);
            Assert.AreNotEqual(null, aes2.Encryptors, "Encryptors not created");
            Assert.IsTrue(aesKey.Concat(iVec).SequenceEqual(aes2.Encryptors), "Encryptors not created correctly");

            aes.Dispose();
            aes2.Dispose();
        }
예제 #3
0
        static void Main(string[] args)
        {
            // Client will send us the key and iVec
            AesEncryptor aes = null;
            // Rsa inicialization
            RsaEncryptor rsa = new RsaEncryptor(RSAKey.RsaParams);

            // Creates new server instance on port 10751
            Server server = new Server(10751);
            // Awaits client connection
            var tcpClient = server.AcceptClientBlockWait();
            // Creates client instance
            var client = new Client(tcpClient);
            byte[] data = null;
            do
            {
                // Gets received data (everything that is in network pipe)
                data = client.ReceiveData();
                if (data == null)
                    continue;

                do
                {
                    var length = BitConverter.ToUInt16(data, 0);
                    data = data.Skip(sizeof(UInt16)).ToArray();
                    var packetData = data.Take(length).ToArray();

                    // init packet, decrypt stream after inicialization
                    Packet packet = new Packet(aes != null ? aes.Decrypt(packetData) : packetData);
                    Packet response = null;
                    switch ((ClientPacketTypes)packet.OpcodeNumber)
                    {
                        case ClientPacketTypes.CMSG_INIT_ENCRYPTED_RSA:
                            var keys = rsa.Decrypt(packet.ReadBytes());
                            aes = new AesEncryptor(keys.Take(16).ToArray(), keys.Skip(16).ToArray());
                            client.AesEncryptor = aes;
                            response = new Packet(ServerPacketTypes.SMSG_INIT_RESPONSE_ENCRYPTED_RSA);
                            response.Write("Hello Client!");
                            break;
                        case ClientPacketTypes.CMSG_INIT_ENCRYPTED_AES:
                            Console.WriteLine(packet.ReadString());
                            response = new Packet(ServerPacketTypes.SMSG_INIT_RESPONSE_ENCRYPTED_AES);
                            response.Write("Hello Client, We are now fully encrypted!");
                            break;
                    }

                    if (response != null)
                    {
                        client.SendPacket(response);
                        response.Dispose();
                    }

                    data = data.Skip(length).ToArray();
                } while (data.Any());

                break;
            } while (true);

            aes.Dispose();
            client.Dispose();
            server.Dispose();

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey(true);
        }