public string Decrypt(string cipherText) { if (cipherText == null) { return(null); } if (!_protectionEnabled) { return(cipherText); } Validate(); return(_key.Decrypt(cipherText)); }
private async Task DecryptDatabase(DataProtectionKey key) { using var scope = Services.CreateScope(); // Accounts var scopedAccountRepository = scope.ServiceProvider.GetRequiredService <IAsyncRepository <Account> >(); var accounts = await scopedAccountRepository.Query().ToListAsync(); foreach (var account in accounts) { if (account.Password != null) { account.Password = key.Decrypt(account.Password); } if (account.OtpSecret != null) { account.OtpSecret = key.Decrypt(account.OtpSecret); } } // AppSettings var scopedAppSettingsRepository = scope.ServiceProvider.GetRequiredService <IAsyncRepository <AppSettings> >(); var domainSettings = await scopedAppSettingsRepository.GetByIdAsync(ServerConstants.Domain); if (domainSettings != null) { var settings = JsonConvert.DeserializeObject <LdapSettings>(domainSettings.Value); settings.Password = key.Decrypt(settings.Password); var json = JsonConvert.SerializeObject(settings); domainSettings.Value = json; } var licenseSettings = await scopedAppSettingsRepository.GetByIdAsync(ServerConstants.Licensing); if (licenseSettings != null) { var settings = JsonConvert.DeserializeObject <LicensingSettings>(licenseSettings.Value); settings.ApiKey = key.Decrypt(settings.ApiKey); var json = JsonConvert.SerializeObject(settings); licenseSettings.Value = json; } // HardwareVaultsActivations var scopedHardwareVaultActivationRepository = scope.ServiceProvider.GetRequiredService <IAsyncRepository <HardwareVaultActivation> >(); var hardwareVaultActivations = await scopedHardwareVaultActivationRepository.Query().ToListAsync(); foreach (var hardwareVaultActivation in hardwareVaultActivations) { hardwareVaultActivation.AcivationCode = key.Decrypt(hardwareVaultActivation.AcivationCode); } // HardwareVaults var scopedHardwareVaultRepository = scope.ServiceProvider.GetRequiredService <IAsyncRepository <HardwareVault> >(); var hardwareVaults = await scopedHardwareVaultRepository.Query().ToListAsync(); foreach (var hardwareVault in hardwareVaults) { if (hardwareVault.MasterPassword != null) { hardwareVault.MasterPassword = key.Decrypt(hardwareVault.MasterPassword); } } // HardwareVaultTasks var scopedHardwareVaultTaskRepository = scope.ServiceProvider.GetRequiredService <IAsyncRepository <HardwareVaultTask> >(); var hardwareVaultTasks = await scopedHardwareVaultTaskRepository.Query().ToListAsync(); foreach (var task in hardwareVaultTasks) { if (task.Password != null) { task.Password = key.Decrypt(task.Password); } if (task.OtpSecret != null) { task.OtpSecret = key.Decrypt(task.OtpSecret); } } // SharedAccounts var scopedSharedAccountRepository = scope.ServiceProvider.GetRequiredService <IAsyncRepository <SharedAccount> >(); var sharedAccounts = await scopedSharedAccountRepository.Query().ToListAsync(); foreach (var account in sharedAccounts) { if (account.Password != null) { account.Password = key.Decrypt(account.Password); } if (account.OtpSecret != null) { account.OtpSecret = key.Decrypt(account.OtpSecret); } } // SoftwareVaultInvitations //var scopedSoftwareVaultInvitationRepository = scope.ServiceProvider.GetRequiredService<IAsyncRepository<SoftwareVaultInvitation>>(); //var softwareVaultInvitations = await scopedSoftwareVaultInvitationRepository.Query().ToListAsync(); //foreach (var softwareVaultInvitation in softwareVaultInvitations) //{ // softwareVaultInvitation.ActivationCode = key.Encrypt(softwareVaultInvitation.ActivationCode); //} using (TransactionScope transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { await scopedAccountRepository.UpdateOnlyPropAsync(accounts, new string[] { nameof(Account.Password), nameof(Account.OtpSecret) }); if (domainSettings != null) { await scopedAppSettingsRepository.UpdateAsync(domainSettings); } if (licenseSettings != null) { await scopedAppSettingsRepository.UpdateAsync(licenseSettings); } await scopedHardwareVaultActivationRepository.UpdateOnlyPropAsync(hardwareVaultActivations, new string[] { nameof(HardwareVaultActivation.AcivationCode) }); await scopedHardwareVaultRepository.UpdateOnlyPropAsync(hardwareVaults, new string[] { nameof(HardwareVault.MasterPassword) }); await scopedHardwareVaultTaskRepository.UpdateOnlyPropAsync(hardwareVaultTasks, new string[] { nameof(HardwareVaultTask.Password), nameof(HardwareVaultTask.OtpSecret) }); await scopedSharedAccountRepository.UpdateOnlyPropAsync(sharedAccounts, new string[] { nameof(SharedAccount.Password), nameof(SharedAccount.OtpSecret) }); //await scopedSoftwareVaultInvitationRepository.UpdateOnlyPropAsync(softwareVaultInvitations, new string[] { nameof(SoftwareVaultInvitation.ActivationCode) }); transactionScope.Complete(); } }