/// <summary> /// Decrypts wav data using private key set in RsaManager static class. Returns true if successful. /// </summary> /// <returns>True if successful otherwise false</returns> public bool Decrypt() { if (Status == WavFileState.Decrypted || Status == WavFileState.Empty) { return(false); } using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider()) { //Gets encrypted array containing key and IV for aes algorithm from the end of data array var encryptedKeyAndIV = data.Skip(data.Length - RsaManager.EncryptedDataLength).ToArray(); var mergedKeyAndIV = RsaManager.Decrypt(encryptedKeyAndIV); if (mergedKeyAndIV == null) { return(false); } //Split merged array to both variables aes.Key = mergedKeyAndIV.Take(32).ToArray(); aes.IV = mergedKeyAndIV.Skip(32).ToArray(); var decryptor = aes.CreateDecryptor(); //Get only data without merged key and IV array from the end data = data.Take(data.Length - RsaManager.EncryptedDataLength).ToArray(); //Decrypt data data = decryptor.TransformFinalBlock(data, 0, data.Length); } Status = WavFileState.Decrypted; return(true); }
/// <summary> /// Encrypts wav data using public key set in RsaManager static class. Returns true if successful. /// </summary> /// <returns>True if successful otherwise false</returns> public bool Encrypt() { if (Status == WavFileState.Encrypted || Status == WavFileState.Empty) { return(false); } using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider()) { var encryptor = aes.CreateEncryptor(); //Encrypt data Data = encryptor.TransformFinalBlock(Data, 0, Data.Length); //Save both AES key parts to one array byte[] mergedKeyAndIV = new byte[aes.Key.Length + aes.IV.Length]; Array.Copy(aes.Key, mergedKeyAndIV, aes.Key.Length); Array.Copy(aes.IV, 0, mergedKeyAndIV, aes.Key.Length, aes.IV.Length); var encryptedKeyAndIV = RsaManager.Encrypt(mergedKeyAndIV); if (encryptedKeyAndIV == null) { return(false); } //Add encrypted AES key and IV array to the end of data array var oldLength = data.Length; Array.Resize(ref data, oldLength + encryptedKeyAndIV.Length); Array.Copy(encryptedKeyAndIV, 0, data, oldLength, encryptedKeyAndIV.Length); } Status = WavFileState.Encrypted; return(true); }
private void SaveBothKeysButton_Click(object sender, RoutedEventArgs e) { var privateKeyName = UniqueKeyFileNamePart + "-private_key-" + CommonKeyFileNamePart; var publicKeyName = UniqueKeyFileNamePart + "-public_key-" + CommonKeyFileNamePart; RsaManager.WriteKeysToFile(privateKeyName, publicKeyName); string message = "Klucze zostały zapisane pod nazwami:\n\n" + privateKeyName + "\n" + publicKeyName; MessageBox.Show(message, "Zapisano", MessageBoxButton.OK, MessageBoxImage.Information); }
private void PublicKeySaveAsButton_Click(object sender, RoutedEventArgs e) { SaveFileDialog SaveAsDialog = new SaveFileDialog(); SaveAsDialog.Title = "Zapisz klucz publiczny jako"; SaveAsDialog.Filter = "Plik tekstowy|*.txt"; if (SaveAsDialog.ShowDialog() == true) { RsaManager.WriteKeysToFile(false, SaveAsDialog.FileName); } }
private void LoadPublicKeyButton_Click(object sender, RoutedEventArgs e) { OpenFileDialog OpenDialog = new OpenFileDialog(); OpenDialog.Title = "Wybierz plik zawierający klucz publiczny"; if (OpenDialog.ShowDialog() == true) { string publicKey = File.ReadAllText(OpenDialog.FileName, Encoding.UTF8); RsaManager.SetKey(false, publicKey); PublicKeyTextBlock.Text = RsaManager.GetKeyString(false); } }
private void GenerateKeyParButton_Click(object sender, RoutedEventArgs e) { PrivateKeyLoadingImage.Visibility = Visibility.Visible; PublicKeyLoadingImage.Visibility = Visibility.Visible; PrivateKeyTextBlock.Text = ""; PublicKeyTextBlock.Text = ""; Task.Run(() => { RsaManager.GenerateKeyPar(); this.Dispatcher.Invoke(() => { PrivateKeyLoadingImage.Visibility = Visibility.Collapsed; PublicKeyLoadingImage.Visibility = Visibility.Collapsed; PrivateKeyTextBlock.Text = RsaManager.GetKeyString(true); PublicKeyTextBlock.Text = RsaManager.GetKeyString(false); }); }); //Task task = Task.Run((Action)RsaManager.generateKeyPar); //task.ContinueWith((t) => //{ // PrivateKeyTextBlock.Text = RsaManager.GetKeyString(true); // PublicKeyTextBlock.Text = RsaManager.GetKeyString(false); //}); }