public byte[] DecryptKey(byte[] cipherText) { string cacheKey = CreateCacheKey(cipherText, context: null); byte[] result; if (_lruCache.TryGet(cacheKey, out result)) { return(result); } result = _actualDataKeyProvider.DecryptKey(cipherText); _lruCache.Add(cacheKey, result); return(result); }
public Stream Decrypt(byte[] dataKey, Stream ciphertextStream, IDictionary <string, string> context) { byte[] plaintextKey = _dataKeyProvider.DecryptKey(dataKey, context); var iv = new byte[IVBytes]; if (!TryFillBuffer(ciphertextStream, iv)) { throw new CryptographicException("not enough data in input stream"); } ISymmetricAlgorithm algo = null; try { algo = _algorithmFactory.CreateAlgorithm(_config); algo.Key = plaintextKey; algo.IV = iv; ICryptoTransform decryptor = algo.CreateDecryptor(); Stream cryptoStream = new CryptoStream(ciphertextStream, decryptor, CryptoStreamMode.Read); // when this stream is disposed, the algo and decryptor will be, too. return(new StreamWithDisposables(cryptoStream, new IDisposable[] { algo, decryptor })); } catch (Exception e) { // If we had trouble creating the stream, destroy the algorithm to prevent the key leaking. if (algo != null) { try { algo.Dispose(); } catch (Exception disposalException) { throw new AggregateException(e, disposalException); } } throw; } }