一個可逆加密的例子 (轉)[@more@]
一個可逆的例子
playground.com/">
下面的這個例子實現了一個可逆加密的例子功能。程式碼很簡單,這裡就不多解釋了。程式碼如下:
EncString.x
aspx.vb" Inherits="aspx.EncString"%>
w3c//DTD HTML 4.0 Transitional//EN">
一個可逆加密的例子
microsoft .NET 7.0">
7.0">
script">
intellisense/">
一個可逆加密的例子
RM id="Form1" method="post" runat="server">
edIndd="ShowRes">
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.
buggerStepThrough()> 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("加密")
MyList.Add("")
RadioButtonList1.Data = 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 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 = {&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
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752019/viewspace-957894/,如需轉載,請註明出處,否則將追究法律責任。