230 lines
		
	
	
		
			9.6 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			230 lines
		
	
	
		
			9.6 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using Microsoft.Win32;
 | |
| using System;
 | |
| using System.Collections.Generic;
 | |
| using System.IO;
 | |
| using System.Linq;
 | |
| using System.Net;
 | |
| using System.Net.Sockets;
 | |
| using System.Runtime.InteropServices;
 | |
| using System.Security.Cryptography;
 | |
| using System.Text;
 | |
| using System.Threading.Tasks;
 | |
| using System.Windows;
 | |
| using System.Windows.Controls;
 | |
| using System.Windows.Data;
 | |
| using System.Windows.Documents;
 | |
| using System.Windows.Input;
 | |
| using System.Windows.Media;
 | |
| using System.Windows.Media.Imaging;
 | |
| using System.Windows.Navigation;
 | |
| using System.Windows.Shapes;
 | |
| 
 | |
| namespace MyApplication
 | |
| {
 | |
|     /// <summary>
 | |
|     /// Interaction logic for MainWindow.xaml
 | |
|     /// </summary>
 | |
|     public partial class MainWindow : Window
 | |
|     {
 | |
| 
 | |
|         const int STD_PORT = 1234;
 | |
|         const string STD_IP = "127.0.0.1";
 | |
|         const int STD_BUFF = 256;
 | |
|         string fileName = "";
 | |
| 
 | |
| 
 | |
|         public MainWindow()
 | |
|         {
 | |
|             InitializeComponent();
 | |
|         }
 | |
| 
 | |
|         private void Select_Click(object sender, RoutedEventArgs e)
 | |
|         {
 | |
|             OpenFileDialog dialog = new OpenFileDialog();
 | |
|             dialog.FileName = ""; // Default file name
 | |
|             dialog.DefaultExt = "*."; // Default file extension
 | |
|             dialog.Filter = "All|*.*"; // Filter files by extension
 | |
| 
 | |
|             bool? result = dialog.ShowDialog();
 | |
|             if (result == true)
 | |
|             {
 | |
|                 fileName = dialog.FileName;
 | |
|                 lableAddress.Content = fileName;
 | |
|                 send_Button.IsEnabled = true;
 | |
|             }
 | |
|         }
 | |
|         private void Send_Click(object sender, RoutedEventArgs e)
 | |
|         {
 | |
|             lableFinish.Content = "";
 | |
|             Send();
 | |
|         }
 | |
|         private void Recive_Click(object sender, RoutedEventArgs e)
 | |
|         {
 | |
|             lableFinish.Content = "";
 | |
|             Recive();
 | |
| 
 | |
|         }
 | |
| 
 | |
|         private void Recive()
 | |
|         {
 | |
|             string downloadsPath = KnownFolders.GetPath(KnownFolder.Downloads);
 | |
|             TcpListener tcpListener = new TcpListener(IPAddress.Parse(STD_IP), STD_PORT);
 | |
|             tcpListener.Start();
 | |
|             using TcpClient tcpClient = tcpListener.AcceptTcpClient();
 | |
|             using NetworkStream networkStream = tcpClient.GetStream();
 | |
|             
 | |
|             //--------------------------------------------------------------------------------------------------Recive Name of File------------------------------------
 | |
|             byte[] buffer = new byte[STD_BUFF];
 | |
|             int readBytes = networkStream.Read(buffer, 0, buffer.Length);
 | |
|             if (readBytes < 0) return;
 | |
|             string fileName = Encoding.UTF8.GetString(buffer,0, readBytes);
 | |
|             string fileDestination = downloadsPath + "\\" + fileName;
 | |
|             lableFileName.Content = fileDestination;
 | |
| 
 | |
|             //-----------------------------------------------------------------------------------------------Send Size of File------------------------------------
 | |
|             buffer = new byte[STD_BUFF];
 | |
|             readBytes = networkStream.Read(buffer, 0, buffer.Length);
 | |
|             if (readBytes < 0) return;
 | |
|             int bufferCount = BitConverter.ToInt32(buffer, 0);
 | |
|             lableSize.Content = bufferCount.ToString();
 | |
| 
 | |
| 
 | |
|             //-------------------------------------------------------------------------------------------------ECDiffieHellmanCng--------------------
 | |
|             using ECDiffieHellmanCng eCDiffieHellmanCng = new ECDiffieHellmanCng();
 | |
| 
 | |
|             eCDiffieHellmanCng.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
 | |
|             eCDiffieHellmanCng.HashAlgorithm = CngAlgorithm.Sha256;
 | |
| 
 | |
|             byte[] myPublicKey = eCDiffieHellmanCng.PublicKey.ToByteArray();
 | |
|             networkStream.Write(myPublicKey, 0, myPublicKey.Length);
 | |
| 
 | |
|             byte[] therePublicKey = new byte[140];
 | |
|             readBytes = networkStream.Read(therePublicKey, 0, therePublicKey.Length);
 | |
|             if (readBytes < 0) return;
 | |
| 
 | |
|             CngKey thereKey = CngKey.Import(therePublicKey, CngKeyBlobFormat.EccPublicBlob);
 | |
|             byte[] Key = eCDiffieHellmanCng.DeriveKeyMaterial(thereKey);
 | |
| 
 | |
|             using Aes aes = Aes.Create();
 | |
|             aes.Key = Key;
 | |
|             aes.IV = Encoding.UTF8.GetBytes("sestnajst1616161");
 | |
|             aes.Mode = CipherMode.CBC;
 | |
|             aes.Padding = PaddingMode.PKCS7;
 | |
| 
 | |
|             using CryptoStream cryptoStream = new CryptoStream(networkStream, aes.CreateDecryptor(), CryptoStreamMode.Read);
 | |
|             using FileStream fileStream = new FileStream(fileDestination, FileMode.OpenOrCreate);
 | |
| 
 | |
|             for (int i = 0; i < bufferCount; i++)
 | |
|             {
 | |
|                 buffer = new byte[STD_BUFF];
 | |
|                 readBytes = cryptoStream.Read(buffer, 0, buffer.Length);
 | |
|                 fileStream.Write(buffer, 0, readBytes);
 | |
|             }
 | |
| 
 | |
|             cryptoStream.Close();
 | |
|             aes.Clear();
 | |
|             eCDiffieHellmanCng.Clear();
 | |
|             networkStream.Close();
 | |
|             tcpClient.Close();
 | |
|             tcpListener.Stop();
 | |
|             lableFinish.Content = "finished";
 | |
|         }
 | |
| 
 | |
|         void Send()
 | |
|         {
 | |
|             using TcpClient tcpClient = new TcpClient();
 | |
|             tcpClient.Connect(IPAddress.Parse(STD_IP), STD_PORT);
 | |
|             using NetworkStream networkStream = tcpClient.GetStream();
 | |
| 
 | |
|             //--------------------------------------------------------------------------------------------------Send Name of File------------------------------------
 | |
|             string[] fileNames = fileName.Split("\\");
 | |
|             byte[] nameInBytes = Encoding.UTF8.GetBytes(fileNames.Last());
 | |
|             networkStream.Write(nameInBytes, 0, nameInBytes.Length);
 | |
|             lableFileName.Content = fileName;
 | |
| 
 | |
|             using FileStream fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Read);
 | |
|             //-----------------------------------------------------------------------------------------------Send Size of File------------------------------------
 | |
|             int bufferCount = Convert.ToInt32(Math.Ceiling((double)fileStream.Length / (double)STD_BUFF));
 | |
|             byte[] size = BitConverter.GetBytes(bufferCount);
 | |
|             networkStream.Write(size, 0, size.Length);
 | |
|             lableSize.Content = bufferCount.ToString();
 | |
| 
 | |
|             //-------------------------------------------------------------------------------------------------ECDiffieHellmanCng--------------------
 | |
|             using ECDiffieHellmanCng eCDiffieHellmanCng = new ECDiffieHellmanCng();
 | |
|             eCDiffieHellmanCng.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
 | |
|             eCDiffieHellmanCng.HashAlgorithm = CngAlgorithm.Sha256;
 | |
| 
 | |
|             //----------------------------------------------------------------------Send my key--------------
 | |
|             byte[] myPublicKey = eCDiffieHellmanCng.PublicKey.ToByteArray();
 | |
|             networkStream.Write(myPublicKey, 0, myPublicKey.Length);
 | |
| 
 | |
|             //-------------------------------------------------------------------Recive there key and create private key------------------------
 | |
|             byte[] therePublicKey = new byte[140];
 | |
|             int readByte = networkStream.Read(therePublicKey, 0, therePublicKey.Length);
 | |
|             if (readByte < 0) return;
 | |
|             CngKey thereKey = CngKey.Import(therePublicKey, CngKeyBlobFormat.EccPublicBlob);
 | |
|             byte[] Key = eCDiffieHellmanCng.DeriveKeyMaterial(thereKey);
 | |
| 
 | |
|             //---------------------------------------------------------------------------AES Crete
 | |
|             using Aes aes = Aes.Create();
 | |
|             aes.Key = Key;
 | |
|             aes.IV = Encoding.UTF8.GetBytes("sestnajst1616161");
 | |
|             aes.Mode = CipherMode.CBC;
 | |
|             aes.Padding = PaddingMode.PKCS7;
 | |
| 
 | |
|             //----------------------------------------------------------------------------Create crypto strim
 | |
|             using CryptoStream cryptoStream = new CryptoStream(fileStream, aes.CreateEncryptor(), CryptoStreamMode.Read);
 | |
|             
 | |
|             for(int i = 0; i < bufferCount+1; i++)
 | |
|             {
 | |
|                 byte[] buffer = new byte[STD_BUFF];
 | |
|                 int returndCaunt = cryptoStream.Read(buffer, 0, buffer.Length);
 | |
|                 networkStream.Write(buffer, 0, returndCaunt);
 | |
|             }
 | |
| 
 | |
|             cryptoStream.Close();
 | |
|             aes.Clear();
 | |
|             eCDiffieHellmanCng.Clear();
 | |
|             networkStream.Close();
 | |
|             networkStream.Close();
 | |
|             tcpClient.Close();
 | |
|             lableFinish.Content = "finished";
 | |
|         }
 | |
| 
 | |
| 
 | |
|         public enum KnownFolder
 | |
|         {
 | |
|             Contacts,
 | |
|             Downloads,
 | |
|             Favorites,
 | |
|             Links,
 | |
|             SavedGames,
 | |
|             SavedSearches
 | |
|         }
 | |
|         public static class KnownFolders
 | |
|         {
 | |
|             private static readonly Dictionary<KnownFolder, Guid> _guids = new()
 | |
|             {
 | |
|                 [KnownFolder.Contacts] = new("56784854-C6CB-462B-8169-88E350ACB882"),
 | |
|                 [KnownFolder.Downloads] = new("374DE290-123F-4565-9164-39C4925E467B"),
 | |
|                 [KnownFolder.Favorites] = new("1777F761-68AD-4D8A-87BD-30B759FA33DD"),
 | |
|                 [KnownFolder.Links] = new("BFB9D5E0-C6A9-404C-B2B2-AE6DB6AF4968"),
 | |
|                 [KnownFolder.SavedGames] = new("4C5C32FF-BB9D-43B0-B5B4-2D72E54EAAA4"),
 | |
|                 [KnownFolder.SavedSearches] = new("7D1D3A04-DEBB-4115-95CF-2F29DA2920DA")
 | |
|             };
 | |
| 
 | |
|             public static string GetPath(KnownFolder knownFolder)
 | |
|             {
 | |
|                 return SHGetKnownFolderPath(_guids[knownFolder], 0);
 | |
|             }
 | |
| 
 | |
|             [DllImport("shell32",
 | |
|                 CharSet = CharSet.Unicode, ExactSpelling = true, PreserveSig = false)]
 | |
|             private static extern string SHGetKnownFolderPath(
 | |
|                 [MarshalAs(UnmanagedType.LPStruct)] Guid rfid, uint dwFlags,
 | |
|                 nint hToken = 0);
 | |
|         }
 | |
| 
 | |
|     }
 | |
| }
 |