C#-对称加密的一个例子

    技术2022-05-11  65

       C# - 对称加密的一个例子   using  System;  using  System.Drawing;  using  System.Collections;  using  System.ComponentModel;  using  System.Windows.Forms;  using  System.Data;  using  System.Security.Cryptography;  using  System.IO;  namespace  对称加密应用  /// /// Form1 的摘要说明。 /// public class Form1 : System.Windows.Forms.Form private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; /// /// 必需的设计器变量。 private System.Windows.Forms.TextBox textBox3; private System.Windows.Forms.OpenFileDialog openfile; private System.Windows.Forms.Button button3; private System.Windows.Forms.SaveFileDialog savefile; private System.Windows.Forms.Button button4; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.CheckBox checkBox1; //创建实例 RijndaelManaged rij = new RijndaelManaged();// //全局变量,标记文件的扩展名 private static string ext=null//标记加密成功与否 private static bool enresult = false//标记解密成功与否的标记 private static bool deresult = false/// private System.ComponentModel.Container components = nullpublic Form1() // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// /// 清理所有正在使用的资源。 /// protected override void Dispose( bool disposing ) if( disposing ) if (components != null{ components.Dispose(); } } base.Dispose( disposing ); } Windows 窗体设计器生成的代码 /// /// 应用程序的主入口点。 /// [STAThread] static void Main() { Application.Run(new Form1()); } //加密文件的按钮 private void button1_Click(object sender, System.EventArgs e) if(textBox1.Text != null || textBox2.Text != null) encryption(textBox3.Text,textBox1.Text,textBox2.Text); if(checkBox1.Checked==true && enresult==true) DeleteFile(textBox1.Text); } //用于加密的函数 public void encryption(string textBox,string readfile,string writefile) try if(textBox.Length >=8 && textBox.Length<=16 )//判断密码的字符的大小 byte [] key = System.Text.Encoding.Default.GetBytes( textBox ); byte [] iv = rij.IV; Rijndael crypt = Rijndael.Create(); ICryptoTransform transform = crypt.CreateEncryptor(key ,iv); //写进文件 FileStream fswrite = new FileStream(writefile,FileMode.Create); CryptoStream cs = new CryptoStream( fswrite ,transform ,CryptoStreamMode.Write ); //打开文件 FileStream fsread = new FileStream(readfile,FileMode.Open); int length; while((length = fsread.ReadByte() )!= -1) cs.WriteByte((byte)length); fsread.Close(); cs.Close(); fswrite.Close(); enresult = true ;//成功加密 MessageBox.Show("已经成功完成加密任务!"); } else {       MessageBox.Show("密码的最小长度为8个字符,最大长度为16个字符!"); return ; } } catch (Exception e) { MessageBox.Show(e.ToString()); } } //用于解密的函数 public void decryption(string textBox,string readfile,string writefile) try if(textBox.Length >=8 && textBox.Length<=16 ) byte [] key = System.Text.Encoding.Default.GetBytes( textBox ); byte [] iv = rij.IV; Rijndael crypt = Rijndael.Create(); ICryptoTransform transform = crypt.CreateDecryptor(key,iv); //读取加密后的文件 FileStream fsopen = new FileStream(readfile,FileMode.Open); CryptoStream cs = new CryptoStream( fsopen ,transform ,CryptoStreamMode.Read ); //把解密后的结果写进文件 FileStream fswrite = new FileStream(writefile,FileMode.OpenOrCreate); int length; while ((length = cs.ReadByte()) != -1) fswrite.WriteByte((byte)length); fswrite.Close(); cs.Close(); fsopen.Close(); deresult=true//成功解密 MessageBox.Show("已经成功完成解密任务!"); } else {       MessageBox.Show("密码的最小长度为8个字符,最大长度为16个字符!"); return ; } } catch (Exception e) { MessageBox.Show(e.ToString()); } } //解密文件 private void button2_Click(object sender, System.EventArgs e) { decryption( textBox3.Text,textBox1.Text,textBox2.Text ) ; if(checkBox1.Checked==true && deresult==true) DeleteFile(textBox1.Text); } //用于打开文件的按钮 private void button3_Click(object sender, System.EventArgs e) { openfile = new OpenFileDialog();openfile.Filter ="All files (*.*)|*.*" ; openfile.ShowDialog(); textBox1.Text = openfile.FileName; ext = getfileext(openfile.FileName); } private void button4_Click(object sender, System.EventArgs e) { savefile = new SaveFileDialog(); savefile.Filter =ext + " files" +"(*."+ext+")|*."+ ext +"|All files (*.*)|*.*" ; savefile.ShowDialog(); textBox2.Text = savefile.FileName; } //得到文件的扩展名 private string getfileext(string filename) try char [] point = new char[] {''.''}string [] filename2 = filename.Split(point); return filename2[1]; } catch return null; } } //删除文件的函数 public void DeleteFile(string filename) try { File.Delete(filename); } catch (Exception e) { MessageBox.Show(e.ToString()); } } } }      

    最新回复(0)