首页 > 开发 > 综合 > 正文

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

2024-07-21 02:27:13
字体:
来源:转载
供稿:网友

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 = null;

  public form1()
  {
   //
   // windows 窗体设计器支持所必需的
   //
   initializecomponent();

   //
   // todo: 在 initializecomponent 调用后添加任何构造函数代码
   //
  }

  ///
  /// 清理所有正在使用的资源。
  ///
  protected override void dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.dispose();
    }
   }
   base.dispose( disposing );
  }

  #region windows 窗体设计器生成的代码
  ///
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  ///
  private void initializecomponent()
  {
   this.textbox1 = new system.windows.forms.textbox();
   this.textbox2 = new system.windows.forms.textbox();
   this.button1 = new system.windows.forms.button();
   this.button2 = new system.windows.forms.button();
   this.textbox3 = new system.windows.forms.textbox();
   this.openfile = new system.windows.forms.openfiledialog();
   this.button3 = new system.windows.forms.button();
   this.savefile = new system.windows.forms.savefiledialog();
   this.button4 = new system.windows.forms.button();
   this.label1 = new system.windows.forms.label();
   this.label2 = new system.windows.forms.label();
   this.checkbox1 = new system.windows.forms.checkbox();
   this.suspendlayout();
   //
   // textbox1
   //
   this.textbox1.location = new system.drawing.point(88, 96);
   this.textbox1.name = "textbox1";
   this.textbox1.size = new system.drawing.size(168, 21);
   this.textbox1.tabindex = 0;
   this.textbox1.text = "";
   //
   // textbox2
   //
   this.textbox2.location = new system.drawing.point(88, 136);
   this.textbox2.name = "textbox2";
   this.textbox2.size = new system.drawing.size(168, 21);
   this.textbox2.tabindex = 1;
   this.textbox2.text = "";
   //
   // button1
   //
   this.button1.location = new system.drawing.point(88, 200);
   this.button1.name = "button1";
   this.button1.tabindex = 2;
   this.button1.text = "加密";
   this.button1.click += new system.eventhandler(this.button1_click);
   //
   // button2
   //
   this.button2.location = new system.drawing.point(192, 200);
   this.button2.name = "button2";
   this.button2.tabindex = 3;
   this.button2.text = "解密";
   this.button2.click += new system.eventhandler(this.button2_click);
   //
   // textbox3
   //
   this.textbox3.hideselection = false;
   this.textbox3.location = new system.drawing.point(88, 48);
   this.textbox3.maxlength = 17;
   this.textbox3.name = "textbox3";
   this.textbox3.passwordchar = '$';
   this.textbox3.tabindex = 4;
   this.textbox3.text = "";
   this.textbox3.textalign = system.windows.forms.horizontalalignment.center;
   //
   // button3
   //
   this.button3.location = new system.drawing.point(280, 96);
   this.button3.name = "button3";
   this.button3.size = new system.drawing.size(40, 23);
   this.button3.tabindex = 5;
   this.button3.text = "open";
   this.button3.click += new system.eventhandler(this.button3_click);
   //
   // button4
   //
   this.button4.location = new system.drawing.point(280, 136);
   this.button4.name = "button4";
   this.button4.size = new system.drawing.size(40, 23);
   this.button4.tabindex = 6;
   this.button4.text = "save";
   this.button4.click += new system.eventhandler(this.button4_click);
   //
   // label1
   //
   this.label1.location = new system.drawing.point(88, 24);
   this.label1.name = "label1";
   this.label1.size = new system.drawing.size(136, 23);
   this.label1.tabindex = 7;
   this.label1.text = "请输入你的密码:";
   //
   // label2
   //
   this.label2.location = new system.drawing.point(216, 48);
   this.label2.name = "label2";
   this.label2.size = new system.drawing.size(184, 23);
   this.label2.tabindex = 8;
   this.label2.text = "(密码大小8至16个字符)";
   //
   // checkbox1
   //
   this.checkbox1.location = new system.drawing.point(88, 168);
   this.checkbox1.name = "checkbox1";
   this.checkbox1.size = new system.drawing.size(216, 24);
   this.checkbox1.tabindex = 9;
   this.checkbox1.text = "删除源文件";
   //
   // form1
   //
   this.autoscalebasesize = new system.drawing.size(6, 14);
   this.clientsize = new system.drawing.size(456, 278);
   this.controls.add(this.checkbox1);
   this.controls.add(this.label2);
   this.controls.add(this.label1);
   this.controls.add(this.button4);
   this.controls.add(this.button3);
   this.controls.add(this.textbox3);
   this.controls.add(this.button2);
   this.controls.add(this.button1);
   this.controls.add(this.textbox2);
   this.controls.add(this.textbox1);
   this.name = "form1";
   this.text = "form1";
   this.resumelayout(false);

  }
  #endregion

  ///
  /// 应用程序的主入口点。
  ///
  [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());
   }
  }


 }
}


 



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表