一个可逆加密的例子

王朝other·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

一个可逆加密的例子

作者:孟宪会 出自:【孟宪会之精彩世界】 发布日期:2003年5月23日 8点25分47秒

下面的代码实现了一个可逆加密的方法。可以用于对Cookie,QueryString等加密处理 。

查 看例子

一个可逆加密的例子

后端代码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.

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 = "一个可逆加密的例子"

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有  導航