Click here to Skip to main content
15,885,546 members
Articles / Web Development / ASP.NET

Password Protected Stream Using Decorator Pattern

Rate me:
Please Sign up or sign in to vote.
3.81/5 (9 votes)
9 Jan 2012CPOL3 min read 28.5K   718   34  
Builds a password protected stream on top of System.IO.Stream
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SecureApplication.Classes;
using System.IO;

namespace SecureApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PasswordProtectedStream st = new PasswordProtectedStream(new FileStream("C:/pwdsample.txt", FileMode.Create), "12345"); //create instance of our stream
            StreamWriter w = new StreamWriter(st);
            w.Write("Hye this is test"); //write some data
            w.Flush();
            w.Close();
            
            st.Close();
            st.Dispose();

            MessageBox.Show("Data Written Successfully");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            PasswordProtectedStream st = new PasswordProtectedStream(new FileStream("C:/pwdsample.txt", FileMode.Open), "12345"); //create instance of our stream, try changing a password here
            st.On_ReadComplete += new PasswordProtectedStream.ReadCompleteHandler(st_On_ReadComplete); //hook the read complete event
            
            StreamReader w = new StreamReader(st);
            w.ReadToEnd(); //read but don't display the data here, else you won't get anything useful. Readers are welcome if they can provide an implementation which enables us to read the data in usual manner, without the use of event
            w.Close();
            st.Close();
            st.Dispose();
        }

        void st_On_ReadComplete(object s, PasswordProtectedStream.ReadCompleteEventArgs e)
        {
            if (e.ReadStatus == PasswordProtectedStream.ReadCompleteEventArgs.Status.FAILURE) //if wrong password
                MessageBox.Show("Wrong Password");
            else
                MessageBox.Show("Data Read back: " + Encoding.ASCII.GetString(e.Data)); //display data
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Hi Friends,

Myself, Sumit Mehta, an IT consultant and a software engineer in India.

My Skillset is based on the 7+ years of working experience in different heterogeneous technologies.

I have successfully designed, developed, and supported live web applications in .NET.

Besides this, I have also managed and administered Plesk control panel, having almost 100 domains and 50+ clients.

I have developed projects for a wide scale and range, i.e. from small scale clients to banking applications. Currently I work in .NET 4.0 and Visual Studio 2010. I also have hands on experience in PHP, Java and VB6. I am also heading towards iPhone development now.

You can get in touch with me at http://www.sumitmehta.net

Comments and Discussions