public void DecryptedActionEncryptsAfterExecution()
        {
            // Arrange
            var data = new TwitterAccountData
            {
                OAuthToken       = DpApi.Encrypt("token"),
                OAuthTokenSecret = DpApi.Encrypt("secret")
            };

            // Act
            data.ExecuteDecryptedAction(d => { });

            // Assert
            Assert.AreNotEqual("token", data.OAuthToken);
            Assert.AreNotEqual("secret", data.OAuthTokenSecret);
        }
        public void EncryptedFuncPassesThroughReturnValue()
        {
            // Arrange

            var data = new TwitterAccountData
            {
                OAuthToken       = DpApi.Encrypt("token"),
                OAuthTokenSecret = DpApi.Encrypt("secret")
            };

            Func <TwitterAccountData, int> action = d => 42;

            // Act
            var value = data.ExecuteDecryptedAction(action);

            // Assert
            Assert.AreEqual(42, value);
        }
        public void EncryptedActionDecryptsBeforeExecution()
        {
            // Arrange
            var data = new TwitterAccountData
            {
                OAuthToken       = DpApi.Encrypt("token"),
                OAuthTokenSecret = DpApi.Encrypt("secret")
            };

            string decryptedToken              = "";
            string decryptedSecret             = "";
            Action <TwitterAccountData> action = d =>
            {
                decryptedToken  = d.OAuthToken;
                decryptedSecret = d.OAuthTokenSecret;
            };

            // Act
            data.ExecuteDecryptedAction(action);

            // Assert
            Assert.AreEqual("token", decryptedToken);
            Assert.AreEqual("secret", decryptedSecret);
        }
示例#4
0
        private async void BtStart_OnClick(object sender, RoutedEventArgs e)
        {
            if (_selectedFile == null || _selectedFileInfo == null || CoBAlgorithm.SelectedItem == null ||
                string.IsNullOrWhiteSpace(TbFileDestination.Text))
            {
                return;
            }


            BtChooseFile.IsEnabled         = false;
            BtChecksumChooseFile.AllowDrop = false;
            RbEncrypt.IsEnabled            = false;
            RbDecrypt.IsEnabled            = false;
            TbFileDestination.IsEnabled    = false;
            CoBAlgorithm.IsEnabled         = false;
            PbPassword.IsEnabled           = false;
            RbThisAccount.IsEnabled        = false;
            RbThisComputer.IsEnabled       = false;
            BtStart.IsEnabled = false;

            PrBFileEncryption.Value   = 0;
            PrBFileEncryption.Maximum = _selectedFileInfo.Length;
            TblSpeed.Text             = "0 MiB/s";
            TblProgress.Text          = $"0 / {_lengthInMiB} MiB";

            try
            {
                var encrypt = RbEncrypt.IsChecked == true;

                if (CoBAlgorithm.Text == "Windows Data Protection (DPAPI)")
                {
                    PrBFileEncryption.IsIndeterminate = true;

                    var dpApi = new DpApi(_selectedFileInfo, TbFileDestination.Text,
                                          RbThisAccount.IsChecked == true
                            ? DataProtectionScope.CurrentUser
                            : DataProtectionScope.LocalMachine);

                    if (encrypt)
                    {
                        await dpApi.Encrypt();
                    }
                    else
                    {
                        await dpApi.Decrypt();
                    }
                }
                else
                {
                    using (var algorithm = ((Algorithm)CoBAlgorithm.SelectedIndex).GetAlgorithm())
                    {
                        if (encrypt)
                        {
                            var encryption = new Encryption
                            {
                                MainWindow  = this,
                                Algorithm   = algorithm,
                                Source      = _selectedFileInfo,
                                Destination = TbFileDestination.Text,
                                Password    = PbPassword.Password,
                                LengthInMiB = _lengthInMiB
                            };
                            await encryption.Encrypt();
                        }
                        else
                        {
                            var decryption = new Decryption
                            {
                                MainWindow  = this,
                                Algorithm   = algorithm,
                                Source      = _selectedFileInfo,
                                Destination = TbFileDestination.Text,
                                Password    = PbPassword.Password,
                                LengthInMiB = _lengthInMiB
                            };
                            await decryption.Decrypt();
                        }
                    }
                }

                await this.ShowMessageAsync("LCrypt",
                                            string.Format(Localization.SuccessfullyEncrypted, _selectedFileInfo.Name, Path.GetFileName(TbFileDestination.Text), CoBAlgorithm.Text),
                                            MessageDialogStyle.Affirmative, new MetroDialogSettings
                {
                    AffirmativeButtonText = "OK",
                    AnimateShow           = true,
                    AnimateHide           = false
                });
            }
            catch (Exception)
            {
                await this.ShowMessageAsync("LCrypt",
                                            string.Format(Localization.EncryptionDecryptionFailed, _selectedFileInfo.Name),
                                            MessageDialogStyle.Affirmative, new MetroDialogSettings
                {
                    AffirmativeButtonText = "OK",
                    AnimateShow           = true,
                    AnimateHide           = false
                });
            }
            finally
            {
                BtChooseFile.IsEnabled         = true;
                BtChecksumChooseFile.AllowDrop = false;
                RbEncrypt.IsEnabled            = true;
                RbDecrypt.IsEnabled            = true;
                TbFileDestination.IsEnabled    = true;
                CoBAlgorithm.IsEnabled         = true;
                PbPassword.IsEnabled           = true;
                RbThisAccount.IsEnabled        = true;
                RbThisComputer.IsEnabled       = true;
                BtStart.IsEnabled = true;

                PrBFileEncryption.Maximum         = 1;
                PrBFileEncryption.Value           = 0;
                PrBFileEncryption.IsIndeterminate = false;
                TblSpeed.Text    = "- MiB/s";
                TblProgress.Text = "0 / - MiB";
            }
        }
示例#5
0
 public void Encrypt()
 {
     OAuthToken       = DpApi.Encrypt(DpApi.KeyType.UserKey, OAuthToken);
     OAuthTokenSecret = DpApi.Encrypt(DpApi.KeyType.UserKey, OAuthTokenSecret);
 }