国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

一個可逆加密的例子

2024-07-21 02:30:02
字體:
來源:轉載
供稿:網友

    下面的代碼實現(xiàn)了一個可逆加密的方法。可以用于對cookie,querystring等加密處理。
  
  查看例子
  
  vb.net代碼
  
  <%@ page language="vb" autoeventwireup="false" codebehind="encstring.<a target="_blank">asp</a>x.vb"
   inherits="aspx<a 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 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;
  }
  }
  }
  }

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 迁安市| 固镇县| 上蔡县| 双峰县| 桐梓县| 乌拉特前旗| 精河县| 平和县| 嵊州市| 建水县| 方山县| 芜湖县| 凉山| 正宁县| 拉孜县| 佛教| 佛学| 肃南| 阜阳市| 黔南| 崇礼县| 伊通| 浮梁县| 桐柏县| 皮山县| 谷城县| 正蓝旗| 社旗县| 德保县| 红安县| 吉首市| 连云港市| 合肥市| 尚志市| 弋阳县| 固安县| 娄底市| 富蕴县| 鸡泽县| 平谷区| 伊春市|