public async void LoadMinisignKeyIntoKeystore() { MainViewError = string.Empty; IsWorking = true; string filename; string password; var openFileDialog = new OpenFileDialog { DefaultExt = ".key", Filter = "Minisign private key (.key)|*.key", Multiselect = false }; var result = openFileDialog.ShowDialog(); if (result == true) { filename = openFileDialog.FileName; try { var win = new PasswordInputViewModel { DisplayName = "Enter minisign password" }; dynamic settings = new ExpandoObject(); settings.WindowStartupLocation = WindowStartupLocation.CenterOwner; settings.Owner = GetView(); var inputOk = _windowManager.ShowDialog(win, null, settings); if (inputOk == true) { password = win.UserPassword; if (!string.IsNullOrEmpty(password)) { var newKey = await Task.Run(() => { var calculatedKey = new Key(); try { calculatedKey.KeyType = KeyType.Minisign; var keyPair = Minisign.LoadPrivateKeyFromFile(filename, password); calculatedKey.PublicKey = keyPair.PublicKey; calculatedKey.PrivateKey = keyPair.SecretKey; calculatedKey.KeyId = keyPair.KeyId; } catch (OutOfMemoryException) { MainViewError = "Could not load minisign key (out of memory)."; } catch (Exception) { MainViewError = "Could not load minisign key."; } return(calculatedKey); }).ConfigureAwait(true); if (newKey != null) { if (newKey.PrivateKey != null) { AddKey(newKey); } } } else { MainViewError = "Could not load minisign key (missing password)"; } } } catch (Exception) { MainViewError = "Could not load minisign key (failure)"; } } else { MainViewError = "Could not load minisign key (missing password)"; } IsWorking = false; }
/// <summary> /// Load/Import a key from a file. /// </summary> public async void LoadKeyIntoKeystore() { MainViewError = string.Empty; IsWorking = true; string password; var openFileDialog = new OpenFileDialog { DefaultExt = ".lkey", Filter = "lkey files (.lkey)|*.lkey", Multiselect = false }; var result = openFileDialog.ShowDialog(); if (result == true) { var filename = openFileDialog.FileName; try { var win = new PasswordInputViewModel { DisplayName = "Enter key password" }; dynamic settings = new ExpandoObject(); settings.WindowStartupLocation = WindowStartupLocation.CenterOwner; settings.Owner = GetView(); var inputOk = _windowManager.ShowDialog(win, null, settings); if (inputOk == true) { password = win.UserPassword; if (!string.IsNullOrEmpty(password)) { Key key; using (var keyFileStream = File.OpenRead(filename)) { key = Serializer.DeserializeWithLengthPrefix <Key>(keyFileStream, PrefixStyle.Base128, 0); } if (key != null) { var decryptionKey = await Task.Run(() => { var tmpKey = new byte[32]; try { tmpKey = PasswordHash.ScryptHashBinary(Encoding.UTF8.GetBytes(password), key.KeySalt, PasswordHash.Strength.MediumSlow, 32); } catch (OutOfMemoryException) { MainViewError = "Could not load key (out of memory)."; } catch (Exception) { MainViewError = "Could not load key (failure)"; } return(tmpKey); }).ConfigureAwait(true); if (decryptionKey != null) { try { key.PrivateKey = SecretBox.Open(key.PrivateKey, key.KeyNonce, decryptionKey); // check if the key pair matches AddKey(key); } catch (CryptographicException) { MainViewError = "Could not load key (wrong password?)"; } } else { MainViewError = "Could not load key (missing decryption key)"; } } else { MainViewError = "Could not load key (maybe not a lkey?)"; } } else { MainViewError = "Could not load key (missing password)"; } } else { MainViewError = "Could not load key (missing password)"; } } catch (Exception) { MainViewError = "Could not load key (exception)"; } } IsWorking = false; }
/// <summary> /// Store/Export a key to file. /// </summary> public async void ExportKeyToFile() { MainViewError = string.Empty; IsWorking = true; string password; if (SelectedKey != null) { var exportKey = new Key { PublicKey = SelectedKey.PublicKey, PrivateKey = SelectedKey.PrivateKey, KeyType = SelectedKey.KeyType, KeyId = SelectedKey.KeyId ?? null, KeySalt = SelectedKey.KeySalt ?? PasswordHash.GenerateSalt(), KeyNonce = SelectedKey.KeyNonce ?? SecretBox.GenerateNonce() }; var saveFileDialog = new SaveFileDialog { OverwritePrompt = true, AddExtension = true, DefaultExt = ".lkey" }; if (exportKey.KeyId != null) { saveFileDialog.FileName = Utilities.BinaryToHex(exportKey.KeyId); } var result = saveFileDialog.ShowDialog(); if (result == true) { var filename = saveFileDialog.FileName; try { var win = new PasswordInputViewModel { DisplayName = "Enter a new protection password" }; dynamic settings = new ExpandoObject(); settings.WindowStartupLocation = WindowStartupLocation.CenterOwner; settings.Owner = GetView(); var inputOk = _windowManager.ShowDialog(win, null, settings); if (inputOk == true) { password = win.UserPassword; if (!string.IsNullOrEmpty(password)) { var encryptionKey = await Task.Run(() => { var tmpKey = new byte[32]; try { tmpKey = PasswordHash.ScryptHashBinary(Encoding.UTF8.GetBytes(password), exportKey.KeySalt, PasswordHash.Strength.MediumSlow, 32); } catch (OutOfMemoryException) { MainViewError = "Could not export key (out of memory)."; } catch (Exception) { MainViewError = "Could not export key."; } return(tmpKey); }).ConfigureAwait(true); if (encryptionKey != null) { exportKey.PrivateKey = SecretBox.Create(exportKey.PrivateKey, exportKey.KeyNonce, encryptionKey); using (var keyFileStream = File.OpenWrite(filename)) { Serializer.SerializeWithLengthPrefix(keyFileStream, exportKey, PrefixStyle.Base128, 0); } } else { MainViewError = "Could not export key (missing encryption key?)"; } } else { MainViewError = "Could not export key (missing password)"; } } } catch (Exception) { MainViewError = "Could not export key (failure)"; } } else { MainViewError = "Could not export key (missing password)"; } } IsWorking = false; }
public async void LoadMinisignKeyIntoKeystore() { MainViewError = string.Empty; IsWorking = true; string filename; string password; var openFileDialog = new OpenFileDialog { DefaultExt = ".key", Filter = "Minisign private key (.key)|*.key", Multiselect = false }; var result = openFileDialog.ShowDialog(); if (result == true) { filename = openFileDialog.FileName; try { var win = new PasswordInputViewModel {DisplayName = "Enter minisign password"}; dynamic settings = new ExpandoObject(); settings.WindowStartupLocation = WindowStartupLocation.CenterOwner; settings.Owner = GetView(); var inputOk = _windowManager.ShowDialog(win, null, settings); if (inputOk == true) { password = win.UserPassword; if (!string.IsNullOrEmpty(password)) { var newKey = await Task.Run(() => { var calculatedKey = new Key(); try { calculatedKey.KeyType = KeyType.Minisign; var keyPair = Minisign.LoadPrivateKeyFromFile(filename, password); calculatedKey.PublicKey = keyPair.PublicKey; calculatedKey.PrivateKey = keyPair.SecretKey; calculatedKey.KeyId = keyPair.KeyId; } catch (OutOfMemoryException) { MainViewError = "Could not load minisign key (out of memory)."; } catch (Exception) { MainViewError = "Could not load minisign key."; } return calculatedKey; }).ConfigureAwait(true); if (newKey != null) { if (newKey.PrivateKey != null) { AddKey(newKey); } } } else { MainViewError = "Could not load minisign key (missing password)"; } } } catch (Exception) { MainViewError = "Could not load minisign key (failure)"; } } else { MainViewError = "Could not load minisign key (missing password)"; } IsWorking = false; }
/// <summary> /// Load/Import a key from a file. /// </summary> public async void LoadKeyIntoKeystore() { MainViewError = string.Empty; IsWorking = true; string password; var openFileDialog = new OpenFileDialog { DefaultExt = ".lkey", Filter = "lkey files (.lkey)|*.lkey", Multiselect = false }; var result = openFileDialog.ShowDialog(); if (result == true) { var filename = openFileDialog.FileName; try { var win = new PasswordInputViewModel {DisplayName = "Enter key password"}; dynamic settings = new ExpandoObject(); settings.WindowStartupLocation = WindowStartupLocation.CenterOwner; settings.Owner = GetView(); var inputOk = _windowManager.ShowDialog(win, null, settings); if (inputOk == true) { password = win.UserPassword; if (!string.IsNullOrEmpty(password)) { Key key; using (var keyFileStream = File.OpenRead(filename)) { key = Serializer.DeserializeWithLengthPrefix<Key>(keyFileStream, PrefixStyle.Base128, 0); } if (key != null) { var decryptionKey = await Task.Run(() => { var tmpKey = new byte[32]; try { tmpKey = PasswordHash.ScryptHashBinary(Encoding.UTF8.GetBytes(password), key.KeySalt, PasswordHash.Strength.MediumSlow, 32); } catch (OutOfMemoryException) { MainViewError = "Could not load key (out of memory)."; } catch (Exception) { MainViewError = "Could not load key (failure)"; } return tmpKey; }).ConfigureAwait(true); if (decryptionKey != null) { try { key.PrivateKey = SecretBox.Open(key.PrivateKey, key.KeyNonce, decryptionKey); // check if the key pair matches AddKey(key); } catch (CryptographicException) { MainViewError = "Could not load key (wrong password?)"; } } else { MainViewError = "Could not load key (missing decryption key)"; } } else { MainViewError = "Could not load key (maybe not a lkey?)"; } } else { MainViewError = "Could not load key (missing password)"; } } else { MainViewError = "Could not load key (missing password)"; } } catch (Exception) { MainViewError = "Could not load key (exception)"; } } IsWorking = false; }
/// <summary> /// Store/Export a key to file. /// </summary> public async void ExportKeyToFile() { MainViewError = string.Empty; IsWorking = true; string password; if (SelectedKey != null) { var exportKey = new Key { PublicKey = SelectedKey.PublicKey, PrivateKey = SelectedKey.PrivateKey, KeyType = SelectedKey.KeyType, KeyId = SelectedKey.KeyId ?? null, KeySalt = SelectedKey.KeySalt ?? PasswordHash.GenerateSalt(), KeyNonce = SelectedKey.KeyNonce ?? SecretBox.GenerateNonce() }; var saveFileDialog = new SaveFileDialog { OverwritePrompt = true, AddExtension = true, DefaultExt = ".lkey" }; if (exportKey.KeyId != null) { saveFileDialog.FileName = Utilities.BinaryToHex(exportKey.KeyId); } var result = saveFileDialog.ShowDialog(); if (result == true) { var filename = saveFileDialog.FileName; try { var win = new PasswordInputViewModel {DisplayName = "Enter a new protection password"}; dynamic settings = new ExpandoObject(); settings.WindowStartupLocation = WindowStartupLocation.CenterOwner; settings.Owner = GetView(); var inputOk = _windowManager.ShowDialog(win, null, settings); if (inputOk == true) { password = win.UserPassword; if (!string.IsNullOrEmpty(password)) { var encryptionKey = await Task.Run(() => { var tmpKey = new byte[32]; try { tmpKey = PasswordHash.ScryptHashBinary(Encoding.UTF8.GetBytes(password), exportKey.KeySalt, PasswordHash.Strength.MediumSlow, 32); } catch (OutOfMemoryException) { MainViewError = "Could not export key (out of memory)."; } catch (Exception) { MainViewError = "Could not export key."; } return tmpKey; }).ConfigureAwait(true); if (encryptionKey != null) { exportKey.PrivateKey = SecretBox.Create(exportKey.PrivateKey, exportKey.KeyNonce, encryptionKey); using (var keyFileStream = File.OpenWrite(filename)) { Serializer.SerializeWithLengthPrefix(keyFileStream, exportKey, PrefixStyle.Base128, 0); } } else { MainViewError = "Could not export key (missing encryption key?)"; } } else { MainViewError = "Could not export key (missing password)"; } } } catch (Exception) { MainViewError = "Could not export key (failure)"; } } else { MainViewError = "Could not export key (missing password)"; } } IsWorking = false; }