日期:2008-07-29  浏览次数:20447 次

'EZ_Crypt.vb(字符串加密和解密类)

'参考Namespace
Imports System
Imports Microsoft.VisualBasic

Imports System.Collections
Imports System.Configuration

Namespace Security
    Public Class Crypt
    '加密字符串
        Public Function Encrypt(ByVal PlainStr As String, ByVal key As String) As String
            Dim strChar, KeyChar, NewStr As String
            Dim Pos As Integer
            Dim i, intLen As Integer
            Dim Side1, Side2 As String
            Pos = 1

            For i = 1 To Len(PlainStr)
                strChar = Mid(PlainStr, i, 1)
                KeyChar = Mid(key, Pos, 1)
                NewStr = NewStr & Chr(Asc(strChar) Xor Asc(KeyChar))
                If Pos = Len(key) Then Pos = 0
                Pos = Pos + 1
            Next

            If Len(NewStr) Mod 2 = 0 Then
                Side1 = StrReverse(Left(NewStr, (Len(NewStr) / 2)))
                Side2 = StrReverse(Right(NewStr, (Len(NewStr) / 2)))
                NewStr = Side1 & Side2
            End If

            Encrypt = NewStr
        End Function

    '解密字符串
        Public Function Decrypt(ByVal PlainStr As String, ByVal key As String) As String
            Dim strChar, KeyChar, NewStr As String
            Dim Pos As Integer
            Dim i As Integer
            Dim Side1 As String
            Dim Side2 As String
            Pos = 1

            If Len(PlainStr) Mod 2 = 0 Then
                Side1 = StrReverse(Left(PlainStr, (Len(PlainStr) / 2)))
                Side2 = StrReverse(Right(PlainStr, (Len(PlainStr) / 2)))