一個可逆加密的例子 (轉)

amyz發表於2007-08-15
一個可逆加密的例子 (轉)[@more@]

一個可逆的例子

playground.com/">

下面的這個例子實現了一個可逆加密的例子功能。程式碼很簡單,這裡就不多解釋了。程式碼如下:

EncString.x

aspx.vb&quot; Inherits=&quot;aspx.EncString&quot;%&gt; w3c//DTD HTML 4.0 Transitional//EN&quot;&gt; <title>一個可逆加密的例子</title> <meta name="GENERATOR" content="a href=" tag-126-1.html="">microsoft .NET 7.0&quot;&gt; <meta name="CODE_LANGUAGE" content="a href=" tag-50-1.html=""> 7.0&quot;&gt; <meta name="vs_defaultClientScript" content="a href=" tag-133-1.html="">script&quot;&gt; <meta name="vs_targetSchema" content=" href=" tag-340-1.html="">intellisense/&quot;&gt; <h3 align="center">一個可逆加密的例子</h3> <p> <fo href="http://blog.itpub.net/10752019/viewspace-957894/tag-415-1.html">RM id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;&gt; edIndd=&quot;ShowRes&quot;&gt; 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 &quot; Web Form Designer Generated Code &quot; 'This call is required by the Web Form Designer. <system href="http://blog.itpub.net/10752019/viewspace-957894/tag-268-1.html">buggerStepThrough()&gt; Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.,_ 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(&quot;加密&quot;) MyList.Add(&quot;&quot;) RadioButtonList1.Data = MyList RadioButtonList1.DataBind() End If End Sub ' 加密 Public Shared Function EncryptText(ByVal strText As String) As String Return Encrypt(strText, &quot;&amp;%#@?,:*&quot;) End Function '解密 Public Shared Function DecryptText(ByVal strText As String) As String Return Decrypt(strText, &quot;&amp;%#@?,:*&quot;) End Function '加密 Private Shared Function Encrypt(ByVal strText As String,_ ByVal strEncrKey As String) As String Dim byKey() As Byte = {} Dim IV() As Byte = {&amp;H12, &amp;H34, &amp;H56, &amp;H78, &amp;H90, &amp;HAB, &amp;HCD, &amp;HEF} Try byKey = System.Text.Encoding.UTF8.GetBytes(Left(strEncrKey, 8)) Dim des As New DESCryptoServiceProvr() 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 = {&amp;H12, &amp;H34, &amp;H56, &amp;H78, &amp;H90, &amp;HAB, &amp;HCD, &amp;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 </system></fo></p>

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752019/viewspace-957894/,如需轉載,請註明出處,否則將追究法律責任。

相關文章