您的位置:首页 > 其它

一个加密与解密的实例

2006-10-10 13:24 260 查看

using System;


using System.Drawing;


using System.Collections;


using System.ComponentModel;


using System.Windows.Forms;


using System.Data;


using System.IO;


using System.Security.Cryptography;




namespace 文件加密




...{




/**//// <summary>


/// Form1 的摘要说明。


/// </summary>


public class Form1 : System.Windows.Forms.Form




...{


private System.Windows.Forms.GroupBox groupBox1;


private System.Windows.Forms.Label label1;


private System.Windows.Forms.Label label2;


private System.Windows.Forms.Label label3;


private System.Windows.Forms.Label label4;


private System.Windows.Forms.Label label5;


private System.Windows.Forms.TextBox textBox1;


private System.Windows.Forms.TextBox textBox2;


private System.Windows.Forms.TextBox textBox3;


private System.Windows.Forms.TextBox textBox4;


private System.Windows.Forms.Button button1;


private System.Windows.Forms.Button button2;


private System.Windows.Forms.Button button3;


private System.Windows.Forms.Button button4;


private System.Windows.Forms.GroupBox groupBox2;


private System.Windows.Forms.Label label6;


private System.Windows.Forms.Button button5;


private System.Windows.Forms.Label label7;


private System.Windows.Forms.TextBox textBox5;


private System.Windows.Forms.TextBox textBox6;


private System.Windows.Forms.TextBox textBox7;


private System.Windows.Forms.Button button6;


private System.Windows.Forms.Label label8;


private System.Windows.Forms.Button button7;


private System.Windows.Forms.SaveFileDialog saveFileDialog1;


private System.Windows.Forms.OpenFileDialog openFileDialog1;




/**//// <summary>


/// 必需的设计器变量。


/// </summary>


private System.ComponentModel.Container components = null;




public Form1()




...{


//


// Windows 窗体设计器支持所必需的


//


InitializeComponent();




//


// TODO: 在 InitializeComponent 调用后添加任何构造函数代码


//


}






/**//// <summary>


/// 清理所有正在使用的资源。


/// </summary>


protected override void Dispose( bool disposing )




...{


if( disposing )




...{


if (components != null)




...{


components.Dispose();


}


}


base.Dispose( disposing );


}






Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码




/**//// <summary>


/// 设计器支持所需的方法 - 不要使用代码编辑器修改


/// 此方法的内容。


/// </summary>


private void InitializeComponent()




...{


this.groupBox1 = new System.Windows.Forms.GroupBox();


this.button1 = new System.Windows.Forms.Button();


this.textBox1 = new System.Windows.Forms.TextBox();


this.label1 = new System.Windows.Forms.Label();


this.label2 = new System.Windows.Forms.Label();


this.label3 = new System.Windows.Forms.Label();


this.label4 = new System.Windows.Forms.Label();


this.label5 = new System.Windows.Forms.Label();


this.textBox2 = new System.Windows.Forms.TextBox();


this.textBox3 = new System.Windows.Forms.TextBox();


this.textBox4 = new System.Windows.Forms.TextBox();


this.button2 = new System.Windows.Forms.Button();


this.button3 = new System.Windows.Forms.Button();


this.button4 = new System.Windows.Forms.Button();


this.groupBox2 = new System.Windows.Forms.GroupBox();


this.label6 = new System.Windows.Forms.Label();


this.button5 = new System.Windows.Forms.Button();


this.label7 = new System.Windows.Forms.Label();


this.textBox5 = new System.Windows.Forms.TextBox();


this.textBox6 = new System.Windows.Forms.TextBox();


this.textBox7 = new System.Windows.Forms.TextBox();


this.button6 = new System.Windows.Forms.Button();


this.label8 = new System.Windows.Forms.Label();


this.button7 = new System.Windows.Forms.Button();


this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();


this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();


this.groupBox1.SuspendLayout();


this.groupBox2.SuspendLayout();


this.SuspendLayout();


//


// groupBox1


//


this.groupBox1.Controls.Add(this.button1);


this.groupBox1.Controls.Add(this.textBox1);


this.groupBox1.Controls.Add(this.label1);


this.groupBox1.Controls.Add(this.label2);


this.groupBox1.Controls.Add(this.label3);


this.groupBox1.Controls.Add(this.label4);


this.groupBox1.Controls.Add(this.label5);


this.groupBox1.Controls.Add(this.textBox2);


this.groupBox1.Controls.Add(this.textBox3);


this.groupBox1.Controls.Add(this.textBox4);


this.groupBox1.Controls.Add(this.button2);


this.groupBox1.Controls.Add(this.button3);


this.groupBox1.Controls.Add(this.button4);


this.groupBox1.Location = new System.Drawing.Point(8, 8);


this.groupBox1.Name = "groupBox1";


this.groupBox1.Size = new System.Drawing.Size(360, 224);


this.groupBox1.TabIndex = 0;


this.groupBox1.TabStop = false;


this.groupBox1.Text = "文件加密";


//


// button1


//


this.button1.Location = new System.Drawing.Point(272, 24);


this.button1.Name = "button1";


this.button1.TabIndex = 2;


this.button1.Text = "浏览...";


this.button1.Click += new System.EventHandler(this.button1_Click);


//


// textBox1


//


this.textBox1.Location = new System.Drawing.Point(104, 24);


this.textBox1.Name = "textBox1";


this.textBox1.Size = new System.Drawing.Size(160, 21);


this.textBox1.TabIndex = 1;


this.textBox1.Text = "";


//


// label1


//


this.label1.Location = new System.Drawing.Point(16, 24);


this.label1.Name = "label1";


this.label1.Size = new System.Drawing.Size(80, 24);


this.label1.TabIndex = 0;


this.label1.Text = "选择加密文件";


this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;


//


// label2


//


this.label2.Location = new System.Drawing.Point(16, 64);


this.label2.Name = "label2";


this.label2.Size = new System.Drawing.Size(80, 24);


this.label2.TabIndex = 0;


this.label2.Text = "输出文件名";


this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;


//


// label3


//


this.label3.ForeColor = System.Drawing.Color.Red;


this.label3.Location = new System.Drawing.Point(16, 184);


this.label3.Name = "label3";


this.label3.Size = new System.Drawing.Size(128, 24);


this.label3.TabIndex = 0;


this.label3.Text = "请注意:密码至少6位";


this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;


//


// label4


//


this.label4.Location = new System.Drawing.Point(16, 104);


this.label4.Name = "label4";


this.label4.Size = new System.Drawing.Size(80, 24);


this.label4.TabIndex = 0;


this.label4.Text = "设定密码";


this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;


//


// label5


//


this.label5.Location = new System.Drawing.Point(16, 144);


this.label5.Name = "label5";


this.label5.Size = new System.Drawing.Size(80, 24);


this.label5.TabIndex = 0;


this.label5.Text = "确认密码";


this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;


//


// textBox2


//


this.textBox2.Location = new System.Drawing.Point(104, 64);


this.textBox2.Name = "textBox2";


this.textBox2.Size = new System.Drawing.Size(160, 21);


this.textBox2.TabIndex = 1;


this.textBox2.Text = "";


//


// textBox3


//


this.textBox3.Location = new System.Drawing.Point(104, 104);


this.textBox3.Name = "textBox3";


this.textBox3.PasswordChar = '*';


this.textBox3.Size = new System.Drawing.Size(160, 21);


this.textBox3.TabIndex = 1;


this.textBox3.Text = "";


//


// textBox4


//


this.textBox4.Location = new System.Drawing.Point(104, 144);


this.textBox4.Name = "textBox4";


this.textBox4.PasswordChar = '*';


this.textBox4.Size = new System.Drawing.Size(160, 21);


this.textBox4.TabIndex = 1;


this.textBox4.Text = "";


//


// button2


//


this.button2.Location = new System.Drawing.Point(272, 64);


this.button2.Name = "button2";


this.button2.TabIndex = 2;


this.button2.Text = "浏览...";


this.button2.Click += new System.EventHandler(this.button2_Click);


//


// button3


//


this.button3.Location = new System.Drawing.Point(272, 144);


this.button3.Name = "button3";


this.button3.TabIndex = 2;


this.button3.Text = "确定";


this.button3.Click += new System.EventHandler(this.button3_Click);


//


// button4


//


this.button4.Enabled = false;


this.button4.Location = new System.Drawing.Point(272, 184);


this.button4.Name = "button4";


this.button4.TabIndex = 2;


this.button4.Text = "开始加密";


this.button4.Click += new System.EventHandler(this.button4_Click);


//


// groupBox2


//


this.groupBox2.Controls.Add(this.label6);


this.groupBox2.Controls.Add(this.button5);


this.groupBox2.Controls.Add(this.label7);


this.groupBox2.Controls.Add(this.textBox5);


this.groupBox2.Controls.Add(this.textBox6);


this.groupBox2.Controls.Add(this.textBox7);


this.groupBox2.Controls.Add(this.button6);


this.groupBox2.Controls.Add(this.label8);


this.groupBox2.Controls.Add(this.button7);


this.groupBox2.Location = new System.Drawing.Point(8, 240);


this.groupBox2.Name = "groupBox2";


this.groupBox2.Size = new System.Drawing.Size(360, 144);


this.groupBox2.TabIndex = 1;


this.groupBox2.TabStop = false;


this.groupBox2.Text = "文件解密";


//


// label6


//


this.label6.Location = new System.Drawing.Point(16, 64);


this.label6.Name = "label6";


this.label6.Size = new System.Drawing.Size(80, 24);


this.label6.TabIndex = 0;


this.label6.Text = "输出文件名";


this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;


//


// button5


//


this.button5.Location = new System.Drawing.Point(272, 24);


this.button5.Name = "button5";


this.button5.TabIndex = 2;


this.button5.Text = "浏览...";


this.button5.Click += new System.EventHandler(this.button5_Click);


//


// label7


//


this.label7.Location = new System.Drawing.Point(16, 104);


this.label7.Name = "label7";


this.label7.Size = new System.Drawing.Size(80, 24);


this.label7.TabIndex = 0;


this.label7.Text = "输入解密密码";


this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;


//


// textBox5


//


this.textBox5.Location = new System.Drawing.Point(104, 24);


this.textBox5.Name = "textBox5";


this.textBox5.Size = new System.Drawing.Size(160, 21);


this.textBox5.TabIndex = 1;


this.textBox5.Text = "";


//


// textBox6


//


this.textBox6.Location = new System.Drawing.Point(104, 64);


this.textBox6.Name = "textBox6";


this.textBox6.Size = new System.Drawing.Size(160, 21);


this.textBox6.TabIndex = 1;


this.textBox6.Text = "";


//


// textBox7


//


this.textBox7.Location = new System.Drawing.Point(104, 104);


this.textBox7.Name = "textBox7";


this.textBox7.PasswordChar = '*';


this.textBox7.Size = new System.Drawing.Size(160, 21);


this.textBox7.TabIndex = 1;


this.textBox7.Text = "";


//


// button6


//


this.button6.Location = new System.Drawing.Point(272, 64);


this.button6.Name = "button6";


this.button6.TabIndex = 2;


this.button6.Text = "浏览...";


this.button6.Click += new System.EventHandler(this.button6_Click);


//


// label8


//


this.label8.Location = new System.Drawing.Point(16, 24);


this.label8.Name = "label8";


this.label8.Size = new System.Drawing.Size(80, 24);


this.label8.TabIndex = 0;


this.label8.Text = "选择解密文件";


this.label8.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;


//


// button7


//


this.button7.Location = new System.Drawing.Point(272, 104);


this.button7.Name = "button7";


this.button7.TabIndex = 2;


this.button7.Text = "开始解密";


this.button7.Click += new System.EventHandler(this.button7_Click);


//


// Form1


//


this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);


this.ClientSize = new System.Drawing.Size(376, 390);


this.Controls.Add(this.groupBox1);


this.Controls.Add(this.groupBox2);


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)




...{


OpenFileDialog ffile=new OpenFileDialog();


ffile.Title = "选择需要加密的文件" ;


ffile.Filter="所有文件 (*.*)|*.*";


if(ffile.ShowDialog()==DialogResult.OK)




...{


//将待加密文件的路径写入文本框1


textBox1.Text=ffile.FileName;


}


}




private void button2_Click(object sender, System.EventArgs e)




...{


SaveFileDialog jfile=new SaveFileDialog();


jfile.Filter="加密文件(*.mef)|*.mef|所有文件 (*.*)|*.*";


if(jfile.ShowDialog()==DialogResult.OK)




...{


//将输出文件路径写入文本框2


textBox2.Text=jfile.FileName;


}


}




private void button3_Click(object sender, System.EventArgs e)




...{


if(textBox3.Text==textBox4.Text)//判断密码是否符合要求




...{


if(textBox3.Text.Length<5)




...{


MessageBox.Show("输入的密码少于6位,请重新输入");


textBox3.Text="";


textBox4.Text="";


return;


}


}


else




...{


MessageBox.Show("两次输入的密码不匹配,请重新输入");


textBox3.Text="";


textBox4.Text="";


return;


}




if(textBox1.Text==""||textBox2.Text=="")//判断是否输入了文件名




...{


MessageBox.Show("请选择好正确的输入输出文件");


}


else




...{


button4.Enabled=true;


}


}




private void button4_Click(object sender, System.EventArgs e)




...{


FileStream fin;


FileStream fout;


try




...{


fin=new FileStream(textBox1.Text,FileMode.Open,FileAccess.Read);


fout=new FileStream(textBox2.Text,FileMode.OpenOrCreate,FileAccess.Write);


}


catch




...{


MessageBox.Show("输入的文件路径不可访问");


return;


}


//每次加密的中间流


byte[] bin=new byte[128];


//设定初始向量值




byte[] desIV=...{0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18};


byte[] desKey=new byte[8];




//根据密码得到密钥


string keystring=this.textBox3.Text;


if(keystring.Length>=8)




...{


desKey[0]=(byte)keystring[0];desKey[1]=(byte)keystring[1];


desKey[2]=(byte)keystring[2];desKey[3]=(byte)keystring[3];


desKey[4]=(byte)keystring[4];desKey[5]=(byte)keystring[5];


desKey[6]=(byte)keystring[6];desKey[7]=(byte)keystring[7];


}


if(keystring.Length==7)




...{


desKey[0]=(byte)keystring[0];desKey[1]=(byte)keystring[1];


desKey[2]=(byte)keystring[2];desKey[3]=(byte)keystring[3];


desKey[4]=(byte)keystring[4];desKey[5]=(byte)keystring[5];


desKey[6]=(byte)keystring[6];desKey[7]=0x18;


}


if(keystring.Length==6)




...{


desKey[0]=(byte)keystring[0];desKey[1]=(byte)keystring[1];


desKey[2]=(byte)keystring[2];desKey[3]=(byte)keystring[3];


desKey[4]=(byte)keystring[4];desKey[5]=(byte)keystring[5];


desKey[6]=0x17;desKey[7]=0x18;


}


//创建DES对象


DES des=new DESCryptoServiceProvider();


//创建加密流


CryptoStream encStream=new CryptoStream(fout,des.CreateEncryptor(desKey,desIV),CryptoStreamMode.Write);




int complete=0;//代表已经加密的流的大小


long totlen=fin.Length;//代表加密文件总的大小


int len;//每次写入的大小




//从输入文件中读取流,然后加密到输出文件中


while(complete<totlen)




...{


len=fin.Read(bin,0,128);


encStream.Write(bin,0,len);


complete+=len;


}


//关闭流


encStream.Close();


fout.Close();


fin.Close();


MessageBox.Show("文件加密已经完成");


button4.Enabled=false;




}




private void button5_Click(object sender, System.EventArgs e)




...{


OpenFileDialog ofDialog=new OpenFileDialog();


ofDialog.Title = "选择需要解密的文件" ;


ofDialog.Filter="加密文件(*.mef)|*.mef|所有文件 (*.*)|*.*";


if(ofDialog.ShowDialog()==DialogResult.OK)




...{


//将待加密文件的路径写入文本框1


textBox5.Text=ofDialog.FileName;


}


}




private void button6_Click(object sender, System.EventArgs e)




...{


SaveFileDialog sfDialog=new SaveFileDialog();


sfDialog.Title="请选择解密后的文件保存路径";


sfDialog.Filter="所有文件(*.*)|*.*";


if(sfDialog.ShowDialog()==DialogResult.OK)




...{


textBox6.Text=sfDialog.FileName;


}


}




private void button7_Click(object sender, System.EventArgs e)




...{


//创建输入输出文件流


FileStream fin,fout;


try




...{


fin=new FileStream(textBox5.Text,FileMode.Open,FileAccess.Read);


fout=new FileStream(textBox6.Text,FileMode.OpenOrCreate,FileAccess.Write);


}


catch(Exception error)




...{


//MessageBox.Show("打开文件出错,请重新选择输入或输出文件");


MessageBox.Show(error.Message, "解密出错", MessageBoxButtons.OK,MessageBoxIcon.Warning);


return;


}






//每次解密的中间流


byte[] bin=new byte[128];


//设定初始向量值




byte[] desIV=...{0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18};


byte[] desKey=new byte[8];


//根据密码得到密钥


string keystring=this.textBox7.Text;


if(keystring.Length>=8)




...{


desKey[0]=(byte)keystring[0];desKey[1]=(byte)keystring[1];


desKey[2]=(byte)keystring[2];desKey[3]=(byte)keystring[3];


desKey[4]=(byte)keystring[4];desKey[5]=(byte)keystring[5];


desKey[6]=(byte)keystring[6];desKey[7]=(byte)keystring[7];


}


if(keystring.Length==7)




...{


desKey[0]=(byte)keystring[0];desKey[1]=(byte)keystring[1];


desKey[2]=(byte)keystring[2];desKey[3]=(byte)keystring[3];


desKey[4]=(byte)keystring[4];desKey[5]=(byte)keystring[5];


desKey[6]=(byte)keystring[6];desKey[7]=0x18;


}


if(keystring.Length==6)




...{


desKey[0]=(byte)keystring[0];desKey[1]=(byte)keystring[1];


desKey[2]=(byte)keystring[2];desKey[3]=(byte)keystring[3];


desKey[4]=(byte)keystring[4];desKey[5]=(byte)keystring[5];


desKey[6]=0x17;desKey[7]=0x18;


}




int complete=0;//代表已经解密的流的大小


long totlen=fin.Length;//代表解密文件总的大小


int len;//每次写入的大小


//创建DES对象


DES des=new DESCryptoServiceProvider();


CryptoStream decStream;//创建解密流




try




...{


decStream=new CryptoStream(fout,des.CreateDecryptor(desKey,desIV),CryptoStreamMode.Write);


//从输入文件中读取流,然后解密到输出文件中


while(complete<totlen)




...{


len=fin.Read(bin,0,128);


decStream.Write(bin,0,len);


complete+=len;


}


//关闭流


decStream.Close();


fin.Close();


fout.Close();


MessageBox.Show("文件解密成功");


}


catch




...{


MessageBox.Show("密码输入错误");


}


}


}


}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: