using System.Net; using System.Net.Sockets; using System.Text; using System.Security.Cryptography; using System.Reflection.Metadata; using System; namespace Naloga { class Client { const int STD_PORT = 1234; const string STD_IP = "127.0.0.1"; const int STD_BUFF = 256; static void Main() { TcpClient client = new TcpClient(); client.Connect(IPAddress.Parse(STD_IP), STD_PORT); NetworkStream tcpStream = client.GetStream(); byte[] bytes = new byte[STD_BUFF]; int lenght = tcpStream.Read(bytes, 0, bytes.Length); int bufferCount = BitConverter.ToInt32(bytes,0); using (ECDiffieHellmanCng eCDiffieHellmanCng = new ECDiffieHellmanCng()) { eCDiffieHellmanCng.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash; eCDiffieHellmanCng.HashAlgorithm = CngAlgorithm.Sha256; byte[] myPublicKey = eCDiffieHellmanCng.PublicKey.ToByteArray(); tcpStream.Write(myPublicKey, 0, myPublicKey.Length); byte[] therePublicKey = new byte[STD_BUFF]; int retLenght = tcpStream.Read(therePublicKey, 0, therePublicKey.Length); CngKey thereKey = CngKey.Import(therePublicKey, CngKeyBlobFormat.EccPublicBlob); byte[] myKey = eCDiffieHellmanCng.DeriveKeyMaterial(thereKey); using (Aes aes = Aes.Create()) { aes.Key = myKey; aes.IV = Encoding.UTF8.GetBytes("sestnajst1616161"); //Console.WriteLine(Encoding.UTF8.GetString(aes.IV)); aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7; // Encrypt the message using(FileStream fileStream = new FileStream("video.mp4", FileMode.OpenOrCreate)) using (CryptoStream cs = new CryptoStream(tcpStream, aes.CreateDecryptor(), CryptoStreamMode.Read)) { for (int i = 0; i < bufferCount+1; i++) { byte[] bytes2 = new byte[STD_BUFF]; int len = cs.Read(bytes2, 0, bytes2.Length); //Console.WriteLine(Encoding.UTF8.GetString(bytes2)); fileStream.Write(bytes2, 0, len); } cs.Close(); fileStream.Close(); } aes.Dispose(); } } tcpStream.Close(); client.Close(); Console.WriteLine("Finished"); Console.ReadKey(); } } }