private void DecriptData(DataMessageStanza dataMessageStanza) { string cryptoKey = dataMessageStanza.AppData.Where(item => item.Key == "crypto-key").Select(item => item.Value).FirstOrDefault(); string salt = dataMessageStanza.AppData.Where(item => item.Key == "encryption").Select(item => item.Value).FirstOrDefault(); if (cryptoKey != null && salt != null) { byte[] cryptoKeyBytes = UrlSafeBase64Convertor.FromBase64(cryptoKey.Substring(3)); byte[] saltBytes = UrlSafeBase64Convertor.FromBase64(salt.Substring(5)); var decryptedBytes = cryptography.Decrypt(dataMessageStanza.RawData.ToByteArray(), cryptoKeyBytes, saltBytes); Volatile.Read(ref MessageReceived)?.Invoke(this, Encoding.UTF8.GetString(decryptedBytes)); } else { if (cryptoKey != null) { Debug.WriteLine("CryptoKey is missing"); } if (salt != null) { Debug.WriteLine("salt is missing"); } } }
public async Task <FcmRegistration> Register(ulong senderId, string token, byte[] publicKey, byte[] authSecret) { byte[] randomBytes = new byte[16];; using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider()) { rng.GetBytes(randomBytes); } Dictionary <string, string> postData = new Dictionary <string, string>(); postData.Add("authorized_entity", senderId.ToString()); postData.Add("endpoint", $"{sendUrl}/{token}"); postData.Add("encryption_key", UrlSafeBase64Convertor.ToBase64(publicKey)); postData.Add("encryption_auth", UrlSafeBase64Convertor.ToBase64(authSecret)); JObject jsonData = null; using (var httpClient = new HttpClient()) using (var content = new FormUrlEncodedContent(postData)) { httpClient.DefaultRequestHeaders.Clear(); httpClient.DefaultRequestHeaders.ExpectContinue = false; httpClient.DefaultRequestHeaders.ConnectionClose = true; HttpResponseMessage response = await httpClient.PostAsync(subscribeUrl, content).ConfigureAwait(false); if (response.StatusCode == HttpStatusCode.OK) { using (Stream stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false)) { jsonData = (JObject)DeserializeFromStream(stream); } } else { throw new InvalidOperationException("Cannot register to FCM"); } } return(new FcmRegistration((string)((JValue)jsonData["token"]).Value, (string)((JValue)jsonData["pushSet"]).Value)); }