利用序列的“加密算法”
本帖最后由 gdyabc 于 2015-10-11 21:22 编辑'------------------------------------------------------------------------------------------------------
'模块语言:VisualBasic 6.0(VB6)
'模块名称 :gCom_Encrypt
'模块功能 :字符串,文件加/解密
'功能设计 :孤云野鹤
'完成时间 : 2015/1/14 第一个版本,无法对中文加解密
'修改时间 : 2015/1/22 修改后经测试无BUG,完美对各种形式的编码字符串加解密
'-------------------------------------------------------------------------------------------------------
Const DatumStr = " 9A01D4253P647Q58ESabRcFTdUefghiCjklmnVoZpqWrGstIuvJXwHxyz~`!@Y#$%^&*(O)B_+N-={K}|\:"";'<L>?,./"
'----------------加密函数----------------
Public Function ECode(eString As String) As String
Dim EncryptString As String, i As Long
Dim TempHexString As String
TempHexString = ChrToHex(eString) '先将要加密的字符串转换为16进制
For i = 1 To Len(TempHexString) Step 1
EncryptString = EncryptString & Format(InStr(DatumStr, Mid(TempHexString, i, 1)), "00")'将转换后的16进制数据在"密码本"DatumStr中查找对应的序列位置,生成密文
Next
ECode = EncryptString
End Function
'----------------解密函数----------------
Public Function DCode(dString As String) As String
Dim HexNumber As Long, i As Long
Dim DencryptString As String
For i = 1 To Len(dString) Step 2 '将加密后的字符串序列在"密码本中"DatumStr查找对应的字符串,还原成明文
HexNumber = Mid(dString, i, 2)
DencryptString = DencryptString & Mid(DatumStr, HexNumber, 1)
Next
DCode = HexToChr(DencryptString) '将16进制还原成10进制字符串
End Function
Function ChrToHex(ByVal ECode As String) As String
Dim i As Long, b As String
For i = 1 To Len(ECode)
b = b & Hex(Asc(Mid(ECode, i, 1))) & " " '将10进制字符串转换成16进制数据
Next
ChrToHex =Trim(b)
End Function
Function HexToChr(ByVal DCode As String) As String
Dim s() As String, b As String
Dim i As Long
s = Split(RTrim(DCode), " ")
For i = 0 To UBound(s) Step 1
b = b & Chr("&H" & s(i)) '将16进制数据转换成10进制字符串
Next
HexToChr = b
End Function
学习了,谢谢楼主! 这个不错,值得学习。 下载收藏学习。。
页:
[1]