CSDN博客

img xuandme000

对称加密解密模块

发表于2004/9/29 10:51:00  637人阅读

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

阅读全文
0 0

相关文章推荐

img
取 消
img