大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> NET专区 -> C#-对称加密的一个例子

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

时间: 2021-07-31 作者:daque

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()); } } }}

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.