简单的加密方法
将字符串转换为机器内码,再将内码还原为字符串,实现加密解密,没有密匙
'功能将一个字符转换为16进制内码表示
'入口:strChar:一个Char类型的字符
'出口:返回给定字符的Ascii码,用字符串表示,占用四个字符的位置
Public Shared Function CharTo16Asc(ByVal strChar As Char) As String
If strChar = Nothing Then Return "" '如果为空直接退出
Dim str16Mid As String = Hex(Asc(strChar)).ToString
'将一个字符转换为ascii码
'判断转换后是否占用了四个字符的位置
If str16Mid.Length >= 4 Then '转换的是汉字,转换后由于占用了4个字节,需要将高两个字节截取掉
Return str16Mid.Substring(str16Mid.Length - 4)
Else '转换后小于2个字节的,需要在前面添加FFFF等
Dim intCharAscLen As String = str16Mid.Length '长度
Dim i As Integer
Dim strF As String = ""
For i = 0 To 4 - intCharAscLen - 1
strF &= "0"
Next
Return strF & str16Mid
End If
End Function
'功能将16进制内码表示的字符转换为字符
'入口:str16Asc:一个使用十六进制表示的一个字符的内码,如:FEA3
'出口:返回字符
Public Shared Function Asc16ToChar(ByVal str16Asc As String) As Char
If str16Asc = "" Then Return Nothing
Return Chr("&H" & str16Asc) '将一个字符转换为ascii码
End Function
'功能将一个字符串转换为16进制内码表示
'入口:strString:一个字符串
'出口:返回给定字符的Ascii码串,用字符串表示,每个字符占用四个字符的位置
Public Shared Function StringTo16Asc(ByVal strString As String) As String
If strString = "" Then Return ""
Dim charMid() As Char = strString.ToCharArray '将字符串以char形式保存到数组中
Dim charOnlyMid As Char
Dim strAscMid As String = ""
For Each charOnlyMid In charMid
strAscMid &= CharTo16Asc(charOnlyMid)
Next
Return strAscMid
End Function
'功能将一个16进制内码表示的字符串转换为字符串
'入口:str16AscString:一个使用十六进制表示的字符串的内码,如:FEA3ADED
'出口:返回字符串
Public Shared Function Asc16ToString(ByVal str16AscString As String) As String
If str16AscString = "" Then Return ""
Dim i As Integer
Dim strMid As String = ""
For i = 0 To str16AscString.Length \ 4 - 1 '一次提取4个字符
Dim strOnlyMid As String = str16AscString.Substring(i * 4, 4)
strMid &= Asc16ToChar(strOnlyMid)
Next
Return strMid
End Function
End Class