Пример #1
0
        private async void DecryptButton_Click(object sender, EventArgs e)
        {
            var RemoveButtonState = DisableAllControls();

            if (String.IsNullOrEmpty(TextInputEditor.Text) && fileData == null)
            {
                RestoreAllControls(RemoveButtonState);
                ShowDialog("Error", "Choose file or write/paste text to encrypt!");
                DecryptButton.Enabled = false;
                return;
            }
            if (!String.IsNullOrEmpty(TextInputEditor.Text) && fileData != null)
            {
                RestoreAllControls(RemoveButtonState);
                ShowDialog("Error", "You can encrypt either file or text at a time!");
                return;
            }
            string password = "";

            try
            {
                password = await SecureStorage.GetAsync("AppPassword");

                if (password == null)
                {
                    ShowDialog("Error", "App's password is missing. Close application and try again!");
                    return;
                }
            }
            catch (Exception)
            {
                ShowDialog("Error", "App's password is missing. Close application and try again!");
                return;
            }
            if (!PasswordSwitch.Checked)
            {
                if (String.IsNullOrEmpty(CustomPasswordEntry.Text) || CustomPasswordEntry.Text.Length < 6 || CustomPasswordEntry.Text.Length > 16)
                {
                    RestoreAllControls(RemoveButtonState);
                    ShowDialog("Error", "Custom Password length should be at least 6 and at most 16");
                    return;
                }
                password = CustomPasswordEntry.Text;
            }
            if (fileData == null)
            {
                DecryptionResult decryptionResult = null;
                await Task.Run(() =>
                {
                    decryptionResult = DecryptionService.DecryptText(TextInputEditor.Text, password);
                });

                if (decryptionResult.Result)
                {
                    ClipboardManager clipboardManager = (ClipboardManager)this.GetSystemService(Context.ClipboardService);
                    clipboardManager.PrimaryClip = ClipData.NewPlainText("Decrypted Text", decryptionResult.DecryptedString);
                    Toast.MakeText(this, "Decrypted text copied to clipboard", ToastLength.Long);
                    fileData = null;
                    RestoreAllControls(ViewStates.Invisible);
                    RemoveButton_Click(null, null);
                    TextInputEditor.Text     = String.Empty;
                    CustomPasswordEntry.Text = String.Empty;
                    PasswordSwitch.Checked   = true;
                    ShowDialog("Decrypted Text", decryptionResult.DecryptedString);
                }
                else
                {
                    RestoreAllControls(RemoveButtonState);
                    ShowDialog("Failed", "Decryption failed. This text can not be encrypted. Error: \"" + decryptionResult.Error + "\"");
                }
            }
            else
            {
                DecryptionResult decryptionResult = null;
                await Task.Run(() =>
                {
                    decryptionResult = DecryptionService.DecryptFile(fileData, password);
                });

                if (decryptionResult.Result)
                {
                    RestoreAllControls(ViewStates.Invisible);
                    ShowDialog("Success", "File decrypted. Decrypted filename is \"" + decryptionResult.DecryptedString + "\" and stored at \"Crypto App" + "\" folder.");
                    fileData = null;
                    RemoveButton_Click(null, null);
                    TextInputEditor.Text     = String.Empty;
                    CustomPasswordEntry.Text = String.Empty;
                    PasswordSwitch.Checked   = true;
                }
                else if (decryptionResult.Result == false && decryptionResult.Error == "Password")
                {
                    RestoreAllControls(RemoveButtonState);
                    PasswordSwitch.Checked         = false;
                    CustomPasswordEntry.Visibility = ViewStates.Visible;
                    ShowDialog("Error", "Password is wrong. Enter correct password to decrypt.");
                    return;
                }
                else
                {
                    RestoreAllControls(RemoveButtonState);
                    ShowDialog("Failed", "File decryption failed. This file can not be decrypted.Error: \"" + decryptionResult.Error + "\"");
                }
            }
        }
Пример #2
0
        private async void EncryptButton_Click(object sender, RoutedEventArgs e)
        {
            bool RemoveButtonState = DisableAllControls();

            if (String.IsNullOrEmpty(TextInputEditor.Text) && fileData == null)
            {
                RestoreAllControls(RemoveButtonState);
                await new ContentDialog()
                {
                    Title = "Error", Content = "Choose file or paste text to decrypt!", CloseButtonText = "Okay"
                }.ShowAsync();
                EncryptButton.IsEnabled = false;
                return;
            }
            string password = "******";

            if (!PasswordSwitch.IsOn)
            {
                if (String.IsNullOrEmpty(CustomPasswordEntry.Text) || CustomPasswordEntry.Text.Length < 6 || CustomPasswordEntry.Text.Length > 16)
                {
                    RestoreAllControls(RemoveButtonState);
                    CustomPasswordEntry.Focus(FocusState.Programmatic);
                    await new ContentDialog()
                    {
                        Title = "Error", Content = "Custom Password length should be at least 6 and at most 16", CloseButtonText = "Okay"
                    }.ShowAsync();
                    return;
                }
                password = CustomPasswordEntry.Text;
            }
            if (fileData == null)
            {
                DecryptionResult decryptionResult = null;
                string           plainText        = TextInputEditor.Text;
                await Task.Run(() =>
                {
                    decryptionResult = DecryptionService.DecryptText(plainText, password);
                });

                if (decryptionResult.Result)
                {
                    var FormatedTextSize = DecryptionService.GetFormatedSize(System.Text.Encoding.UTF8.GetByteCount(decryptionResult.DecryptedString));
                    var result           = await new ContentDialog()
                    {
                        Title = "Decrypted Text", Content = decryptionResult.DecryptedString, PrimaryButtonText = "Copy decrypted text", CloseButtonText = "Do not copy"
                    }.ShowAsync();
                    if (result == ContentDialogResult.Primary)
                    {
                        var DP = new DataPackage();
                        DP.SetText(decryptionResult.DecryptedString);
                        Clipboard.SetContent(DP);
                    }
                    fileData = null;
                    RestoreAllControls(false);
                    RemoveButton_Click(null, null);
                    TextInputEditor.Text     = String.Empty;
                    CustomPasswordEntry.Text = String.Empty;
                    PasswordSwitch.IsOn      = true;
                }
                else
                {
                    await new ContentDialog()
                    {
                        Title = "Failed", Content = "Decryption failed. This text can not be decrypted. Error: \"" + decryptionResult.Error + "\"", CloseButtonText = "Okay"
                    }.ShowAsync();
                    RestoreAllControls(RemoveButtonState);
                }
            }
            else
            {
                DecryptionResult decryptionResult = null;
                await Task.Run(async() =>
                {
                    decryptionResult = await DecryptionService.DecryptFile(fileData, password);
                });

                if (decryptionResult.Result)
                {
                    ActivityText.Text = "Do not close the application. Saving decrypted file.";
                    FileSavePicker savePicker        = new FileSavePicker();
                    var            FileNameExtension = System.IO.Path.GetExtension(decryptionResult.WritePath);
                    savePicker.FileTypeChoices.Add("Crypto App Decrypted File", new List <string>()
                    {
                        FileNameExtension
                    });
                    savePicker.SuggestedFileName = System.IO.Path.GetFileName(decryptionResult.WritePath);
                    var writeFile = await savePicker.PickSaveFileAsync();

                    if (writeFile != null)
                    {
                        CachedFileManager.DeferUpdates(writeFile);
                        await FileIO.WriteBytesAsync(writeFile, decryptionResult.DecryptedContents);

                        FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(writeFile);

                        if (status == FileUpdateStatus.Complete)
                        {
                            await new ContentDialog()
                            {
                                Title = "Success", Content = "File is decrypted and saved as " + writeFile.Name, CloseButtonText = "Okay"
                            }.ShowAsync();
                        }
                        else
                        {
                            await new ContentDialog()
                            {
                                Title = "Failure", Content = "File could not be saved! ", CloseButtonText = "Okay"
                            }.ShowAsync();
                        }
                    }
                    else
                    {
                        await new ContentDialog()
                        {
                            Title = "Cancelled", Content = "File was not saved! ", CloseButtonText = "Okay"
                        }.ShowAsync();
                    }
                    RestoreAllControls(false);
                    fileData = null;
                    RemoveButton_Click(null, null);
                    TextInputEditor.Text     = String.Empty;
                    CustomPasswordEntry.Text = String.Empty;
                    PasswordSwitch.IsOn      = true;
                }
                else if (decryptionResult.Result == false && decryptionResult.Error == "Password")
                {
                    await new ContentDialog()
                    {
                        Title = "Error", Content = "Password is wrong. Enter correct password to decrypt.", CloseButtonText = "Okay"
                    }.ShowAsync();
                    RestoreAllControls(RemoveButtonState);
                    PasswordSwitch.IsOn            = false;
                    CustomPasswordStack.Visibility = Visibility.Visible;
                    CustomPasswordEntry.Focus(FocusState.Programmatic);
                    return;
                }
                else
                {
                    await new ContentDialog()
                    {
                        Title = "Failed", Content = "Decryption failed. This file can not be decrypted. Error: \"" + decryptionResult.Error + "\"", CloseButtonText = "Okay"
                    }.ShowAsync();
                    RestoreAllControls(RemoveButtonState);
                }
            }
        }