帮忙看看这段密码转换的程序是如何转换的
请尽可能详细解释下,谢谢。Public Function PassWordConvert(Instring As String)--------------------编程问答-------------------- 建一个工程,调用一下就很容易明白了。 --------------------编程问答-------------------- --------------------编程问答-------------------- 帮顶。 --------------------编程问答-------------------- 看在100分的份上,写一下解释吧:
Dim ConvertLength As Integer
Dim PassConvBuf(0 To 99) As Byte
For i = 1 To Len(Instring)
PassConvBuf(i - 1) = Asc(Mid(Instring, i, 1))
Next i
ConvertLength = Len(Instring)
' 密码算法:
' 每相邻两个码的和再高四位和低四位分别按位取反
On Error Resume Next
Dim Temp, temp1, temp2 As Byte
For i = 0 To ConvertLength - 2
Temp = (PassConvBuf(i) + PassConvBuf(i + 1)) And &HFF
temp1 = Temp And &HF
temp2 = (Temp And &HF0) / 16
temp1 = Not temp1
temp2 = Not temp2
PassConvBuf(i) = (temp2 * 16 + temp1) And &HFF
Next i
Dim OutString As String
For i = 0 To ConvertLength - 2
OutString = OutString & Hex(PassConvBuf(i))
Next i
PassWordConvert = OutString
End Function
Public Function PassWordConvert(Instring As String)
Dim ConvertLength As Integer
Dim PassConvBuf(0 To 99) As Byte
For i = 1 To Len(Instring)
PassConvBuf(i - 1) = Asc(Mid(Instring, i, 1))
'把 Instring 的每个字符的 ASCII码 依次填入 Byte数组 PassConvBuf 中。
'注:这只能对西文字符串进行处理,有汉字就会产生溢出错误
Next i
ConvertLength = Len(Instring)
' 我觉得这句应该写在 For 循环之前,可以少调用一次函数。
' ConvertLength = Len(Instring)
' For i = 1 To ConvertLength
' PassConvBuf(i - 1) = Asc(Mid(Instring, i, 1))
' Next i
' 密码算法:
' 每相邻两个码的和再高四位和低四位分别按位取反
On Error Resume Next '出错时从下一句继续执行(偶的程序中从来不这样写 -_-! )
Dim Temp, temp1, temp2 As Byte
'应该这样写:
'Dim Temp As Byte, temp1 As Byte, temp2 As Byte
For i = 0 To ConvertLength - 2
Temp = (PassConvBuf(i) + PassConvBuf(i + 1)) And &HFF
'把相邻两个字符的 ASCII码 相加,赋值给 Temp
'前面那句的 (....) And &HFF 多此一举
temp1 = Temp And &HF '求Temp的 低4bit 的值
temp2 = (Temp And &HF0) / 16 '求Temp的 高4bit 的值
'要算高4位,写成: temp2 = Temp \ 16 岂不是直接了当?
temp1 = Not temp1 '每位都取反
temp2 = Not temp2
PassConvBuf(i) = (temp2 * 16 + temp1) And &HFF
'把temp2‘左移4位’与temp1相加,求低字节的值,赋值给 PassConvBuf(i)
Next i
Dim OutString As String
For i = 0 To ConvertLength - 2
OutString = OutString & Hex(PassConvBuf(i))
'把转换后的“密码”用十六进制字符串的形式表示
Next i
PassWordConvert = OutString '把这个十六进制字符串作为函数值返回
End Function
--------------------编程问答-------------------- 其实这个算法是有问题的:
' 密码算法:
' 每相邻两个码的和再高四位和低四位分别按位取反
'..........
Temp = (PassConvBuf(i) + PassConvBuf(i + 1)) And &HFF
temp1 = Temp And &HF
temp2 = (Temp And &HF0) / 16
temp1 = Not temp1
temp2 = Not temp2
PassConvBuf(i) = (temp2 * 16 + temp1) And &HFF
'.........
它的结果并不是“每相邻两个码的和再高四位和低四位分别按位取反”
要“每相邻两个码的和再高四位和低四位分别按位取反”,这样写就行了:
For i = 0 To ConvertLength - 2
PassConvBuf(i) = Not (PassConvBuf(i) + PassConvBuf(i + 1))
Next
要得到和原来的算法相同的结果:
For i = 0 To ConvertLength - 2
PassConvBuf(i) = (Not (PassConvBuf(i) + PassConvBuf(i + 1)) - 16)
Next
--------------------编程问答-------------------- 郁闷~~~~~
要得到和原来的算法相同的结果:
For i = 0 To ConvertLength - 2
PassConvBuf(i) = (Not (PassConvBuf(i) + PassConvBuf(i + 1))) - 16
Next
--------------------编程问答-------------------- dinging
补充:VB , 基础类