📄 classencrypttext.vb
字号:
#Region "信息加密和解密"
Public Class EncryptText
#Region "Function HandleChar() —— 字符运算处理"
Public Function HandleChar(ByVal SourceChar As Integer, ByVal EChar As Char) As Integer
Dim tempchar
tempchar = SourceChar
tempchar = tempchar Xor Asc(EChar)
Return tempchar
End Function
#End Region
#Region "Function EncryptString() —— 字符串加密"
Public Function EncryptString(ByVal sourcestr As String, ByVal EStr As String) As String
If EStr = "" Then
EStr = "lipeng@cs-air" '密钥Estr的默认值
End If
Dim si, ei As Integer
Dim Stempchar, result As String
Dim tempresult As Integer
Dim Etempchar As String
si = 1
Stempchar = Left(sourcestr, 1)
result = ""
Do While Not Stempchar = "" '依次加密明文字符
ei = 1
Etempchar = Left(EStr, 1) '选取密钥的第一个字符
tempresult = Asc(Stempchar)
Do While Not Etempchar = "" '依次选取密钥与明文字符运算
tempresult = HandleChar(tempresult, Etempchar)
Etempchar = Left(Right(EStr, Len(EStr) - ei), 1)
ei = ei + 1 '选取下一个密钥字符
Loop
If result <> "" Then
result = result + "-" + Trim(tempresult) '密文数据以 '-' 相连
Else
result = result + Trim(tempresult)
End If
'取下一个明文字符
Stempchar = Left(Right(sourcestr, Len(sourcestr) - si), 1)
si = si + 1
Loop
Return result
End Function
#End Region
#Region "Function DecryptString() —— 字符串解密"
Public Function DecryptString(ByVal sourcestr As String, ByVal DStr As String) As String
If DStr = "" Then
DStr = "lipeng@cs-air"
End If
If Trim(sourcestr) = "" Then Return ""
Dim si, di As Integer
Dim Stempchar, result As String
Dim Dtempchar As String
Dim tempresult As Integer
Dim sstrarr() As String
Dim arrcount, arri As Integer
sstrarr = Split(sourcestr, "-")
arrcount = UBound(sstrarr)
result = ""
For si = 0 To arrcount
tempresult = sstrarr(si)
di = 1
Dtempchar = Right(DStr, 1)
Do While Not Dtempchar = ""
tempresult = HandleChar(tempresult, Dtempchar)
Dtempchar = Right(Left(DStr, Len(DStr) - di), 1)
di = di + 1
Loop
result = result + Chr(tempresult)
Next
Return result
End Function
#End Region
#Region "Function ImprovedEncryptString() —— 改进的字符串加密"
Public Function ImprovedEncryptString(ByVal sourcestr As String, ByVal EStr As String) As String
If EStr = "" Then
EStr = "lipeng@cs-air"
End If
Dim si, ei, Estrlen As Integer
Dim Stempchar, result As String
Dim tempresult As Integer
Dim Etempchar As String
Dim EcharArr() As Char
Estrlen = EStr.Length
ReDim EcharArr(Estrlen)
EcharArr = EStr '将密钥字符串赋给字符数组
si = 1
ei = 0
Stempchar = Left(sourcestr, 1)
result = ""
Do While Not Stempchar = ""
tempresult = Asc(Stempchar)
Etempchar = EcharArr(ei)
tempresult = HandleChar(tempresult, Etempchar)
ei = ei + 1 '选取下一个密钥
ei = ei Mod Estrlen '当密钥字符选取完后就从头开始
If result <> "" Then
result = result + "-" + Trim(tempresult) '密文数据以 '-' 相连
Else
result = result + Trim(tempresult)
End If
'取下一个明文字符
Stempchar = Left(Right(sourcestr, Len(sourcestr) - si), 1)
si = si + 1
Loop
Return result
End Function
#End Region
#Region "Function ImprovedDecryptString() —— 改进的字符串解密"
Public Function ImprovedDecryptString(ByVal sourcestr As String, ByVal DStr As String) As String
If DStr = "" Then
DStr = "lipeng@cs-air"
End If
If Trim(sourcestr) = "" Then Return ""
Dim si, di, Dstrlen As Integer
Dim Stempchar, result As String
Dim Dtempchar As String
Dim tempresult As Integer
Dim DcharArr() As Char
Dstrlen = DStr.Length
ReDim DcharArr(Dstrlen)
DcharArr = DStr
Dim sstrarr() As String
Dim arrcount, arri As Integer
sstrarr = Split(sourcestr, "-")
arrcount = UBound(sstrarr)
result = ""
di = 0
For si = 0 To arrcount
tempresult = sstrarr(si)
Dtempchar = DcharArr(di)
tempresult = HandleChar(tempresult, Dtempchar)
di = di + 1
di = di Mod Dstrlen
result = result + Chr(tempresult)
Next
Return result
End Function
#End Region
End Class
#End Region
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -