예제 #1
0
        protected override object GetInternal(string key)
        {
            object value = null;

            try
            {
                if (_directoryValid)
                {
                    //check for a non-expiring cache:
                    var cachePath = GetFilePath(key);
                    if (!File.Exists(cachePath))
                    {
                        cachePath = null;
                        //check for expired caches:
                        var fileName       = GetFileNameSearchPattern(key);
                        var existingCaches = Directory.EnumerateFiles(_directory, fileName).OrderByDescending(x => x);
                        if (existingCaches.Count() > 0)
                        {
                            var mostRecentCache = existingCaches.ElementAt(0);
                            //if the most recent cache is live, return it -
                            //format is {key}.cache.{expiresAt}.expiry
                            if (mostRecentCache.EndsWith(".expiry"))
                            {
                                var expiresAt     = mostRecentCache.Substring(mostRecentCache.IndexOf(".expiry") - 19, 19);
                                var expiresAtDate = expiresAt.Replace('-', '/').Replace('_', ':');
                                var expiryDate    = DateTime.Parse(expiresAtDate);
                                if (expiryDate > DateTime.UtcNow)
                                {
                                    cachePath = Path.Combine(_directory, mostRecentCache);
                                }
                                else
                                {
                                    var deleteKey = key;
                                    Task.Factory.StartNew(() => DeleteFile(deleteKey));
                                }
                            }
                        }
                    }
                    if (cachePath != null)
                    {
                        if (CacheConfiguration.Current.DiskCache.EncryptItems)
                        {
                            var encryptedBytes = File.ReadAllBytes(cachePath);
                            value = SimpleAes.Decrypt(encryptedBytes);
                        }
                        else
                        {
                            value = File.ReadAllText(cachePath);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Warn("DiskCache.GetInternal - failed, item not returned. Message: {0}", ex.Message);
            }
            return(value);
        }
예제 #2
0
        public void TestSymetrical()
        {
            const string original = "this is a test string";

            using (var aes = new SimpleAes())
            {
                var enc = aes.Encrypt(original);
                Debug.WriteLine("encrypted = " + enc);
                string decrypted = aes.Decrypt(enc);
                Assert.AreEqual(original, decrypted);

                var secondEnc = aes.Encrypt(original);
                Assert.AreNotEqual(secondEnc, enc);
            }
        }
        public string DecryptAESFromBase64String(string cipher)
        {
            string htmlUnsafeCipher = cipher.Replace('_', '/').Replace('-', '+');

            switch (cipher.Length % 4)
            {
            case 2:
            {
                htmlUnsafeCipher += "==";
                break;
            }

            case 3:
            {
                htmlUnsafeCipher += "=";
                break;
            }
            }

            var cipherBytes = Convert.FromBase64String(htmlUnsafeCipher);

            return(SimpleAes.Decrypt(cipherBytes));
        }
 public string DecryptAES(byte[] cipherBytes)
 {
     return(SimpleAes.Decrypt(cipherBytes));
 }