下面的代码实现了一个可逆加密的方法。可以用于对cookie,querystring等加密处理。 
   
  查看例子 
   
  vb.net代码 
   
  <%@ page language="vb" autoeventwireup="false" codebehind="encstring.<a href="http://dev.21tx.com/web/asp/" target="_blank">asp</a>x.vb" 
   inherits="aspx<a href="http://dev.21tx.com/web/" target="_blank">web</a>.encstring" %> 
  <!doctype html public "-//w3c//dtd html 4.0 transitional//en"> 
  <html> 
   <head> 
   <title>一个可逆加密的例子</title> 
   <meta name="generator" content="microsoft visual studio.net 7.0"> 
   <meta name="code_language" content="visual basic 7.0"> 
   <meta name="vs_defaultclientscript" content="<a href="http://dev.21tx.com/web/javascript/" target="_blank">javascript</a>"> 
   <meta name="vs_targetschema" content="http://schemas.microsoft.com/intellisense/ie5"> 
   </head> 
   <body ms_positioning="gridlayout"> 
   <asp:label id="label1" runat="server"></asp:label> 
   <p align="center"> 
   <form id="form1" method="post" runat="server"> 
   <font face="宋体"> 
   <asp:textbox id="textbox1" runat="server" width="96%"></asp:textbox> 
   <asp:radiobuttonlist id="radiobuttonlist1" runat="server" font-bold="true" 
   repeatdirection="horizontal" autopostback="true" onselectedindexchanged="showres"> 
   </asp:radiobuttonlist> 
   <asp:textbox id="textbox2" runat="server" width="96%"></asp:textbox> 
   </font> 
   </form> 
   </p> 
   </body> 
  </html> 
   
  后端代码encstring.aspx.vb: 
  imports system 
  imports system.io 
  imports system.xml 
  imports system.text 
  imports system.security.cryptography 
  public class encstring 
   inherits system.web.ui.page 
   protected withevents textbox1 as system.web.ui.webcontrols.textbox 
   protected withevents textbox2 as system.web.ui.webcontrols.textbox 
   protected withevents form1 as system.web.ui.htmlcontrols.htmlform 
   protected withevents label1 as system.web.ui.webcontrols.label 
   protected withevents radiobuttonlist1 as system.web.ui.webcontrols.radiobuttonlist 
   
  #region " web form designer generated code " 
   
   'this call is required by the web form designer. 
   <system.diagnostics.debuggerstepthrough()> private sub initializecomponent() 
   
   end sub 
   
   private sub page_init(byval sender as system.object, byval e as system.eventargs) handles 
   
  mybase.init 
   'codegen: this method call is required by the web form designer 
   'do not modify it using the code editor. 
   initializecomponent() 
   end sub 
   
  #end region 
   
   private sub page_load(byval sender as system.object, byval e as system.eventargs) handles 
   
  mybase.load 
   'put user code to initialize the page here 
   label1.text = "<h3 align='center'>一个可逆加密的例子</h3>" 
   if not ispostback then 
   dim mylist as new arraylist() 
   mylist.add("加密") 
   mylist.add("解密") 
   radiobuttonlist1.datasource = mylist 
   radiobuttonlist1.databind() 
   end if 
   end sub 
   
   ' 加密 
   public shared function encrypttext(byval strtext as string) as string 
   return encrypt(strtext, "&%#@?,:*") 
   end function 
   
   '解密 
   public shared function decrypttext(byval strtext as string) as string 
   return decrypt(strtext, "&%#@?,:*") 
   end function 
   
   '加密函数 
   private shared function encrypt(byval strtext as string, byval strencrkey as string) as string 
   dim bykey() as byte = {} 
   dim iv() as byte = {&h12, &h34, &h56, &h78, &h90, &hab, &hcd, &hef} 
   try 
   bykey = system.text.encoding.utf8.getbytes(left(strencrkey, 8)) 
   dim des as new descryptoserviceprovider() 
   dim inputbytearray() as byte = encoding.utf8.getbytes(strtext) 
   dim ms as new memorystream() 
   dim cs as new cryptostream(ms, des.createencryptor(bykey, iv), cryptostreammode.write) 
   cs.write(inputbytearray, 0, inputbytearray.length) 
   cs.flushfinalblock() 
   return convert.tobase64string(ms.toarray()) 
   catch ex as exception 
   return ex.message 
   end try 
   end function 
   
   '解密函数 
   private shared function decrypt(byval strtext as string, byval sdecrkey as string) as string 
   dim bykey() as byte = {} 
   dim iv() as byte = {&h12, &h34, &h56, &h78, &h90, &hab, &hcd, &hef} 
   dim inputbytearray(strtext.length) as byte 
   try 
   bykey = system.text.encoding.utf8.getbytes(left(sdecrkey, 8)) 
   dim des as new descryptoserviceprovider() 
   inputbytearray = convert.frombase64string(strtext) 
   dim ms as new memorystream() 
   dim cs as new cryptostream(ms, des.createdecryptor(bykey, iv), cryptostreammode.write) 
   cs.write(inputbytearray, 0, inputbytearray.length) 
   cs.flushfinalblock() 
   dim encoding as system.text.encoding = system.text.encoding.utf8 
   return encoding.getstring(ms.toarray()) 
   catch ex as exception 
   return ex.message 
   end try 
   end function 
   
   public sub showres(byval sender as object, byval e as system.eventargs)_ 
   handles radiobuttonlist1.selectedindexchanged 
   if radiobuttonlist1.selectedindex = 0 then 
   textbox2.text = encrypttext(textbox1.text) 
   else 
   textbox2.text = decrypttext(textbox1.text) 
   end if 
   end sub 
  end class
   c#代码 
   
  encryptstring.aspx 
   
  <%@ page language="c#" enableviewstate = "true" codebehind="encryptstring.aspx.cs" autoeventwireup="false" inherits="emeng.exam.encryptstring" %> 
  <!doctype html public "-//w3c//dtd html 4.0 transitional//en"> 
  <html> 
  <head> 
  <title>一个可逆加密的例子</title> 
  <meta name="generator" content="microsoft visual studio.net 7.0"> 
  <meta name="code_language" content="visual basic 7.0"> 
  <meta name="vs_defaultclientscript" content="javascript"> 
  <meta name="vs_targetschema" content="http://schemas.microsoft.com/intellisense/ie5"> 
  </head> 
  <body> 
  <form id="form1" method="post" runat="server"> 
  <p align="center">一个可逆加密的例子 
  <asp:textbox id="textbox1" runat="server" width="96%">http://dotnet.aspx.cc/</asp:textbox> 
  <asp:radiobuttonlist id="radiobuttonlist1" runat="server" font-bold="true" repeatdirection="horizontal" 
  autopostback="true"></asp:radiobuttonlist> 
  <asp:textbox id="textbox2" runat="server" width="96%"></asp:textbox> 
  <asp:textbox id="textbox3" runat="server" width="96%"></asp:textbox> 
  </p> 
  </form> 
  </body> 
  </html> 
   
  encryptstring.aspx.cs 
   
  using system; 
  using system.collections; 
  using system.componentmodel; 
  using system.drawing; 
  using system.web; 
  using system.web.sessionstate; 
  using system.web.ui; 
  using system.web.ui.webcontrols; 
  using system.web.ui.htmlcontrols; 
  using system.io; 
  using system.text; 
  using system.security.cryptography; 
   
   
  namespace emeng.exam 
  { 
  /// <summary> 
  /// encryptstring 的摘要说明。 
  /// </summary> 
  public class encryptstring : system.web.ui.page 
  { 
  protected system.web.ui.webcontrols.textbox textbox1; 
  protected system.web.ui.webcontrols.radiobuttonlist radiobuttonlist1; 
  protected system.web.ui.webcontrols.textbox textbox2; 
  protected system.web.ui.webcontrols.textbox textbox3; 
  protected system.web.ui.htmlcontrols.htmlform form1; 
   
  private void page_load(object sender, system.eventargs e) 
  { 
  // 在此处放置用户代码以初始化页面 
  if(!this.ispostback) 
  { 
  arraylist mylist = new arraylist(); 
  mylist.add("加密"); 
  mylist.add("解密"); 
  radiobuttonlist1.datasource = mylist; 
  radiobuttonlist1.databind(); 
  } 
  } 
   
  #region web 窗体设计器生成的代码 
  override protected void oninit(eventargs e) 
  { 
  // 
  // codegen: 该调用是 asp.net web 窗体设计器所必需的。 
  // 
  initializecomponent(); 
  base.oninit(e); 
  } 
  /// <summary> 
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改 
  /// 此方法的内容。 
  /// </summary> 
  private void initializecomponent() 
  { 
  this.radiobuttonlist1.selectedindexchanged += new system.eventhandler(this.radiobuttonlist1_selectedindexchanged); 
  this.load += new system.eventhandler(this.page_load); 
   
  } 
  #endregion 
   
  private void radiobuttonlist1_selectedindexchanged(object sender, system.eventargs e) 
  { 
  if( radiobuttonlist1.selectedindex == 0) 
  textbox2.text = encrypttext(textbox1.text); 
  else 
  textbox3.text = decrypttext(textbox2.text); 
  } 
  // 加密 
  public string encrypttext(string strtext) 
  { 
  return encrypt(strtext, "&%#@?,:*"); 
  } 
   
  //'解密 
  public string decrypttext(string strtext) 
  { 
  return decrypt(strtext, "&%#@?,:*"); 
  } 
  //'加密函数 
  private string encrypt(string strtext, string strencrkey) 
  { 
  byte[] bykey = {}; 
  byte[] iv = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef}; 
  try 
  { 
  bykey = system.text.encoding.utf8.getbytes(strencrkey.substring(0, 8)); 
  descryptoserviceprovider des = new descryptoserviceprovider(); 
  byte[] inputbytearray = encoding.utf8.getbytes(strtext); 
  memorystream ms = new memorystream(); 
  cryptostream cs = new cryptostream(ms, des.createencryptor(bykey, iv), cryptostreammode.write); 
  cs.write(inputbytearray, 0, inputbytearray.length); 
  cs.flushfinalblock(); 
  return convert.tobase64string(ms.toarray()); 
  } 
  catch(exception ex) 
  { 
  return ex.message; 
  } 
  } 
   
  //'解密函数 
  private string decrypt(string strtext, string sdecrkey) 
  { 
  byte[] bykey = {}; 
  byte[] iv = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef}; 
  byte[] inputbytearray = new byte[strtext.length]; 
  try 
  { 
  bykey = system.text.encoding.utf8.getbytes(sdecrkey.substring(0, 8)); 
  descryptoserviceprovider des = new descryptoserviceprovider(); 
  inputbytearray = convert.frombase64string(strtext); 
  memorystream ms = new memorystream(); 
  cryptostream cs = new cryptostream(ms, des.createdecryptor(bykey, iv), cryptostreammode.write); 
  cs.write(inputbytearray, 0, inputbytearray.length); 
  cs.flushfinalblock(); 
  system.text.encoding encoding = system.text.encoding.utf8; 
  return encoding.getstring(ms.toarray()); 
  } 
  catch(exception ex) 
  { 
  return ex.message; 
  } 
  } 
  } 
  }
新闻热点
疑难解答