|
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.