diff --git a/src/Orc.SupportPackage.Example/App.xaml.cs b/src/Orc.SupportPackage.Example/App.xaml.cs index 78d62842..ba3ae47c 100644 --- a/src/Orc.SupportPackage.Example/App.xaml.cs +++ b/src/Orc.SupportPackage.Example/App.xaml.cs @@ -38,4 +38,4 @@ protected override void OnStartup(StartupEventArgs e) base.OnStartup(e); } } -} \ No newline at end of file +} diff --git a/src/Orc.SupportPackage.Example/ViewModels/MainViewModel.cs b/src/Orc.SupportPackage.Example/ViewModels/MainViewModel.cs index 8b637b71..3a155ec1 100644 --- a/src/Orc.SupportPackage.Example/ViewModels/MainViewModel.cs +++ b/src/Orc.SupportPackage.Example/ViewModels/MainViewModel.cs @@ -7,12 +7,14 @@ using System.Threading.Tasks; using System.Windows; using System.Windows.Media.Imaging; - using SystemInfo; using Catel; + using Catel.IoC; using Catel.MVVM; using Catel.Services; using Catel.Threading; + using Orc.FileSystem; using Orc.SupportPackage.ViewModels; + using SystemInfo; public class MainViewModel : ViewModelBase { @@ -21,27 +23,49 @@ public class MainViewModel : ViewModelBase private readonly IScreenCaptureService _screenCaptureService; private readonly ISystemInfoService _systemInfoService; + private readonly IMessageService _messageService; private readonly IUIVisualizerService _uiVisualizerService; private readonly IAppDataService _appDataService; - - public MainViewModel(IScreenCaptureService screenCaptureService, ISystemInfoService systemInfoService, - IUIVisualizerService uiVisualizerService, IAppDataService appDataService) + private readonly ITypeFactory _typeFactory; + private readonly IEncryptionService _encryptionService; + private readonly IOpenFileService _openFileService; + private readonly IFileService _fileService; + + public MainViewModel(IScreenCaptureService screenCaptureService, ISystemInfoService systemInfoService, IMessageService messageService, + IUIVisualizerService uiVisualizerService, IAppDataService appDataService, ITypeFactory typeFactory, IEncryptionService encryptionService, + IOpenFileService openFileService, IFileService fileService) { Argument.IsNotNull(() => screenCaptureService); Argument.IsNotNull(() => systemInfoService); + Argument.IsNotNull(() => messageService); Argument.IsNotNull(() => uiVisualizerService); Argument.IsNotNull(() => appDataService); + Argument.IsNotNull(() => typeFactory); + Argument.IsNotNull(() => encryptionService); + Argument.IsNotNull(() => openFileService); + Argument.IsNotNull(() => fileService); _screenCaptureService = screenCaptureService; _systemInfoService = systemInfoService; + _messageService = messageService; _uiVisualizerService = uiVisualizerService; _appDataService = appDataService; - + _typeFactory = typeFactory; + _encryptionService = encryptionService; + _openFileService = openFileService; + _fileService = fileService; Screenshot = new TaskCommand(OnScreenshotExecuteAsync); ShowSystemInfo = new TaskCommand(OnShowSystemInfoExecuteAsync); SavePackage = new TaskCommand(OnSavePackageExecuteAsync); + EncryptAndSavePackage = new TaskCommand(OnEncryptAndSavePackageExecuteAsync); + GenerateKeys = new TaskCommand(OnGenerateKeysExecuteAsync); + DecryptPackage = new TaskCommand(OnDecryptPackageExecuteAsync); Title = "Orc.SupportPackage example"; + + var currentDirectory = Environment.CurrentDirectory; + PublicKeyPath = Path.Combine(currentDirectory, "public.pem"); + PrivateKeyPath = Path.Combine(currentDirectory, "private.pem"); } #region Commands @@ -52,6 +76,66 @@ private async Task OnSavePackageExecuteAsync() await _uiVisualizerService.ShowDialogAsync(); } + public TaskCommand EncryptAndSavePackage { get; private set; } + + private async Task OnEncryptAndSavePackageExecuteAsync() + { + var supportPackageViewModel = _typeFactory.CreateInstance(); + supportPackageViewModel.EncryptionContext = new EncryptionContext + { + PrivateKeyPath = PrivateKeyPath, + PublicKey = await _encryptionService.ReadPublicKeyFromPemFileAsync(PublicKeyPath) + }; + + await _uiVisualizerService.ShowDialogAsync(supportPackageViewModel); + } + + public TaskCommand GenerateKeys { get; private set; } + + private async Task OnGenerateKeysExecuteAsync() + { + _encryptionService.Generate(PrivateKeyPath, PublicKeyPath); + await _messageService.ShowInformationAsync("Encryption keys generated"); + } + + public TaskCommand DecryptPackage { get; private set; } + + private async Task OnDecryptPackageExecuteAsync() + { + var result = await _openFileService.DetermineFileAsync(new DetermineOpenFileContext + { + }); + + if (!result.Result) + { + return; + } + + var directory = Path.GetDirectoryName(result.FileName); + var fileName = Path.GetFileNameWithoutExtension(result.FileName); + + var decryptedPackagePath = Path.Combine(directory, $"{fileName}_dec.spkg"); + + using (var sourceStream = _fileService.OpenRead(result.FileName)) + { + if (_fileService.Exists(decryptedPackagePath)) + { + _fileService.Delete(decryptedPackagePath); + } + + using (var targetStream = _fileService.Create(decryptedPackagePath)) + { + await _encryptionService.DecryptAsync(sourceStream, targetStream, new EncryptionContext + { + PrivateKeyPath = PrivateKeyPath, + PublicKey = await _encryptionService.ReadPublicKeyFromPemFileAsync(PublicKeyPath) + }); + } + } + + await _messageService.ShowInformationAsync($"Decrypted support package saved on path {decryptedPackagePath}"); + } + public TaskCommand Screenshot { get; private set; } private async Task OnScreenshotExecuteAsync() @@ -85,12 +169,17 @@ private async Task OnShowSystemInfoExecuteAsync() var sysInfoLines = sysInfoElements.Select(x => x.ToString()); SystemInfo = string.Join("\n", sysInfoLines); } + #endregion #region Properties public BitmapImage ScreenPic { get; private set; } public string SystemInfo { get; set; } + + public string PrivateKeyPath { get; set; } + + public string PublicKeyPath { get; set; } #endregion } } diff --git a/src/Orc.SupportPackage.Example/Views/MainView.xaml b/src/Orc.SupportPackage.Example/Views/MainView.xaml index 50488903..bdb7dc8e 100644 --- a/src/Orc.SupportPackage.Example/Views/MainView.xaml +++ b/src/Orc.SupportPackage.Example/Views/MainView.xaml @@ -41,7 +41,55 @@ - + +