?? form1.cs
字號:
this.label7.TabIndex = 13;
this.label7.Text = "解密密碼:";
//
// label6
//
this.label6.Location = new System.Drawing.Point(8, 136);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(104, 16);
this.label6.TabIndex = 12;
this.label6.Text = "解密輸出文件名:";
//
// label5
//
this.label5.Location = new System.Drawing.Point(8, 104);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(96, 16);
this.label5.TabIndex = 11;
this.label5.Text = "已加密文件名:";
//
// button4
//
this.button4.Location = new System.Drawing.Point(352, 168);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(80, 24);
this.button4.TabIndex = 10;
this.button4.Text = "解密文件";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(352, 96);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(80, 24);
this.button3.TabIndex = 9;
this.button3.Text = "選擇文件";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button8
//
this.button8.Location = new System.Drawing.Point(0, 0);
this.button8.Name = "button8";
this.button8.TabIndex = 0;
//
// openFileDialog1
//
this.openFileDialog1.FileOk += new System.ComponentModel.CancelEventHandler(this.openFileDialog1_FileOk);
//
// button5
//
this.button5.Location = new System.Drawing.Point(240, 496);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(80, 24);
this.button5.TabIndex = 2;
this.button5.Text = "刷新";
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button6
//
this.button6.Location = new System.Drawing.Point(376, 496);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(80, 24);
this.button6.TabIndex = 3;
this.button6.Text = "退出";
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(520, 541);
this.Controls.Add(this.button6);
this.Controls.Add(this.button5);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Name = "Form1";
this.Text = "演示程序";
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{//選擇要加密的文件
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
this.textBox1.Text = this.openFileDialog1.FileName;
}
private void button2_Click(object sender, System.EventArgs e)
{//加密文件
if (this.textBox1.Text == "")
{
MessageBox.Show("請選擇被加密文件!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
if (this.textBox2.Text == "")
{
MessageBox.Show("請輸入加密輸出文件名!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
if (this.textBox3.Text == ""|this.textBox3.Text.Length < 6|this.textBox3.Text.Length > 8|this.textBox3.Text != this.textBox4.Text)
{
MessageBox.Show("密碼不符合規定,請輸入正確的密碼!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
try
{
//加密讀入文件名
string myInfilename = this.textBox1.Text;
//加密輸出文件名
string myOutfilename = this.textBox2.Text;
//設置DES算法初始向量
byte [] myDESIV = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};
byte [] myDESKEY = {};
string mykeystring = this.textBox3.Text;
//根據密碼設置密鑰大小
if (mykeystring.Length == 6)
{
myDESKEY = new byte [] {(byte) mykeystring[0],(byte) mykeystring[1],(byte) mykeystring[2],(byte) mykeystring[3],
(byte) mykeystring[4],(byte) mykeystring[5],0x07,0x08};
}
if (mykeystring.Length == 7)
{
myDESKEY = new byte [] {(byte) mykeystring[0],(byte) mykeystring[1],(byte) mykeystring[2],(byte) mykeystring[3],
(byte) mykeystring[4],(byte) mykeystring[5],(byte)mykeystring[6],0x08};
}
if (mykeystring.Length >=8)
{
myDESKEY = new byte [] {(byte) mykeystring[0],(byte) mykeystring[1],(byte) mykeystring[2],(byte) mykeystring[3],
(byte) mykeystring[4],(byte) mykeystring[5],(byte)mykeystring[6],(byte)mykeystring[7]};
}
//輸入輸出的文件流
FileStream myInfilestream = new FileStream(myInfilename,System.IO.FileMode.Open,System.IO.FileAccess.Read);
FileStream myOutfilestream = new FileStream(myOutfilename,FileMode.OpenOrCreate,FileAccess.Write);
myOutfilestream.SetLength(0);
//加密文件的中間流
byte [] Inerdata = new byte[100];
//已加密文件流
int completesize = 0;
//總共要加密的文件流大小
long myfilelength = myInfilestream.Length;
//創建DES
DES mydes = new System.Security.Cryptography.DESCryptoServiceProvider();
//創建加密流
CryptoStream Encrytstream = new CryptoStream(myOutfilestream,mydes.CreateEncryptor(myDESKEY,myDESIV),CryptoStreamMode.Write);
while(completesize < myfilelength)
{//每次寫入加密文件的數據大小
int length = myInfilestream.Read(Inerdata,0,100);
Encrytstream.Write(Inerdata,0,length);
completesize = completesize + length;
}
Encrytstream.Close();
myOutfilestream.Close();
myInfilestream.Close();
MessageBox.Show("文件加密成功!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
this.textBox8.Text = this.textBox2.Text;
}
catch (Exception Err)
{
MessageBox.Show("文件加密操作有誤: "+Err.Message,"信息提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
private void button3_Click(object sender, System.EventArgs e)
{//選擇要解密的文件
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
this.textBox5.Text = this.openFileDialog1.FileName;
}
private void button4_Click(object sender, System.EventArgs e)
{//解密文件
if (this.textBox5.Text == "")
{
MessageBox.Show("請選擇要解密的0文件!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
if (this.textBox6.Text == "")
{
MessageBox.Show("請輸入解密輸出文件名!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
if (this.textBox7.Text.Length < 6|this.textBox7.Text.Length > 8)
{
MessageBox.Show("密碼不符合規定,請輸入正確的密碼!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
try
{
//解密讀入文件名
string myInfilename = this.textBox5.Text;
//解密輸出文件名
string myOutfilename = this.textBox6.Text;
//設置DES算法初始向量
byte [] myDESIV = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};
byte [] myDESKEY = {};
string mykeystring = this.textBox7.Text;
//根據密碼設置密鑰大小
if (mykeystring.Length == 6)
{
myDESKEY = new byte [] {(byte) mykeystring[0],(byte) mykeystring[1],(byte) mykeystring[2],(byte) mykeystring[3],
(byte) mykeystring[4],(byte) mykeystring[5],0x07,0x08};
}
if (mykeystring.Length == 7)
{
myDESKEY = new byte [] {(byte) mykeystring[0],(byte) mykeystring[1],(byte) mykeystring[2],(byte) mykeystring[3],
(byte) mykeystring[4],(byte) mykeystring[5],(byte)mykeystring[6],0x08};
}
if (mykeystring.Length >=8)
{
myDESKEY = new byte [] {(byte) mykeystring[0],(byte) mykeystring[1],(byte) mykeystring[2],(byte) mykeystring[3],
(byte) mykeystring[4],(byte) mykeystring[5],(byte)mykeystring[6],(byte)mykeystring[7]};
}
//輸入輸出的文件流
FileStream myInfilestream = new FileStream(myInfilename,System.IO.FileMode.Open,System.IO.FileAccess.Read);
FileStream myOutfilestream = new FileStream(myOutfilename,FileMode.OpenOrCreate,FileAccess.Write);
myOutfilestream.SetLength(0);
//解密文件的中間流
byte [] Inerdata = new byte[100];
//已解密文件流
int completesize = 0;
//總共要解密的文件流大小
long myfilelength = myInfilestream.Length;
//創建DES
DES mydes = new System.Security.Cryptography.DESCryptoServiceProvider();
//創建加密流
CryptoStream Decrytstream = new CryptoStream(myOutfilestream,mydes.CreateDecryptor(myDESKEY,myDESIV),CryptoStreamMode.Write);
while(completesize < myfilelength)
{//每次寫入加密文件的數據大小
int length = myInfilestream.Read(Inerdata,0,100);
Decrytstream.Write(Inerdata,0,length);
completesize = completesize + length;
}
Decrytstream.Close();
myOutfilestream.Close();
myInfilestream.Close();
MessageBox.Show("文件解密成功!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
catch (Exception Err)
{
MessageBox.Show("文件解密操作有誤: "+Err.Message,"信息提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
// flag = 1;
}
}
private void button5_Click(object sender, System.EventArgs e)
{//刷新對話框
this.textBox1.Text = "";
this.textBox2.Text = "";
this.textBox3.Text = "";
this.textBox4.Text = "";
this.textBox5.Text = "";
this.textBox6.Text = "";
this.textBox7.Text = "";
this.textBox8.Text = "";
this.textBox9.Text = "//192.168.0.2/movie/";
this.textBox10.Text = "";
this.textBox11.Text = "//192.168.0.2/";
}
private void button6_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void button9_Click(object sender, System.EventArgs e)
{//下載文件
this.textBox5.Text = this.textBox10.Text;
if(this.textBox10.Text==""|this.textBox11.Text=="")
{
MessageBox.Show("請輸入你要下載的文件名字!","錯誤:", MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
else
{
WebClient MyClient=new WebClient();
string URL=this.textBox11.Text;
string SaveFileName=this.textBox10.Text;
try
{
MyClient.DownloadFile(URL , SaveFileName);
MessageBox.Show("文件下載成功!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
this.textBox5.Text = this.textBox10.Text;
}
catch(Exception Err)
{
MessageBox.Show("文件下載失敗!錯誤是:"+Err.Message,"信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
}
}
private void button7_Click(object sender, System.EventArgs e)
{//上傳文件
if(this.textBox8.Text==""|this.textBox9.Text=="")
{
MessageBox.Show("請輸入你要上載的文件名字!","錯誤:", MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
else
{
/// 得到文件名,文件擴展名字,服務器路徑
string fileNamePath = textBox8.Text.Trim();
string uriString = textBox9.Text.Trim();
string fileName = fileNamePath.Substring(fileNamePath.LastIndexOf("\\") + 1);
string fileNameExt = fileName.Substring(fileName.LastIndexOf(".") + 1);
if(uriString.EndsWith("/") == false) uriString = uriString + "/";
uriString = uriString + fileName;
WebClient MyClient=new WebClient();
string URL=uriString;
string FileName=fileName;
MyClient.Credentials = CredentialCache.DefaultCredentials;
// 要上傳的文件
FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
try
{
byte[] postArray = r.ReadBytes((int)fs.Length);
Stream postStream = MyClient.OpenWrite(URL,"PUT");
if(postStream.CanWrite)
{
postStream.Write(postArray,0,postArray.Length);
MessageBox.Show("文件上載成功!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
//return;
}
else
{
label1.Text = "文件目前不可寫!";
}
postStream.Close();
}
catch(Exception Err)
{
MessageBox.Show("文件上載失敗!錯誤是:"+Err.Message,"信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
// finally
// {
// MessageBox.Show("\n服務器的回復為: \n"+System.Text.Encoding.ASCII.GetString(MyResponseArray));
//return;
// }
}
}
private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -