对称加密解密模块

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

Imports System.Text

Imports System.Security.Cryptography

Module ModSecurity

Function EnText(ByVal Text As String, ByVal sKey As String) As String

Text = Text.ToLower

Dim des As New DESCryptoServiceProvider

Dim inputByteArray() As Byte

inputByteArray = Encoding.Default.GetBytes(Text)

des.Key = ASCIIEncoding.ASCII.GetBytes(Left(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5"), 8))

des.IV = ASCIIEncoding.ASCII.GetBytes(Left(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5"), 8))

Dim ms As New System.IO.MemoryStream

Dim cs As New CryptoStream(ms, des.CreateEncryptor, CryptoStreamMode.Write)

cs.Write(inputByteArray, 0, inputByteArray.Length)

cs.FlushFinalBlock()

Dim ret As New StringBuilder

Dim b As Byte

For Each b In ms.ToArray()

ret.AppendFormat("{0:X2}", b)

Next

Return ret.ToString()

End Function

Function DeText(ByVal Text As String, ByVal sKey As String) As String

Dim des As New DESCryptoServiceProvider

Dim len As Integer

len = Text.Length / 2 - 1

Dim inputByteArray(len) As Byte

Dim x, i As Integer

For x = 0 To len

i = Convert.ToInt32(Text.Substring(x * 2, 2), 16)

inputByteArray(x) = CType(i, Byte)

Next

des.Key = ASCIIEncoding.ASCII.GetBytes(Left(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5"), 8))

des.IV = ASCIIEncoding.ASCII.GetBytes(Left(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5"), 8))

Dim ms As New System.IO.MemoryStream

Dim cs As New CryptoStream(ms, des.CreateDecryptor, CryptoStreamMode.Write)

cs.Write(inputByteArray, 0, inputByteArray.Length)

cs.FlushFinalBlock()

Return Encoding.Default.GetString(ms.ToArray)

End Function

End Module

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