工作中经常会需要计算一大串字节的异或,用计算器手工一个一个的输入太累人了,碰巧这两天在关注VBScript,于是就想到写了这个工具。这是我第一次用VB,代码很简单,希望能对你有用。
'==========================================================================
'
'
' NAME: calc_xor.vbs
'
' COMMENT: Calculates the result of 'xor' all elements in the input
'
'==========================================================================
Option Explicit
Dim strInput
'Promt for string to search for in log files
strInput = InputBox("Enter data to calc on.","calc xor","")
if strInput = "" then
wscript.quit
end If
'MsgBox(Len(strInput))
Dim arrayBytes()
ReDim arrayBytes(Len(strInput)/2)
Dim i,nBytes
Dim chHalf1,chHalf2,chWhole
i = 1
nBytes = 0
Do While (i < Len(strInput))
'Skip spaces between elements
Do While Asc(Mid(strInput,i,1)) = 32 'space
i = i + 1
Loop
chHalf1 = Asc(Mid(strInput,i,1))
chHalf2 = Asc(Mid(strInput,i+1,1))
'Check and convert first half
If chHalf1 >= 48 And chHalf1 <= 57 Then
chHalf1 = chHalf1 - 48
ElseIf chHalf1 >= 65 And chHalf1 <= 70 Then
chHalf1 = chHalf1 - 65 + 10
ElseIf chhalf1 >= 97 And chHalf1 <= 104 Then
chHalf1 = chHalf1 - 97 + 10
Else
MsgBox("invalid character")
wscript.quit
End If
' Check and convert the second half
If chHalf2 >= 48 And chHalf2 <= 57 Then
chHalf2 = chHalf2 - 48
ElseIf chHalf2 >= 65 And chHalf2 <= 70 Then
chHalf2 = chHalf2 - 65 + 10
ElseIf chHalf2 >= 97 And chHalf2 <= 104 Then
chHalf2 = chHalf2 - 97 + 10
Else
MsgBox("invalid character")
wscript.quit
End If
' Combine the first and second halves together to form a whole byte
chWhole = chHalf1 * 16 + chHalf2
arrayBytes(nBytes) = chWhole
i = i + 2
nBytes = nBytes + 1
Loop
'MsgBox(CStr(nBytes) + " bytes all together")
Dim WshSHell
set WshShell = CreateObject("WScript.Shell")
WshShell.Run("calc")
WScript.Sleep(100)
WshShell.AppActivate("Calculator")
WScript.Sleep(100)
WshShell.SendKeys("{F6}") 'Change to Decimal
'WshShell.SendKeys("{F4}") 'Change to single byte
WScript.Sleep(100)
WshShell.SendKeys("0")
For i =1 to nBytes
WshShell.SendKeys("{^}")
WScript.Sleep(100)
WshShell.SendKeys(arrayBytes(i-1))
WScript.Sleep(100)
WshShell.SendKeys("{=}")
WScript.Sleep(100)
Next
WshShell.SendKeys("{F5}") 'Change to Hex for easy recognition