怎么实现Web系统URL传输(表单提交)参数加密--VB

Imports Microsoft.VisualBasic
Imports System
Imports System.Security.Cryptography
Imports System.IO
Imports System.Text

Namespace EIP.Framework

    ' Security 的摘要说明。
    ' Security类实现.NET框架下的加密和解密。
    'CopyRight KangSoft@Hotmail.com@Hotmail.com@hotmail.com

    Public Class Security
        Dim _QueryStringKey As String = "abcdefgh"  'URL传输参数加密Key
        Dim _PassWordKey As String = "hgfedcba"  'PassWord加密Key

        Public Sub New()
            '
            ' TODO: 在此处添加构造函数逻辑
            '
        End Sub

        '/ 加密URL传输的字符串

        Public Function EncryptQueryString(ByVal QueryString As String) As String
            Return Encrypt(QueryString, _QueryStringKey)
        End Function

        '/ 解密URL传输的字符串

        Public Function DecryptQueryString(ByVal QueryString As String) As String
            Return Decrypt(QueryString, _QueryStringKey)
        End Function

        '/ 加密帐号口令

        Public Function EncryptPassWord(ByVal PassWord As String) As String
            Return Encrypt(PassWord, _PassWordKey)
        End Function


        '/ 解密帐号口令

        Public Function DecryptPassWord(ByVal PassWord As String) As String
            Return Decrypt(PassWord, _PassWordKey)
        End Function


        '/ DEC 加密过程

        Public Function Encrypt(ByVal pToEncrypt As String, ByVal sKey As String) As String
            Dim des As DESCryptoServiceProvider = New DESCryptoServiceProvider()  '把字符串放到byte数组中 

            Dim inputByteArray() As Byte = Encoding.Default.GetBytes(pToEncrypt)
            'byte[]  inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt); 

            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey)  '建立加密对象的密钥和偏移量
            des.IV = ASCIIEncoding.ASCII.GetBytes(sKey)   '原文使用ASCIIEncoding.ASCII方法的GetBytes方法
            Dim ms As MemoryStream = New MemoryStream()  '使得输入密码必须输入英文文本
            Dim cs As CryptoStream = New CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write)

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

            Dim ret As StringBuilder = New StringBuilder()
            Dim b As Byte
            For Each b In ms.ToArray()
                ret.AppendFormat("{0:X2}", b)
            Next
            ret.ToString()
            Return ret.ToString()
        End Function

        '/ DEC 解密过程

        Public Function Decrypt(ByVal pToDecrypt As String, ByVal sKey As String) As String
            Dim des As DESCryptoServiceProvider = New DESCryptoServiceProvider()

            Dim inputByteArray() As Byte = New Byte(pToDecrypt.Length / 2) {}
            Dim x As Integer
            For x = 0 To pToDecrypt.Length / 2 - 1 Step x + 1
                Dim i As Integer = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16))
                inputByteArray(x) = CType(i, Byte)
            Next

            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey)  '建立加密对象的密钥和偏移量,此值重要,不能修改 
            des.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
            Dim ms As MemoryStream = New MemoryStream()
            Dim cs As CryptoStream = New CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write)

            cs.Write(inputByteArray, 0, inputByteArray.Length - 1)
            cs.FlushFinalBlock()

            Dim ret As StringBuilder = New StringBuilder()  '建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象 

            Return System.Text.Encoding.Default.GetString(ms.ToArray())
        End Function


        '/ 检查己加密的字符串是否与原文相同

        Public Function ValidateString(ByVal EnString As String, ByVal FoString As String, ByVal Mode As Integer) As Boolean
            Select Case Mode
                ' Case Else
                Case 1
                    If Decrypt(EnString, _QueryStringKey) = FoString.ToString() Then
                        Return True
                    Else
                        Return False
                    End If
                Case 2
                    If Decrypt(EnString, _PassWordKey) = FoString.ToString() Then
                        Return True
                    Else
                        Return False
                    End If
            End Select
        End Function
    End Class

End Namespace

調用時:
 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Label1.Text = "id=1"
        Dim objSecurity As EIP.Framework.Security = New EIP.Framework.Security()
        Me.Label1.Text = objSecurity.EncryptQueryString(Me.Label1.Text.Trim)

        Me.Label1.Text += objSecurity.DecryptQueryString(Me.Label1.Text.Trim)


    End Sub

posted @ 2006-09-15 11:25  Nina  阅读(1140)  评论(0)    收藏  举报