Exemplo n.º 1
0
 private static byte[] Encrypt(ISymmetricAlgorithm algorithm, byte[] plaintext)
 {
     using (var outputStream = new MemoryStream())
     {
         using (ICryptoTransform encryptor = algorithm.CreateEncryptor())
         {
             using (var cryptoStream = new CryptoStream(outputStream, encryptor, CryptoStreamMode.Write))
             {
                 // write the IV to the top of the output stream
                 outputStream.Write(algorithm.IV, 0, algorithm.IV.Length);
                 // and then write the rest of the payload as ciphertext
                 cryptoStream.Write(plaintext, 0, plaintext.Length);
                 cryptoStream.FlushFinalBlock();
                 cryptoStream.Flush();
                 return(outputStream.ToArray());
             }
         }
     }
 }
Exemplo n.º 2
0
        public Stream Encrypt(out byte[] dataKey, Stream plaintextStream, IDictionary <string, string> context)
        {
            byte[] plaintextKey;
            _dataKeyProvider.GenerateKey(_config.KeyBits, out plaintextKey, out dataKey, context);

            ISymmetricAlgorithm algo = null;

            try
            {
                algo     = _algorithmFactory.CreateAlgorithm(_config);
                algo.Key = plaintextKey;
                algo.GenerateIV();

                // All these hoops with the concatenated streams, StreamWithDisposable, etc.
                // are to support: using (Stream foo = provider.Encrypt(...)) {...}
                ICryptoTransform encryptor    = algo.CreateEncryptor();
                Stream           ivStream     = new MemoryStream(algo.IV);
                Stream           cryptoStream = new CryptoStream(plaintextStream, encryptor, CryptoStreamMode.Read);
                Stream           streamPair   = new ConcatenatedStream(ivStream, cryptoStream);

                // when this stream is disposed, so will be all of its constituent streams,
                // plus the algorithm and the encryptor.
                return(new StreamWithDisposables(streamPair, new IDisposable[] { algo, encryptor }));
            }
            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;
            }
        }
		private static byte[] Encrypt(ISymmetricAlgorithm algorithm, byte[] plaintext)
		{
			using (var outputStream = new MemoryStream())
			{
				using (ICryptoTransform encryptor = algorithm.CreateEncryptor())
				{
					using (var cryptoStream = new CryptoStream(outputStream, encryptor, CryptoStreamMode.Write))
					{
						// write the IV to the top of the output stream
						outputStream.Write(algorithm.IV, 0, algorithm.IV.Length);
						// and then write the rest of the payload as ciphertext
						cryptoStream.Write(plaintext, 0, plaintext.Length);
						cryptoStream.FlushFinalBlock();
						cryptoStream.Flush();
						return outputStream.ToArray();
					}
				}
			}
		}