Esempio n. 1
0
        public void TEST_ENCRYPT()
        {
            using (FileStream fs = new FileStream(file_name, FileMode.Create, FileAccess.ReadWrite))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.Write(_testTextFileContents);

                    Assert.IsTrue(File.Exists(file_name));
                }
            }

            ISimpleStreamEncryption crypto = new DESStreamEncryption();
            String key = crypto.GetKey();
            String iv = crypto.GetIV();

            using (FileStream fs = new FileStream(_keyFileName, FileMode.Create, FileAccess.ReadWrite))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.Write(key);
                    Assert.IsTrue(File.Exists(_keyFileName));
                }
            }

            using (FileStream fs = new FileStream(_IVFileName, FileMode.Create, FileAccess.ReadWrite))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.Write(iv);
                    Assert.IsTrue(File.Exists(_IVFileName));
                }
            }

            using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(_testTextFileContents)))
            {
                using (Stream encryptedStream = crypto.EncryptStream(ms, key, iv))
                {
                    encryptedStream.Seek(0, SeekOrigin.Begin);

                    MemoryStream eMS = new MemoryStream();
                    encryptedStream.CopyTo(eMS);

                    // On the fly decryption test!
                    String decryptedText = String.Empty;

                    using( Stream decryptedStream = crypto.DecryptStream(eMS, key, iv) ){
                        MemoryStream dMS = new MemoryStream();

                        decryptedStream.CopyTo(dMS);

                        decryptedText = Encoding.ASCII.GetString(dMS.ToArray());

                        Assert.AreEqual(_testTextFileContents, decryptedText);
                    }

                    encryptedStream.Seek(0, SeekOrigin.Begin);
                    using(FileStream fs = new FileStream(encrypt_name, FileMode.Create, FileAccess.Write ) ){
                        encryptedStream.CopyTo(fs);
                    }
                }
            }
        }