public static Task <string> AesEncryptTask(CancellationToken ct, string inputfile, string outfile, KeySizeAES size, IProgress <double> progress) { return(Task <string> .Run(() => AESEncrypt(ct, inputfile, outfile, size, progress), ct)); }
private async void BtnStart_Click(object sender, RoutedEventArgs e) { BtnStart.IsEnabled = false; Modeselect.IsEnabled = false; HashAlgorithms alg = HashAlgorithms.MD5; cts = new CancellationTokenSource(); try { switch (Modeselect.SelectedIndex) { case 0: TbOutput.Clear(); string specials = null; string gen = null; if ((bool)CbSpecial.IsChecked) { specials = TbSpecials.Text; } for (int i = 0; i < SlNumber.Value; i++) { gen = Cryptog.GeneratePassWord((bool)CbLowercase.IsChecked, (bool)CbLowercase.IsChecked, (bool)CbNumbers.IsChecked, specials, (int)SlLength.Value); TbOutput.AppendText(gen + "\r\n"); } break; case 1: TbCaesarOutput.Clear(); if (RbCaesarEnc.IsChecked == true) { var rules = CaesarRules.Classic; if (RbCaesarRandom.IsChecked == true) { rules = CaesarRules.Random; } TbCaesarKeyRule.Text = CaesarRules.SerializeRule(rules); TbCaesarOutput.Text = Cryptog.CaesarCrypt(TbCaesarInput.Text, rules, false); } else { var rules = CaesarRules.DeserializeRule(TbCaesarKeyRule.Text); TbCaesarOutput.Text = Cryptog.CaesarCrypt(TbCaesarInput.Text, rules, true); } break; case 2: if ((bool)RbMD5f.IsChecked) { alg = HashAlgorithms.MD5; } else if ((bool)RbSHA1f.IsChecked) { alg = HashAlgorithms.SHA1; } else if ((bool)RbSHA256f.IsChecked) { alg = HashAlgorithms.SHA256; } else if ((bool)RbSHA512f.IsChecked) { alg = HashAlgorithms.SHA512; } string hash = await Cryptog.ComputeHashTask(cts.Token, FInput.SelectedPath, Indicator, alg); TbHashOutput.Text = hash; break; case 3: if ((bool)RbMD5s.IsChecked) { alg = HashAlgorithms.MD5; } else if ((bool)RbSHA1s.IsChecked) { alg = HashAlgorithms.SHA1; } else if ((bool)RbSHA256s.IsChecked) { alg = HashAlgorithms.SHA256; } else if ((bool)RbSHA512s.IsChecked) { alg = HashAlgorithms.SHA512; } string ret = Cryptog.HashInputString(TbHashStringInput.Text, alg); TbHashStringOutput.Text = ret; break; case 4: await Cryptog.XorEncryptTask(cts.Token, XorIn.SelectedPath, XorKey.SelectedPath, XorOut.SelectedPath, Indicator); break; case 5: KeySizeAES keysize = KeySizeAES.bit128; if ((bool)RbAesK128.IsChecked) { keysize = KeySizeAES.bit128; } else if ((bool)RbAesK192.IsChecked) { keysize = KeySizeAES.bit192; } else if ((bool)RbAesK256.IsChecked) { keysize = KeySizeAES.bit256; } if ((bool)RbAesEnc.IsChecked) { TbAesKey.Text = await Cryptog.AesEncryptTask(cts.Token, AesIn.SelectedPath, AesOut.SelectedPath, keysize, Indicator); } else if ((bool)RbAesDec.IsChecked) { await Cryptog.AesDecryptTask(cts.Token, AesIn.SelectedPath, AesOut.SelectedPath, TbAesKey.Text, Indicator); } break; } } catch (OperationCanceledException) { WpfHelpers.ExceptionDialog("Task Canceled"); } Modeselect.IsEnabled = true; BtnStart.IsEnabled = true; }
private static string AESEncrypt(CancellationToken ct, string inputfile, string outfile, KeySizeAES size, IProgress <double> progress) { RijndaelManaged aesEncryption = new RijndaelManaged(); aesEncryption.KeySize = (int)size; aesEncryption.BlockSize = 128; aesEncryption.Mode = CipherMode.CBC; aesEncryption.Padding = PaddingMode.PKCS7; string completeEncodedKey = GenerateAESKey(aesEncryption.KeySize); string[] key = ASCIIEncoding.UTF8.GetString(Convert.FromBase64String(completeEncodedKey)).Split(','); aesEncryption.IV = Convert.FromBase64String(key[0]); aesEncryption.Key = Convert.FromBase64String(key[1]); ICryptoTransform crypto = aesEncryption.CreateEncryptor(); Stream infile = File.OpenRead(inputfile); Stream o = File.Create(outfile); using (CryptoStream outp = new CryptoStream(o, crypto, CryptoStreamMode.Write)) { int read = 0; byte[] buffer = new byte[4096]; do { read = infile.Read(buffer, 0, buffer.Length); outp.Write(buffer, 0, read); if (progress != null) { if (progress != null) { double percent = ((double)infile.Position / (double)infile.Length) * 100.00d; progress.Report(percent); } } if (infile.Position % 4096 == 0) { ct.ThrowIfCancellationRequested(); } }while (read != 0); } return(completeEncodedKey); }