一个可逆加密的例子

一个可逆加密的例子

下面的这个例子实现了一个可逆加密的例子功能。代码很简单,这里就不多解释了。代码如下:

EncString.aspx

<% @ Page Language = " vb "  AutoEventWireup = " false "  Codebehind = " EncString.aspx.vb "  Inherits = " aspxWeb.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="JavaScript">
    
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
  
HEAD>
  
<body MS_POSITIONING="GridLayout">
    
<h3 align="center">一个可逆加密的例子 h3>
    
<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  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
     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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值