在對文件流的使用中我們主要用到了filestream 類; 在參數中指名其文件路徑後我們還需要定義它的工作方式;(filemode.open)讀文件流;(filemode.create)寫文件流;要實現加密我們首先要拿到文件的數據內容(在內存中),因為我們在之前對文件定義了讀的形式,那麼我們就可以在內存中開辟一組byte類型的數組以存放文件流。 接下來的事情就都好說了,既然我能把流數據放入內存中,我們就可以改寫內存中的數據值(byte值)來對文件數據進行逐個改寫;這就是簡單的加密思想。
實現過程:我們先添加一個類(以oop思想編程)“manager.cs”
public bool addkeyWord(string filename)
{
FileStream sread = new FileStream(filename,FileMode.Open );
int length = (int)sread.Length;
byte[ ] buffer = new byte[length];
sread.Read(buffer, 0, length);
sread.Close(); /// 注意一定不要忘了關流,否則流一直被占用,其它操作就做不了了;
byte [ ]buffer2=new byte [length ]; ///再定義一個與其等長度的byte[ ]
int m = length;
for (int i = 0; i < length; i++) ///通過循環對每個流數據進行了一次改寫;
{
buffer2[i] =buffer[m-1]; ///通過順序的調換實現了文件的加密;至於算法方面大家可以自己想;
m--;
}
FileStream swt = new FileStream(filename,FileMode.Create );
swt.Write(buffer2, 0, length); ///最後將調換後的文件流重新寫回;
swt.Close();
return true;
}
至於解密過程也就是它的反過程啦!在這裡本人只給出了核心過程;有什麼該完善的地方望大家提出寶貴的意見,也可以發email:[email protected] 與我聯系;這是我的第一份博客,以後望大家多多支持;謝謝了!!!
public partial class Form2 : Form
...{
public Form2()
...{
InitializeComponent();
}
filemanager.manager fm = new filemanager.manager();
/**//// <summary>
/// 執行加密:
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
...{
string filename = this.textBox1.Text;
bool var = fm.addkeyWord(filename);
if (var)
...{
MessageBox.Show("加密完成!");
}
}
private void button3_Click(object sender, EventArgs e)
...{
&n; OpenFileDialog ofp = new OpenFileDialog();
ofp.ShowDialog();
this.textBox1.Text = ofp.FileName;
}
/**//// <summary>
/// 執行解密;
/// </summary>
public void resolve()
...{
ArrayList alist = new ArrayList();
alist.Add("正");
alist.Add("在");
alist.Add("校");
alist.Add("驗");
alist.Add("您");
alist.Add("的");
alist.Add("口");
alist.Add("令");
alist.Add(".");
alist.Add("..");
alist.Add("..");
this.label1.Text = "";
for (int i = 0; i < alist.Count; i++)
...{
this.label1.Text += alist[i].ToString();
Thread.Sleep(200);
}
if (this.textBox2.Text == "hhrong2312")
...{
fm.solverescue(this.textBox1.Text);
MessageBox.Show("解密成功");
}
else
...{
MessageBox.Show("口令不正確");
}
}
public void showtimes()
...{
while (true)
...{
this.label2.Text = DateTime.Now.ToString();
Thread.Sleep(1000);
}
}
private void button2_Click(object sender, EventArgs e)
...{
Thread th = new Thread(new ThreadStart(resolve));
th.Start();
//th.Abort();
}
private void Form2_Load(object sender, EventArgs e)
...{
Thread th = new Thread(new ThreadStart(showtimes));
th.Start();
}
}
Manager.cs 類 :由form頁負責調用:
/**//// <summary>
/// 加密過程:
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public bool addkeyWord(string filename)
...{
FileStream sread = new FileStream(filename,FileMode.Open );
int length = (int)sread.Length;
byte[] buffer = new byte[length];
sread.Read(buffer, 0, length);
sread.Close();
byte []buffer2=new byte [length ];
int m = length;
//int m = 5;
for (int i = 0; i < length; i++)
...{
buffer2[i] =buffer[m-1];
m--;
}
FileStream swt = new FileStream(filename,FileMode.Create );
swt.Write(buffer2, 0, length);
swt.Close();
return true;
}
/**//// <summary>
/// 解密過程
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public bool solverescue(string filename)
...{
FileStream sread = new FileStream(filename, FileMode.Open);
int length=(int)sread .Length ;
byte[] buffer = new byte[length];
byte[] buffer2 = new byte[length];
sread.Read(buffer, 0, length);
sread.Close();
int m = length;
for (int i = 0; i < length; i++)
...{
buffer2[i] = buffer[m - 1];
m--;
}
FileStream swt = new FileStream(filename, FileMode.Create);
swt.Write(buffer2, 0, length);
swt.Close();
return true;
}