public async Task <object> GetAsync(string key, Type type)
        {
            var encrypted = await m_inner.GetAsync(key, typeof(Encrypted)) as Encrypted;

            if (encrypted == null)
            {
                return(null);
            }

            try
            {
                string decryptedValue = m_cryptographer.Decrypt(m_encryptionKey, encrypted);
                using (var reader = new StringReader(decryptedValue))
                {
                    if (type == typeof(string))
                    {
                        return(reader.ReadToEnd());
                    }

                    return(HealthVaultClient.Serializer.Deserialize(reader, type, null));
                }
            }
            catch (Exception)
            {
                return(null);
            }
        }
Beispiel #2
0
        public int[] GetIntArray(byte[] byteArray)
        {
            int byteCount = byteArray.Length;

            if (byteCount % 4 != 0)
            {
                throw new Exception("Wrong size of binary field (2): " + byteCount.ToString());
            }
            int intCount = byteCount / 4;

            int[] intArray = new int[intCount];
            Buffer.BlockCopy(byteArray, 0, intArray, 0, byteCount);
            if (cryptographer != null)
            {
                cryptographer.Decrypt(intArray, intCount);
            }
            return(intArray);
        }
Beispiel #3
0
        public T GetValue <T>(string key, bool expireOnceRead)
        {
            var cookie = HttpContext.Current.Request.Cookies[key];
            T   value  = default(T);

            if (cookie != null)
            {
                if (!string.IsNullOrWhiteSpace(cookie.Value))
                {
                    var converter = TypeDescriptor.GetConverter(typeof(T));
                    try
                    {
                        value = (T)converter.ConvertFromString(_cryptographer.Decrypt(cookie.Value));
                    }
                    catch (NotSupportedException)
                    {
                        if (converter.CanConvertFrom(typeof(string)))
                        {
                            value = (T)converter.ConvertFrom(_cryptographer.Decrypt(cookie.Value));
                        }
                    }
                }

                if (expireOnceRead)
                {
                    cookie = HttpContext.Current.Response.Cookies[key];

                    if (cookie != null)
                    {
                        cookie.Expires = DateTime.Now.AddDays(-100d);
                    }
                }
            }

            return(value);
        }
Beispiel #4
0
        /// <summary>
        /// Returns the result of an attempt of autenticate.
        /// </summary>
        /// <param name="password">Entered password.</param>
        /// <returns></returns>
        public async Task <bool> Autenticate(string password, string encryptedPassword)
        {
            if (password == null || encryptedPassword == null)
            {
                throw new ArgumentNullException();
            }

            await Task.Delay(rng.Next(authDelay - authDelayRange, authDelay + authDelayRange));

            if (cryptographer != null)
            {
                cryptographer.ChangeKey(password);
                return(cryptographer.Decrypt(encryptedPassword) == password);
            }
            else
            {
                return(encryptedPassword == password);
            }
        }
Beispiel #5
0
        /*----------------------------------------------------------------------------------------------------
        * Return a copy of <TModel> with encrypted/decrypted string properties.
        *  ----------------------------------------------------------------------------------------------------*/
        private TModel ApplyCryptography(TModel model, CryptographyMode mode)
        {
            dynamic cryptedModel = Activator.CreateInstance(typeof(TModel));

            foreach (var prop in model.GetType().GetProperties())
            {
                if (prop.PropertyType == typeof(string))
                {
                    string propertyValue = prop.GetValue(model).ToString();
                    if (mode == CryptographyMode.Decrypt)
                    {
                        prop.SetValue(cryptedModel, cryptographer.Decrypt(propertyValue));
                    }
                    else if (mode == CryptographyMode.Encrypt)
                    {
                        prop.SetValue(cryptedModel, cryptographer.Encypt(propertyValue));
                    }
                }
            }
            return(cryptedModel);
        }
Beispiel #6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="algorithm"></param>
        /// <param name="encryptedBase64ConnectString"></param>
        /// <returns></returns>
        public static string Decrypt(CryptoAlgorithm algorithm, string encryptedBase64ConnectString)
        {
            ICryptographer cryptographer = GetCryptographer(algorithm);

            return(cryptographer.Decrypt(encryptedBase64ConnectString));
        }