欢迎光临
我们一直在努力

用DES加密数据库信息,增强Asp.net的安全性(2)–实现源码-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

 

using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.io;
using system.text;
using system.xml;
using system.security.cryptography;

namespace writeconfig
{
 /// <summary>
 /// form1 的摘要说明。
 /// </summary>
 public class frmmain : system.windows.forms.form
 {
  private system.windows.forms.label labserver;
  private system.windows.forms.label labdatabase;
  private system.windows.forms.label labuser;
  private system.windows.forms.label labpassword;
  private system.windows.forms.textbox txtserver;
  private system.windows.forms.textbox txtdatabase;
  private system.windows.forms.textbox txtuser;
  private system.windows.forms.textbox txtpassword;
  private system.windows.forms.button btnread;
  private system.windows.forms.label label1;
  private system.windows.forms.textbox txtfile;
  private system.windows.forms.button btnwrite;
  private system.windows.forms.button btnlook;
  private system.windows.forms.openfiledialog ofd;
  private system.windows.forms.label labkey;
  private system.windows.forms.textbox txtkey;
  private fileinfo fi;
  private bool find;
  private string password=””;
  protected string keypassword=””;
  private system.windows.forms.button btndeletenode;
  
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private system.componentmodel.container components = null;

  public frmmain()
  {
   //
   // windows 窗体设计器支持所必需的
   //
   initializecomponent();
   int[] tmp=new int[8]{23,234,195,165,201,240,143,198};
   foreach(int i in tmp)
   {
    keypassword+=((char)i).tostring();
   }
  }

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

  #region windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 – 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void initializecomponent()
  {
   system.resources.resourcemanager resources = new system.resources.resourcemanager(typeof(frmmain));
   this.labserver = new system.windows.forms.label();
   this.labdatabase = new system.windows.forms.label();
   this.labuser = new system.windows.forms.label();
   this.labpassword = new system.windows.forms.label();
   this.txtserver = new system.windows.forms.textbox();
   this.txtdatabase = new system.windows.forms.textbox();
   this.txtuser = new system.windows.forms.textbox();
   this.txtpassword = new system.windows.forms.textbox();
   this.btnread = new system.windows.forms.button();
   this.btnwrite = new system.windows.forms.button();
   this.txtfile = new system.windows.forms.textbox();
   this.label1 = new system.windows.forms.label();
   this.btnlook = new system.windows.forms.button();
   this.ofd = new system.windows.forms.openfiledialog();
   this.labkey = new system.windows.forms.label();
   this.txtkey = new system.windows.forms.textbox();
   this.btndeletenode = new system.windows.forms.button();
   this.suspendlayout();
   //
   // labserver
   //
   this.labserver.location = new system.drawing.point(21, 57);
   this.labserver.name = “labserver”;
   this.labserver.size = new system.drawing.size(87, 14);
   this.labserver.tabindex = 0;
   this.labserver.text = “服务器:”;
   //
   // labdatabase
   //
   this.labdatabase.location = new system.drawing.point(21, 93);
   this.labdatabase.name = “labdatabase”;
   this.labdatabase.size = new system.drawing.size(87, 14);
   this.labdatabase.tabindex = 1;
   this.labdatabase.text = “数据源:”;
   //
   // labuser
   //
   this.labuser.location = new system.drawing.point(21, 129);
   this.labuser.name = “labuser”;
   this.labuser.size = new system.drawing.size(87, 14);
   this.labuser.tabindex = 2;
   this.labuser.text = “用户名:”;
   //
   // labpassword
   //
   this.labpassword.location = new system.drawing.point(21, 165);
   this.labpassword.name = “labpassword”;
   this.labpassword.size = new system.drawing.size(87, 14);
   this.labpassword.tabindex = 3;
   this.labpassword.text = “密  码:”;
   //
   // txtserver
   //
   this.txtserver.location = new system.drawing.point(117, 50);
   this.txtserver.name = “txtserver”;
   this.txtserver.size = new system.drawing.size(187, 25);
   this.txtserver.tabindex = 4;
   this.txtserver.text = “”;
   //
   // txtdatabase
   //
   this.txtdatabase.location = new system.drawing.point(117, 86);
   this.txtdatabase.name = “txtdatabase”;
   this.txtdatabase.size = new system.drawing.size(187, 25);
   this.txtdatabase.tabindex = 4;
   this.txtdatabase.text = “”;
   //
   // txtuser
   //
   this.txtuser.location = new system.drawing.point(117, 122);
   this.txtuser.name = “txtuser”;
   this.txtuser.size = new system.drawing.size(187, 25);
   this.txtuser.tabindex = 4;
   this.txtuser.text = “”;
   //
   // txtpassword
   //
   this.txtpassword.location = new system.drawing.point(117, 158);
   this.txtpassword.name = “txtpassword”;
   this.txtpassword.passwordchar = *;
   this.txtpassword.size = new system.drawing.size(187, 25);
   this.txtpassword.tabindex = 4;
   this.txtpassword.text = “”;
   this.txtpassword.textchanged += new system.eventhandler(this.txtpassword_textchanged);
   //
   // btnread
   //
   this.btnread.location = new system.drawing.point(320, 154);
   this.btnread.name = “btnread”;
   this.btnread.size = new system.drawing.size(100, 29);
   this.btnread.tabindex = 5;
   this.btnread.text = “读配置”;
   this.btnread.click += new system.eventhandler(this.btnread_click);
   //
   // btnwrite
   //
   this.btnwrite.location = new system.drawing.point(320, 191);
   this.btnwrite.name = “btnwrite”;
   this.btnwrite.size = new system.drawing.size(100, 28);
   this.btnwrite.tabindex = 5;
   this.btnwrite.text = “写配置”;
   this.btnwrite.click += new system.eventhandler(this.btnwrite_click);
   //
   // txtfile
   //
   this.txtfile.location = new system.drawing.point(117, 14);
   this.txtfile.name = “txtfile”;
   this.txtfile.size = new system.drawing.size(187, 25);
   this.txtfile.tabindex = 6;
   this.txtfile.text = “请选择目录下web.config文件”;
   //
   // label1
   //
   this.label1.location = new system.drawing.point(21, 21);
   this.label1.name = “label1”;
   this.label1.size = new system.drawing.size(96, 14);
   this.label1.tabindex = 3;
   this.label1.text = “配置文件:”;
   //
   // btnlook
   //
   this.btnlook.location = new system.drawing.point(320, 13);
   this.btnlook.name = “btnlook”;
   this.btnlook.size = new system.drawing.size(100, 28);
   this.btnlook.tabindex = 7;
   this.btnlook.text = “浏览…”;
   this.btnlook.click += new system.eventhandler(this.btnlook_click);
   //
   // ofd
   //
   this.ofd.filter = “web配置文件|web.config”;
   //
   // labkey
   //
   this.labkey.location = new system.drawing.point(21, 201);
   this.labkey.name = “labkey”;
   this.labkey.size = new system.drawing.size(96, 14);
   this.labkey.tabindex = 3;
   this.labkey.text = “读写节值:”;
   //
   // txtkey
   //
   this.txtkey.location = new system.drawing.point(117, 194);
   this.txtkey.name = “txtkey”;
   this.txtkey.size = new system.drawing.size(187, 25);
   this.txtkey.tabindex = 4;
   this.txtkey.text = “connstr”;
   //
   // btndeletenode
   //
   this.btndeletenode.location = new system.drawing.point(320, 119);
   this.btndeletenode.name = “btndeletenode”;
   this.btndeletenode.size = new system.drawing.size(100, 28);
   this.btndeletenode.tabindex = 5;
   this.btndeletenode.text = “删除节”;
   this.btndeletenode.visible = false;
   this.btndeletenode.click += new system.eventhandler(this.btndeletenode_click);
   //
   // frmmain
   //
   this.autoscalebasesize = new system.drawing.size(8, 18);
   this.clientsize = new system.drawing.size(437, 239);
   this.controls.add(this.btnlook);
   this.controls.add(this.txtfile);
   this.controls.add(this.btnread);
   this.controls.add(this.txtserver);
   this.controls.add(this.labpassword);
   this.controls.add(this.labuser);
   this.controls.add(this.labdatabase);
   this.controls.add(this.labserver);
   this.controls.add(this.txtdatabase);
   this.controls.add(this.txtuser);
   this.controls.add(this.txtpassword);
   this.controls.add(this.btnwrite);
   this.controls.add(this.label1);
   this.controls.add(this.labkey);
   this.controls.add(this.txtkey);
   this.controls.add(this.btndeletenode);
   this.icon = ((system.drawing.icon)(resources.getobject(“$this.icon”)));
   this.maximizebox = false;
   this.minimizebox = false;
   this.name = “frmmain”;
   this.startposition = system.windows.forms.formstartposition.centerscreen;
   this.text = “数据库连接配置”;
   this.load += new system.eventhandler(this.form1_load);
   this.resumelayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [stathread]
  static void main()
  {
   application.run(new frmmain());
  }

  private void form1_load(object sender, system.eventargs e)
  {
  
  }

  private void btnlook_click(object sender, system.eventargs e)
  {
   ofd.showdialog();
   txtfile.text=ofd.filename;
  }

  private bool inittxtfile()
  {
   txtfile.text=txtfile.text.trim();
   if(txtfile.text==””)
   {
    txtfile.text=system.environment.currentdirectory+@”\web.config“;
   }
   fi=new fileinfo(txtfile.text);
   if(!fi.exists)
   {
    messagebox.show(“没有找到配置文件!”,”错误”,messageboxbuttons.ok,messageboxicon.error);
    return false;
   }
   else
   {
    return true;
   }

  }
  private void btnwrite_click(object sender, system.eventargs e)
  {
   if(!inittxtfile())
   {
    return;
   }
   if((txtserver.text.trim()==””)||(txtdatabase.text.trim()==””)||(txtuser.text.trim()==””))
   {
    messagebox.show(“写配置失败,配置信息不完整!”,”错误”,messageboxbuttons.ok,messageboxicon.error);
    return;
   }
   find=false;
   xmldocument xd=new xmldocument();
   xd.load(fi.fullname);
   xmlnodelist nodelist=null;
   try
   {
    nodelist=xd.selectsinglenode(“/configuration/appsettings”).childnodes; //获取appsettings节点的所有子节点
   }
   catch
   {
    messagebox.show(“写配置失败,未找到配置节!”,”错误”,messageboxbuttons.ok,messageboxicon.error);
    return;
   }
   //连接字符串
   string orgstr=”server=”+txtserver.text.trim()+”;uid=”+txtuser.text.trim()+”;database=”+txtdatabase.text.trim()+”;pwd=”+password+””;
   string str= encrypt(orgstr,keypassword);

   foreach(xmlnode node in nodelist)
   {
    xmlelement xe=(xmlelement)node; //将子节点类型转换为xmlelement类型
    if(xe.getattribute(“key”)==txtkey.text.trim())
    {
     xe.attributes[“value”].value = str;
     find=true;
     break;
    }
   }
   if(find)
   {
    xd.save(txtfile.text);
    messagebox.show(“写配置成功!”,”配置成功”,messageboxbuttons.ok,messageboxicon.information);
   }
   else
   {
    //未找到节点,建立节点
    xmlnode xn=xd.selectsinglenode(“/configuration/appsettings”);
    xmlelement elem = xd.createelement(“and”);
    elem.setattribute(“key”,txtkey.text.trim());
    elem.setattribute(“value”,str);
    try
    {
     xn.appendchild(elem);
     xd.save(fi.fullname);
     messagebox.show(“写配置成功!”,”配置成功”,messageboxbuttons.ok,messageboxicon.information);
    }
    catch
    {
     messagebox.show(“写配置失败,可能文件权限不够!”,”错误”,messageboxbuttons.ok,messageboxicon.error);
    }
   }
  }

  private void btnread_click(object sender, system.eventargs e)
  {
   if(!inittxtfile())
   {
    return;
   }
   find=false;
   xmldocument xd=new xmldocument();
   xd.load(fi.fullname);
   xmlnodelist nodelist=null;
   try
   {
    nodelist=xd.selectsinglenode(“/configuration/appsettings”).childnodes; //获取appsettings节点的所有子节点
   }
   catch
   {
    messagebox.show(“读配置失败,未找到配置节!”,”错误”,messageboxbuttons.ok,messageboxicon.error);
    return;
   }
   string readstr=””;
   foreach(xmlnode node in nodelist)
   {
    xmlelement xe=(xmlelement)node; //将子节点类型转换为xmlelement类型
    if(xe.getattribute(“key”)==txtkey.text.trim())
    {
     readstr=xe.attributes[“value”].value;
     find=true;
     break;
    }
   }
   if(!find)
   {
    messagebox.show(“读配置失败,未找到配置节!”,”错误”,messageboxbuttons.ok,messageboxicon.error);
    return;
   }
   #region 读配置并解析
   string connstr=decrypt(readstr,keypassword);
   string [] tmp2=connstr.split(new char[]{;,=});
   for(int i=0;i<tmp2.length;i+=2)
   {
    if((tmp2[i].tolower()==”server”)||(tmp2[i].tolower()==”data source”))
    {
     txtserver.text=tmp2[i+1];
    }
    else if((tmp2[i].tolower()==”uid”)||(tmp2[i].tolower()==”userid”))
    {
     txtuser.text=tmp2[i+1];
    }
    else if((tmp2[i].tolower()==”database”)||(tmp2[i].tolower()==”initial catalog”))
    {
     txtdatabase.text=tmp2[i+1];
    }
    else if((tmp2[i].tolower()==”pwd”)||(tmp2[i].tolower()==”password”))
    {
     txtpassword.text=”*******************”;
     password=tmp2[i+1];
    }
    continue;

   }
   #endregion
  }
  #region 加密方法
  public string encrypt(string ptoencrypt, string skey)
  {
   descryptoserviceprovider des = new descryptoserviceprovider();
   byte[] inputbytearray = encoding.default.getbytes(ptoencrypt);

   //建立加密对象的密钥和偏移量
   //原文使用asciiencoding.ascii方法的getbytes方法
   //使得输入密码必须输入英文文本
   des.key = asciiencoding.ascii.getbytes(skey);
   des.iv = asciiencoding.ascii.getbytes(skey);
   memorystream ms = new memorystream();
   cryptostream cs = new cryptostream(ms, des.createencryptor(),cryptostreammode.write);
   cs.write(inputbytearray, 0, inputbytearray.length);
   cs.flushfinalblock();
   stringbuilder ret = new stringbuilder();
   foreach(byte b in ms.toarray())
   {
    ret.appendformat(“{0:x2}”, b);
   }
   ret.tostring();
   return ret.tostring();
  }
  #endregion
  #region 解密方法
  public string decrypt(string ptodecrypt, string skey)
  {
   descryptoserviceprovider des = new descryptoserviceprovider();

   byte[] inputbytearray = new byte[ptodecrypt.length / 2];
   for(int x = 0; x < ptodecrypt.length / 2; x++)
   {
    int i = (convert.toint32(ptodecrypt.substring(x * 2, 2), 16));
    inputbytearray[x] = (byte)i;
   }

   //建立加密对象的密钥和偏移量,此值重要,不能修改
   des.key = asciiencoding.ascii.getbytes(skey);
   des.iv = asciiencoding.ascii.getbytes(skey);
   memorystream ms = new memorystream();
   cryptostream cs = new cryptostream(ms, des.createdecryptor(),cryptostreammode.write);

   cs.write(inputbytearray, 0, inputbytearray.length);
   cs.flushfinalblock();

   //建立stringbuild对象,createdecrypt使用的是流对象,必须把解密后的文本变成流对象
   stringbuilder ret = new stringbuilder();

   return system.text.encoding.default.getstring(ms.toarray());
  }
  #endregion
  private void txtpassword_textchanged(object sender, system.eventargs e)
  {
   password=txtpassword.text.trim();
  }

  private void btndeletenode_click(object sender, system.eventargs e)
  {
   if(!inittxtfile())
   {
    return;
   }
   xmldocument xd=new xmldocument();
   xd.load(fi.fullname);

   xmlnodelist nodelist=null;
   try
   {
    nodelist=xd.selectsinglenode(“/configuration/appsettings”).childnodes; //获取appsettings节点的所有子节点
   }
   catch
   {
    messagebox.show(“删除配置节失败,未找到配置节!”,”错误”,messageboxbuttons.ok,messageboxicon.error);
    return;
   }
   int clearnodes=0;
   foreach(xmlnode node in nodelist)
   {
    xmlelement xe=(xmlelement)node; //将子节点类型转换为xmlelement类型
    if(xe.getattribute(“key”)==txtkey.text.trim())
    {
     node.removeall();
     clearnodes++;
    }
   }
   if(clearnodes>0)
   {
    xd.save(txtfile.text);
    messagebox.show(“删除配置节成功!”,”删除成功”,messageboxbuttons.ok,messageboxicon.information);
   }
  }
 }
}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 用DES加密数据库信息,增强Asp.net的安全性(2)–实现源码-.NET教程,Asp.Net开发
分享到: 更多 (0)