public LoadForm(string fileName, Service1Client _client, string SafeFileName, string algorithm, string userName) { //upload constructor InitializeComponent(); _userName = userName; this.ControlBox = false; lblCaption.Text = $@"File {Path.GetFileName(fileName)}{Environment.NewLine} is uploading and encrypting!{Environment.NewLine} Please wait"; this.Text = @"Uploading..."; progressBar1.Maximum = 20; timer1.Interval = 250; progressBar1.Style = ProgressBarStyle.Marquee; timer1.Start(); if (algorithm == "Knapsack") { _crypto = new KnapSack(); _algByte = Encoding.ASCII.GetBytes("K"); } else if (algorithm == "SimpleSubstitution") { _crypto = SimpleSub.Instance; _algByte = Encoding.ASCII.GetBytes("S"); } else if (algorithm == "XXTEA") { _crypto = XXTEA.Instance; _algByte = Encoding.ASCII.GetBytes("X"); } UploadFile(fileName, _client, SafeFileName); }
private async void DownloadFile(string fileName, Service1Client _client) { var fileinfo = _client.FileInfo(fileName, _userName); var path = _directoryPath + @"\Download\"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } var nChunks = fileinfo.Length / CHUNK + 1; var down = new byte[fileinfo.Length]; for (int i = 0; i < nChunks; i++) { var Downdata = _client.DownloadWithChunks(fileinfo.Name, _userName, i); Buffer.BlockCopy(Downdata, 0, down, i * CHUNK, Downdata.Length); } //var down = _client.Download(fileName, _userName); var dataWithoutAlgorithamByte = new byte[down.Length - 1]; _algByte = new Byte[] { down[down.Length - 1] }; if (Encoding.ASCII.GetString(_algByte) == "X") { _crypto = XXTEA.Instance; } else if (Encoding.ASCII.GetString(_algByte) == "K") { _crypto = new KnapSack(); } else if (Encoding.ASCII.GetString(_algByte) == "S") { _crypto = SimpleSub.Instance; } Buffer.BlockCopy(down, 0, dataWithoutAlgorithamByte, 0, down.Length - 1); var data = _crypto.Decrypt(dataWithoutAlgorithamByte); var hash = await Task.Run(() => GetHash(dataWithoutAlgorithamByte)); //var hash = GetHash(data); var redisGet = _client.GetFileHash(_userName, fileName); if (hash == redisGet) { File.WriteAllBytes($"{path}{fileName}", data); } else { MessageBox.Show($@"Error while downloading!{Environment.NewLine}Try again!!!", "Download Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } timer1.Stop(); DialogResult = DialogResult.OK; this.Close(); }