public void UserLevelEncryptionCanBeDecrypted() { // Arrange const string plain = "Hello World this is a test with some text"; // Act string encrypted = DpApi.Encrypt(DpApi.KeyType.UserKey, plain); string decrypted = DpApi.Decrypt(encrypted); // Assert Assert.AreNotEqual(plain, encrypted); Assert.AreEqual(plain, decrypted); }
public void MachineLevelEncryptionCanBeDecrypted() { // Arrange string plain = "Hello World this is a test with some text"; // Act string encrypted = DpApi.Encrypt(plain); string decrypted = DpApi.Decrypt(encrypted); // Assert Assert.AreNotEqual(plain, encrypted); Assert.AreEqual(plain, decrypted); }
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"; } }
public void Decrypt() { OAuthToken = DpApi.Decrypt(OAuthToken); OAuthTokenSecret = DpApi.Decrypt(OAuthTokenSecret); }