Exemplo n.º 1
0
        public void Streams()
        {
            // Verify that we can encrypt and decrypt streams.

            var decryptedBytes = new byte[32 * 1024];
            var encryptedBytes = (byte[])null;
            var key            = string.Empty;

            // Encrypt some bytes.

            for (int i = 0; i < decryptedBytes.Length; i++)
            {
                decryptedBytes[i] = (byte)i;
            }

            using (var decryptedStream = new MemoryStream(decryptedBytes))
            {
                using (var encryptedStream = new MemoryStream())
                {
                    using (var cipher = new AesCipher())
                    {
                        cipher.EncryptStream(decryptedStream, encryptedStream);

                        // Save the key and encrypted data so we can test decryption below.

                        encryptedBytes = encryptedStream.ToArray();
                        key            = cipher.Key;
                    }

                    // Verify that the two streams haven't been dispoed.

                    decryptedStream.Position = 0;
                    encryptedStream.Position = 0;
                }
            }

            // Decrypt the encypted data and verify.

            using (var encryptedStream = new MemoryStream(encryptedBytes))
            {
                using (var decryptedStream = new MemoryStream())
                {
                    using (var cipher = new AesCipher(key))
                    {
                        cipher.DecryptStream(encryptedStream, decryptedStream);
                    }

                    // Verify that the decrypted data matches the original.

                    Assert.Equal(decryptedBytes, decryptedStream.ToArray());
                }
            }
        }