public static string Decrypt(string plainText) { return(SymmetricEncryption.Decrypt <AesManaged>(plainText, password)); }
public static void ProcessCallQueueItem(CallQueueItem cqi) { try { if (cqi != null && cqi.Call != null && cqi.Call.HasAnyDispatches()) { if (_communicationService == null) { _communicationService = Bootstrapper.GetKernel().Resolve <ICommunicationService>(); } if (_callsService == null) { _callsService = Bootstrapper.GetKernel().Resolve <ICallsService>(); } List <int> groupIds = new List <int>(); /* Trying to see if I can eek out a little perf here now that profiles are in Redis. Previously the * the parallel operation would cause EF errors. This shouldn't be the case now because profiles are * cached and GetProfileForUser operations will hit that first. */ if (cqi.Profiles == null || !cqi.Profiles.Any()) { if (_userProfilesService == null) { _userProfilesService = Bootstrapper.GetKernel().Resolve <IUserProfileService>(); } cqi.Profiles = _userProfilesService.GetAllProfilesForDepartment(cqi.Call.DepartmentId).Select(x => x.Value).ToList(); } if (cqi.CallDispatchAttachmentId > 0) { //var callsService = Bootstrapper.GetKernel().Resolve<ICallsService>(); cqi.Call.ShortenedAudioUrl = _callsService.GetShortenedAudioUrl(cqi.Call.CallId, cqi.CallDispatchAttachmentId); } cqi.Call.ShortenedCallUrl = _callsService.GetShortenedCallLinkUrl(cqi.Call.CallId); try { cqi.Call.CallPriority = _callsService.GetCallPrioritesById(cqi.Call.DepartmentId, cqi.Call.Priority, false); } catch { /* Doesn't matter */ } var dispatchedUsers = new HashSet <string>(); // Dispatch Personnel if (cqi.Call.Dispatches != null && cqi.Call.Dispatches.Any()) { Parallel.ForEach(cqi.Call.Dispatches, d => { dispatchedUsers.Add(d.UserId); try { var profile = cqi.Profiles.FirstOrDefault(x => x.UserId == d.UserId); if (profile != null) { _communicationService.SendCall(cqi.Call, d, cqi.DepartmentTextNumber, cqi.Call.DepartmentId, profile, cqi.Address); } } catch (SocketException sex) { } }); } if (_departmentGroupsService == null) { _departmentGroupsService = Bootstrapper.GetKernel().Resolve <IDepartmentGroupsService>(); } // Dispatch Groups if (cqi.Call.GroupDispatches != null && cqi.Call.GroupDispatches.Any()) { foreach (var d in cqi.Call.GroupDispatches) { if (!groupIds.Contains(d.DepartmentGroupId)) { groupIds.Add(d.DepartmentGroupId); } var members = _departmentGroupsService.GetAllMembersForGroup(d.DepartmentGroupId); foreach (var member in members) { if (!dispatchedUsers.Contains(member.UserId)) { dispatchedUsers.Add(member.UserId); try { var profile = cqi.Profiles.FirstOrDefault(x => x.UserId == member.UserId); _communicationService.SendCall(cqi.Call, new CallDispatch() { UserId = member.UserId }, cqi.DepartmentTextNumber, cqi.Call.DepartmentId, profile, cqi.Address); } catch (SocketException sex) { } catch (Exception ex) { Logging.LogException(ex); } } } } } // Dispatch Units if (cqi.Call.UnitDispatches != null && cqi.Call.UnitDispatches.Any()) { if (_unitsService == null) { _unitsService = Bootstrapper.GetKernel().Resolve <IUnitsService>(); } foreach (var d in cqi.Call.UnitDispatches) { var unit = _unitsService.GetUnitById(d.UnitId); if (unit != null && unit.StationGroupId.HasValue) { if (!groupIds.Contains(unit.StationGroupId.Value)) { groupIds.Add(unit.StationGroupId.Value); } } _communicationService.SendUnitCall(cqi.Call, d, cqi.DepartmentTextNumber, cqi.Address); var unitAssignedMembers = _unitsService.GetCurrentRolesForUnit(d.UnitId); if (unitAssignedMembers != null && unitAssignedMembers.Count() > 0) { foreach (var member in unitAssignedMembers) { if (!dispatchedUsers.Contains(member.UserId)) { dispatchedUsers.Add(member.UserId); try { var profile = cqi.Profiles.FirstOrDefault(x => x.UserId == member.UserId); _communicationService.SendCall(cqi.Call, new CallDispatch() { UserId = member.UserId }, cqi.DepartmentTextNumber, cqi.Call.DepartmentId, profile, cqi.Address); } catch (SocketException sex) { } catch (Exception ex) { Logging.LogException(ex); } } } } else { if (unit.StationGroupId.HasValue) { var members = _departmentGroupsService.GetAllMembersForGroup(unit.StationGroupId.Value); foreach (var member in members) { if (!dispatchedUsers.Contains(member.UserId)) { dispatchedUsers.Add(member.UserId); try { var profile = cqi.Profiles.FirstOrDefault(x => x.UserId == member.UserId); _communicationService.SendCall(cqi.Call, new CallDispatch() { UserId = member.UserId }, cqi.DepartmentTextNumber, cqi.Call.DepartmentId, profile, cqi.Address); } catch (SocketException sex) { } catch (Exception ex) { Logging.LogException(ex); } } } } } } } // Dispatch Roles if (cqi.Call.RoleDispatches != null && cqi.Call.RoleDispatches.Any()) { if (_rolesService == null) { _rolesService = Bootstrapper.GetKernel().Resolve <IPersonnelRolesService>(); } foreach (var d in cqi.Call.RoleDispatches) { var members = _rolesService.GetAllMembersOfRole(d.RoleId); foreach (var member in members) { if (!dispatchedUsers.Contains(member.UserId)) { dispatchedUsers.Add(member.UserId); try { var profile = cqi.Profiles.FirstOrDefault(x => x.UserId == member.UserId); _communicationService.SendCall(cqi.Call, new CallDispatch() { UserId = member.UserId }, cqi.DepartmentTextNumber, cqi.Call.DepartmentId, profile, cqi.Address); } catch (SocketException sex) { } catch (Exception ex) { Logging.LogException(ex); } } } } } // Send Call Print to Printer if (_printerProvider == null) { _printerProvider = Bootstrapper.GetKernel().Resolve <IPrinterProvider>(); } Dictionary <int, DepartmentGroup> fetchedGroups = new Dictionary <int, DepartmentGroup>(); if (cqi.Call.Dispatches != null && cqi.Call.Dispatches.Any()) { foreach (var d in cqi.Call.Dispatches) { var group = _departmentGroupsService.GetGroupForUser(d.UserId, cqi.Call.DepartmentId); if (group != null) { if (!groupIds.Contains(group.DepartmentGroupId)) { groupIds.Add(group.DepartmentGroupId); } if (!fetchedGroups.ContainsKey(group.DepartmentGroupId)) { fetchedGroups.Add(group.DepartmentGroupId, group); } } } } foreach (var groupId in groupIds) { try { DepartmentGroup group = null; if (fetchedGroups.ContainsKey(groupId)) { group = fetchedGroups[groupId]; } else { group = _departmentGroupsService.GetGroupById(groupId); } if (!String.IsNullOrWhiteSpace(group.PrinterData) && group.DispatchToPrinter) { var printerData = JsonConvert.DeserializeObject <DepartmentGroupPrinter>(group.PrinterData); var apiKey = SymmetricEncryption.Decrypt(printerData.ApiKey, Config.SystemBehaviorConfig.ExternalLinkUrlParamPassphrase); var callUrl = _callsService.GetShortenedCallPdfUrl(cqi.Call.CallId, true, groupId); var printJob = _printerProvider.SubmitPrintJob(apiKey, printerData.PrinterId, "CallPrint", callUrl); } } catch (Exception ex) { Logging.LogException(ex); } } } } finally { _communicationService = null; } }
public void UseCase_Symmetric() { string sharedPassword; (SymmetricEncryption.EncryptedDataContainer encryptedDataContainer, byte[] salt)fileforBob; string alicePlainText; string bobPlainText; // Alice { var plain = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; alicePlainText = plain; outputHelper.WriteLine($"Alice wants to encrypt: {plain}"); var alicePassword = "******"; outputHelper.WriteLine($"Alice will use password: {alicePassword}"); sharedPassword = alicePassword; outputHelper.WriteLine($"Alice tells Bob the password"); var(key, salt) = Sample.KeyGeneration.CreateFromPassword(alicePassword, 256); outputHelper.WriteLine($"The key derivation algorithm will create this key: {key.ToText()}"); outputHelper.WriteLine($"The key derivation algorithm will create this salt: {salt.ToText()}"); outputHelper.WriteLine($"The key derivation settings are known"); outputHelper.WriteLine($"HashAlgorithm: {Sample.KeyGeneration.HashAlgorithmSha256.ToString()}"); outputHelper.WriteLine($"SaltSizeInBits: {Sample.KeyGeneration.SaltSizeInBits}"); outputHelper.WriteLine($"Iterations: {Sample.KeyGeneration.Iterations:N0}"); outputHelper.WriteLine($"Alice will encrypt this data with AES GCM."); var associatedData = DateTime.Now.ToString(CultureInfo.InvariantCulture); outputHelper.WriteLine($"Alice will this unencrypted information: {associatedData}"); var encryptedDataContainer = SymmetricEncryption.Encrypt(key, plain.ToData(), associatedData.ToData()); outputHelper.WriteLine($"This is the ciphertext: {encryptedDataContainer.CipherText.ToText()}"); outputHelper.WriteLine($"This is the tag, the authentication tag derived from the ciphertext: {encryptedDataContainer.Nonce.ToText()}"); outputHelper.WriteLine($"This is the nonce: {encryptedDataContainer.Tag.ToText()}"); fileforBob = (encryptedDataContainer : encryptedDataContainer, salt : salt); outputHelper.WriteLine("Alice sends ciphertext, tag, nonce and salt to bob"); } outputHelper.WriteLine("--------------------------------------------------"); // Bob { outputHelper.WriteLine($"Bob got the passwort from alice and the ciphertext, tag, nonce, associatedData and salt"); outputHelper.WriteLine(JsonConvert.SerializeObject(fileforBob.encryptedDataContainer, Formatting.Indented)); outputHelper.WriteLine($"Salt: {fileforBob.salt}"); outputHelper.WriteLine($"Bob will use the password: {sharedPassword}"); var(key, _) = Sample.KeyGeneration.CreateFromPassword(sharedPassword, 256, fileforBob.salt); outputHelper.WriteLine($"The key derivation algorithm will create this key: {key.ToText()}"); outputHelper.WriteLine($"Bob will decrypt this data with AES GCM."); outputHelper.WriteLine($"Bob make shure, that the associatedData is as expected: {fileforBob.encryptedDataContainer.AssociatedData.ToUtf8String()}"); var plainData = SymmetricEncryption.Decrypt(key, fileforBob.encryptedDataContainer); bobPlainText = plainData.ToUtf8String(); outputHelper.WriteLine($"Bob decrypted this data: {plainData.ToUtf8String()}"); outputHelper.WriteLine("Because we use an authenticated encryption with associated data (AEAD) Bob can be sure, that this data wasn't change."); } alicePlainText.Should().BeEquivalentTo(bobPlainText, "The plain text must be the same."); }