Skip to content

Instantly share code, notes, and snippets.

@seiren-naru-shirayuri
Last active November 21, 2022 10:54
Show Gist options
  • Save seiren-naru-shirayuri/8a9076c7e1da6373ded23d5d4a447b50 to your computer and use it in GitHub Desktop.
Save seiren-naru-shirayuri/8a9076c7e1da6373ded23d5d4a447b50 to your computer and use it in GitHub Desktop.
Some useful scripts in VBScript

Scripts

Some useful scripts in VBScript.

This gist is released under GLWTPL.

Type cscript <scriptname> /? for detailed help.

Bin2Hex Convert binary file to bin.hex encoded text file.

Hex2Bin Convert bin.hex encoded text file to binary file.

ScriptEncoder Encode vbscript into encoded vbscript (vbe).

FileGen Generate a vbs that generates the original file.

Hash Calculate the hash of file.

FileSplitter Split file or combine files.

XorCrypt Encrypt or decrypt file in xor with one-byte key.

XorKeyCrypt Encrypt or decrypt file in xor with key file.

KeyGen Generate key file of specified length.

EncodingConvert Convert file encoded in one encoding to another.

HZConvert Convert input to HZ or vice versa.

UTF7Convert Convert input to UTF-7 or vice versa.

Base64Convert Convert input to Base64 or vice versa.

Option Explicit
Const StdIn = 0
Const adTypeBinary = 1, adTypeText = 2
Dim stream, fso, node
Dim charset, str
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set stream = CreateObject("ADODB.Stream")
Set node = CreateObject("Msxml2.DOMDocument").CreateElement("base64")
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot create COM objects.")
WScript.Quit 1
End If
On Error GoTo 0
If WScript.Arguments.Count = 0 Then
WScript.StdErr.WriteLine("Missing parameter")
WScript.Quit 1
End If
If WScript.Arguments.Named.Exists("?") Then
WScript.StdOut.Write("Convert input to base64 or vice versa" & vbNewLine & _
vbNewLine & _
"Base64Convert /e|/d:[encoding] [/w] [[/f] /a|/u] String" & vbNewLine & _
vbNewLine & _
"/e Encode string as base64" & vbNewLine & _
"/d Decode string from base64" & vbNewLine & _
"encoding The encoding of string" & vbNewLine & _
"/w Convert half-width characters to full-width after encoding or convert full-width" & vbNewLine & _
" characters to half-width before decoding" & vbNewLine & _
"/f Read input from stdin instead of from command line" & vbNewLine & _
"/a Read stdin as ANSI" & vbNewLine & _
"/u Read stdin as Unicode" & vbNewLine & _
"String String to be converted" & vbNewLine & _
vbNewLine & _
"Note:" & vbNewLine & _
"/s is not compatible with /e" & vbNewLine & _
"If encoding is omitted, the default encoding is UTF-8" & vbNewLine & _
vbNewLine & _
"Examples:" & vbNewLine & _
"cscript Base64Convert.vbs /e:gbk /c 母の母はハハハと笑う" & vbNewLine & _
"cscript Base64Convert.vbs /d /f /a < base64.txt")
WScript.Quit
End If
If WScript.Arguments.Named.Exists("e") And WScript.Arguments.Named.Exists("d") Then
WScript.StdErr.WriteLine("Conflict switches /e and /d.")
WScript.Quit 1
ElseIf Not WScript.Arguments.Named.Exists("e") And Not WScript.Arguments.Named.Exists("d") Then
WScript.StdErr.WriteLine("Missing switches /e or /d.")
WScript.Quit 1
Else
If WScript.Arguments.Named.Exists("e") Then
charset = WScript.Arguments.Named("e")
If IsEmpty(charset) Then charset = "utf-8"
end If
If WScript.Arguments.Named.Exists("d") Then
charset = WScript.Arguments.Named("d")
If IsEmpty(charset) Then charset = "utf-8"
end If
End If
If WScript.Arguments.Named.Exists("f") Then
If WScript.Arguments.Named.Exists("a") And WScript.Arguments.Named.Exists("u") Then
WScript.StdErr.WriteLine("Conflict switches /a and /u.")
WScript.Quit 1
ElseIf Not WScript.Arguments.Named.Exists("a") And Not WScript.Arguments.Named.Exists("u") Then
WScript.StdErr.WriteLine("Missing switch /a or /u.")
WScript.Quit 1
Else
If WScript.Arguments.Named.Exists("a") Then
str = fso.GetStandardStream(StdIn, False).ReadAll
ElseIf WScript.Arguments.Named.Exists("u") Then
str = fso.GetStandardStream(StdIn, True).ReadAll
End If
End If
Else
If WScript.Arguments.Unnamed.Count = 0 Then
WScript.StdErr.WriteLine("Missing parameter String.")
WScript.Quit 1
Else
str = WScript.Arguments.Unnamed.Item(0)
End If
End If
Dim bytes, arr, i, ascch
node.dataType = "bin.base64"
If WScript.Arguments.Named.Exists("e") Then
On Error Resume Next
stream.Charset = charset
If err.number <> 0 Then
WScript.StdErr.WriteLine("Unknwon encoding specified.")
WScript.Quit 1
End If
On Error GoTo 0
stream.Type = adTypeText
stream.Open
stream.WriteText str
stream.Flush
stream.Position = 0
stream.Type = adTypeBinary
bytes = stream.Read
stream.Close
node.nodeTypedValue = bytes
str = node.text
If WScript.Arguments.Named.Exists("w") Then
ReDim arr(Len(str) - 1)
For i = 0 To Len(str) - 1
ascch = AscW(Mid(str, i + 1, 1))
If ascch = 32 Then
ascch = 12288
ElseIf ascch >= 33 And ascch <= 126 Then
ascch = ascch + 65248
End If
arr(i) = ChrW(ascch)
Next
str = Join(arr, "")
End If
WScript.StdOut.WriteLine(str)
ElseIf WScript.Arguments.Named.Exists("d") Then
If WScript.Arguments.Named.Exists("w") Then
ReDim arr(Len(str) - 1)
For i = 0 To Len(str) - 1
ascch = AscW(Mid(str, i + 1, 1))
If ascch = 12288 Then
ascch = 32
ElseIf ascch >= -255 And ascch <= -162 Then
ascch = ascch + 288
End If
arr(i) = ChrW(ascch)
Next
str = Join(arr, "")
End If
On Error Resume Next
node.text = str
If err.number <> 0 Then
WScript.StdErr.WriteLine("""" & str & """ is not valid base64 encoded string.")
WScript.Quit 1
End If
stream.Type = adTypeBinary
stream.Open
stream.Write node.nodeTypedValue
stream.Flush
stream.Position = 0
stream.Type = adTypeText
On Error Resume Next
stream.Charset = charset
If err.number <> 0 Then
WScript.StdErr.WriteLine("Unknwon encoding specified.")
WScript.Quit 1
End If
On Error GoTo 0
WScript.StdOut.WriteLine(stream.ReadText)
End If
Option Explicit
Const adTypeBinary = 1, adTypeText = 2
Dim node, adostream
Dim BinFile, HexFile
If WScript.Arguments.Named.Exists("?") Then
WScript.StdOut.Write("Convert binary file to bin.hex encoded text file" & vbNewLine & _
vbNewLine & _
"Syntax: bin2hex BinFile [HexFile]" & vbNewLine & _
vbNewLine & _
"Note:" & vbNewLine & _
"If HexFile is omitted, the default name of HexFile is ""BinFile.hex""." & vbNewLine & _
vbNewLine)
WScript.Quit
End If
If WScript.Arguments.Unnamed.Count < 1 Then
WScript.StdErr.WriteLine("Too few arguments.")
WScript.Quit 1
ElseIf WScript.Arguments.Unnamed.Count < 2 Then
BinFile = WScript.Arguments.Unnamed(0)
HexFile = BinFile & ".hex"
Else
BinFile = WScript.Arguments.Unnamed(0)
HexFile = WScript.Arguments.Unnamed(1)
End If
On Error Resume Next
Set node = CreateObject("MSXML2.DOMDocument").createElement("bin")
Set adostream = CreateObject("ADODB.Stream")
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot create COM objects.")
WScript.Quit 1
End If
node.dataType = "bin.hex"
adostream.Type = adTypeBinary
adostream.Open
adostream.LoadFromFile BinFile
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot open """ & BinFile & """.")
WScript.Quit 1
End If
node.nodeTypedValue = adostream.Read
adostream.Position = 0
adostream.SetEOS
adostream.Type = adTypeText
adostream.Charset = "ascii"
If err.number <> 0 Then
WScript.StdErr.WriteLine("Unknwon encoding specified.")
WScript.Quit 1
End If
adostream.WriteText node.text
adostream.SaveToFile HexFile
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot open """ & HexFile & """.")
WScript.Quit 1
End If
WScript.StdOut.WriteLine("""" & BinFile & """ was converted from binary to bin.hex and saved as """ & HexFile & """.")
Option Explicit
Const adSaveCreateOverWrite = 2
Const adTypeText = 2
Dim adostream
Dim CharSetIn, CharSetOut, FileIn, FileOut
If WScript.Arguments.Named.Exists("?") Then
WScript.StdOut.Write("Convert file encoded in one encoding to another" & vbNewLine & _
vbNewLine & _
"Syntax: EncodingConvert /in:CharSetIn /out:CharSetOut FileIn FileOut" & vbNewLine & _
vbNewLine & _
"See HKEY_CLASSES_ROOT\MIME\Database\Charset in registry for supported encodings" & vbNewLine)
WScript.Quit
End If
Set adostream = CreateObject("ADODB.Stream")
CharSetIn = WScript.Arguments.Named.Item("in")
CharSetOut = WScript.Arguments.Named.Item("out")
FileIn = WScript.Arguments.Unnamed.Item(0)
FileOut = WScript.Arguments.Unnamed.Item(1)
Dim BufferUnicode
adostream.Type = adTypeText
adostream.Open
adostream.Charset = CharSetIn
adostream.LoadFromFile FileIn
BufferUnicode = adostream.ReadText
adostream.Position = 0
adostream.SetEOS
adostream.Charset = CharSetOut
adostream.WriteText BufferUnicode
adostream.SaveToFile FileOut, adSaveCreateOverWrite
adostream.Close
Set adostream = Nothing
WScript.Echo("The encoding of " & FileIn & " is converted from " & CharSetIn & " to " & CharSetOut & " and saved as " & FileOut)
Option Explicit
Const adTypeBinary = 1, adTypeText = 2
Const adCRLF = -1
Const adWriteChar = 0, adWriteLine = 1
Const adSaveCreateOverWrite = 2
Dim fso, stream, node
Dim FileOriginal, FileScript
If WScript.Arguments.Named.Exists("?") Then
WScript.StdOut.Write("Generate a vbs that generates the original file" & vbNewLine & _
vbNewLine & _
"Syntax: FileGen OriginalFileName [GeneratedVBSName]" & vbNewLine & _
vbNewLine & _
"Note:" & vbNewLine & _
"If GeneratedVBSName is omitted, the default name of generated vbs file is ""OriginalFileName.vbs""." & vbNewLine)
WScript.Quit
End If
If WScript.Arguments.Unnamed.Count < 1 Then
WScript.StdErr.WriteLine("Too few arguements.")
WScript.Quit 1
ElseIf WScript.Arguments.Unnamed.Count < 2 Then
FileOriginal = WScript.Arguments.Unnamed(0)
FileScript = FileOriginal & ".vbs"
Else
FileOriginal = WScript.Arguments.Unnamed(0)
FileScript = WScript.Arguments.Unnamed(1)
End If
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set node = CreateObject("MSXML2.DOMDocument").CreateElement("binary")
Set stream = CreateObject("ADODB.Stream")
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot create COM objects.")
WScript.Quit 1
End If
On Error GoTo 0
node.dataType = "bin.hex"
stream.Type = adTypeBinary
stream.Open
On Error Resume Next
stream.LoadFromFile FileOriginal
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot open file """ & FileOriginal & """.")
WScript.Quit 1
End If
On Error GoTo 0
node.nodeTypedValue = stream.Read
stream.Position = 0
stream.SetEOS
stream.Type = adTypeText
stream.Charset = "ascii"
stream.LineSeparator = adCRLF
stream.WriteText "Dim a(" & Len(node.text) / 2 - 1 & ")", adWriteLine
Dim arr, arr2, i, str
ReDim arr(Len(node.text) / 2 - 1)
str = node.text
For i = 0 To UBound(arr)
arr(i) = "a(" & i & ")=""" & Mid(str, i * 2 + 1, 2) & """"
Next
stream.WriteText Join(arr, vbCrLf), adWriteLine
str = fso.GetFileName(FileOriginal)
stream.WriteText "Dim b(" & Len(str) - 1 & ")", adWriteLine
ReDim arr2(Len(str) - 1)
For i = 0 To Len(str) - 1
arr2(i) = "b(" & i & ")=" & AscW(Mid(str, i + 1, 1))
Next
stream.WriteText Join(arr2, vbCrLf), adWriteLine
stream.WriteText "Set n=CreateObject(""MSXML2.DOMDocument"").createElement(""e"")" & vbCrLf & _
"n.dataType=""bin.hex""" & vbCrLf & _
"n.text=Join(a,"""")" & vbCrLf & _
"Set s=CreateObject(""ADODB.Stream"")" & vbCrLf & _
"s.Type=1" & vbCrLf & _
"s.Open" & vbCrLf & _
"s.Write n.nodeTypedValue" & vbCrLf & _
"For i=0 to UBound(b)" & vbCrLf & _
"b(i)=ChrW(b(i))" & vbCrLf & _
"Next" & vbCrLf & _
"f=Join(b,"""")" & vbCrLf & _
"s.SaveToFile f,2" & vbCrLf & _
"WScript.Echo """"""""&f&"""""" was generated.""", adWriteChar
On Error Resume Next
stream.SaveToFile FileScript, adSaveCreateOverWrite
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot open file """ & FileScript & """.")
WScript.Quit 1
End If
On Error GoTo 0
WScript.StdOut.WriteLine("""" & FileScript & """ that generates """ & FileOriginal & """ was generated.")
Option Explicit
Const modeSplit = 1
Const modeCombine = 2
Const adTypeBinary = 1
Dim fso, adostreamin, adostreamout
Dim FileIn, FileOut, Mode, PartCount, PartSize
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set adostreamin = CreateObject("ADODB.Stream")
Set adostreamout = CreateObject("ADODB.Stream")
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot create COM objects.")
WScript.Quit 1
End If
On Error GoTo 0
If WScript.Arguments.Named.Exists("?") Then
WScript.StdOut.Write("Split file or combine files" & vbNewLine & _
vbNewLine & _
"Syntax: FileSplitter /s|/c [/pc:count|/ps:size] FileNameIn [FileNameOut]" & vbNewLine & _
vbNewLine & _
"/s Split file" & vbNewLine & _
"/c Combine files" & vbNewLine & _
"/pc Set the count of parts of splitted files" & vbNewLine & _
"/ps Set the size in bytes of each part of splitted file" & vbNewLine & _
vbNewLine & _
"When splitting file, the splitted files are stored in folder ""FileNameIn.Splitted""" & vbnewline & _
"(without FileNameOut) or in folder ""FileNameOut"" (with FileNameOut) and named ""FileNameIn.1""," & vbNewLine & _
"""FileNameIn.2"" ..." & vbnewline & _
"(without FileNameOut) or ""FileNameOut.1"", ""FileNameOut.2"" ... (with FileNameOut)." & vbNewLine & _
"When combining files, ""FileNameIn.1"", ""FileNameIn.2"" ... would be combined into ""FileNameIn""" & vbNewLine & _
"(without FileNameOut) or ""FileNameOut"" (with FileNameOut) and stored in the same folder as" & vbNewLine & _
"""FileNameIn.1"", ""FileNameIn.2"" ..." & vbNewLine & _
vbNewLine & _
"Note:" & vbNewLine & _
"FileNameIn can optionally include path but FileNameOut Cannot." & vbNewLine & _
"Size can be optionally suffixed with K(KB) or M(MB)." & vbNewLine & _
"/s is not compatible with /c." & vbNewLine & _
"/pc is not compatible with /ps." & vbNewLine & _
"/pc or /ps must be used with /s." & vbNewLine)
WScript.Quit 0
End If
If WScript.Arguments.Named.Exists("s") And WScript.Arguments.Named.Exists("c") Then
WScript.StdErr.WriteLine("Conflict switches /s and /c.")
WScript.Quit 1
ElseIf Not WScript.Arguments.Named.Exists("s") And Not WScript.Arguments.Named.Exists("c") Then
WScript.StdErr.WriteLine("Missing switch /s or /c.")
WScript.Quit 1
Else
If WScript.Arguments.Named.Exists("s") Then
If WScript.Arguments.Named.Exists("pc") And WScript.Arguments.Named.Exists("ps") Then
WScript.StdErr.WriteLine("Conflit switches /pc and /ps.")
WScript.Quit 1
ElseIf Not WScript.Arguments.Named.Exists("pc") And Not WScript.Arguments.Named.Exists("ps") Then
WScript.StdErr.WriteLine("Missing switches /pc or /ps.")
WScript.Quit 1
End If
Mode = modeSplit
If WScript.Arguments.Named.Exists("pc") Then
PartCount = CLng(WScript.Arguments.Named("pc"))
ElseIf WScript.Arguments.Named.Exists("ps") Then
PartSize = WScript.Arguments.Named("ps")
Select Case Mid(PartSize, Len(PartSize), 1)
Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
On Error Resume Next
PartSize = CLng(PartSize)
If err.number <> 0 Then
WScript.StdErr.Write("Invalid parameter size")
WScript.Quit 1
End If
On Error GoTo 0
If PartSize <= 0 Then
WScript.StdErr.WriteLine("Invalid parameter size.")
WScript.Quit 1
End If
Case "k", "K"
PartSize = CLng(Mid(PartSize, 1, Len(PartSize) - 1)) * 1024
Case "m", "M"
PartSize = CLng(Mid(PartSize, 1, Len(PartSize) - 1)) * 1048576
Case Else
WScript.StdErr.WriteLine("Invalid parameter size.")
WScript.Quit 1
End Select
Else
WScript.StdErr.Write("Missing switch /pc or /ps.")
WScript.Quit 1
End If
ElseIf WScript.Arguments.Named.Exists("c") Then
Mode = modeCombine
End If
End If
If WScript.Arguments.Unnamed.Count = 0 Then
WScript.StdErr.WriteLine("Missing parameter FileNameIn.")
WScript.Quit 1
ElseIf WScript.Arguments.Unnamed.Count = 1 Then
FileIn = WScript.Arguments.Unnamed(0)
Else
FileIn = WScript.Arguments.Unnamed(0)
FileOut = WScript.Arguments.Unnamed(1)
If fso.GetParentFolderName(FileOut) <> "" Then
WScript.StdErr.WriteLine("Cannot specify path to parameter FileNameOut.")
WScript.Quit 1
End If
End If
Dim PartSizePrev, FileOutDir, Count
Select Case Mode
Case modeSplit
FileOutDir = fso.GetParentFolderName(FileIn)
If IsEmpty(FileOut) Then
FileOutDir = fso.BuildPath(fso.GetParentFolderName(FileIn), fso.GetFileName(FileIn) & ".Splitted")
FileOut = fso.BuildPath(FileOutDir, FileIn & ".")
Else
FileOutDir = fso.BuildPath(fso.GetParentFolderName(FileIn), FileOut)
FileOut = fso.BuildPath(FileOutDir, FileOut & ".")
End If
On Error Resume Next
If Not fso.FolderExists(FileOutDir) Then
fso.CreateFolder(FileOutDir)
End If
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot create folder """ & FileOutDir & """.")
WScript.Quit 1
End If
adostreamin.Open
adostreamin.Type = adTypeBinary
adostreamout.Open
adostreamout.Type = adTypeBinary
adostreamin.LoadFromFile FileIn
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot open file """ & FileIn & """.")
WScript.Quit 1
End If
Count = 1
PartSizePrev = 0
If IsEmpty(PartSize) Then
Do Until adostreamin.EOS
PartSize = CLng(adostreamin.Size / PartCount * Count)
adostreamin.CopyTo adostreamout, PartSize - PartSizePrev
adostreamout.SaveToFile FileOut & Count
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot open file """ & FileOut & Count & """.")
WScript.Quit 1
End If
adostreamout.Position = 0
adostreamout.SetEOS
PartSizePrev = PartSize
Count = Count + 1
Loop
ElseIf IsEmpty(PartCount) Then
Do Until adostreamin.EOS
adostreamin.CopyTo adostreamout, PartSize
adostreamout.SaveToFile FileOut & Count
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot open file """ & FileOut & Count & """.")
WScript.Quit 1
End If
adostreamout.Position = 0
adostreamout.SetEOS
Count = Count + 1
Loop
End If
On Error GoTo 0
adostreamin.Close
adostreamout.Close
WScript.StdOut.WriteLine("""" & FileIn & """ was splitted into """ & FileOut & "Xs"".")
Case modeCombine
adostreamin.Open
adostreamin.Type = adTypeBinary
adostreamout.Open
adostreamout.Type = adTypeBinary
Count = 1
On Error Resume Next
Do
If fso.FileExists(FileIn & "." & Count) Then
adostreamin.LoadFromFile FileIn & "." & Count
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot open file """ & FileIn & "." & Count & """.")
WScript.Quit 1
End If
adostreamin.CopyTo adostreamout
Else
Exit Do
End If
Count = Count + 1
Loop While True
If IsEmpty(FileOut) Then
FileOut = FileIn
Else
FileOut = fso.BuildPath(fso.GetParentFolderName(FileIn), FileOut)
End If
adostreamout.SaveToFile FileOut
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot open file """ & FileOut & """.")
WScript.Quit 1
End If
On Error GoTo 0
WScript.StdOut.WriteLine("""" & FileIn & ".Xs"" were combined into """ & FileOut & """.")
End Select
Option Explicit
Const adTypeBinary = 1
Dim node, stream, omd5, osha1, osha256
Dim FileIn, buffer, md5, sha1, sha256
If WScript.Arguments.Named.Exists("?") Then
WScript.StdOut.Write("Calculate the hash of file" & vbNewLine & _
vbNewLine & _
"Syntax: Hash [/md5|/sha1|/sha256] File" & vbNewLine & _
vbNewLine & _
"/md5 Calculate the MD5 of ""File""" & vbNewLine & _
"/sha1 Calculate the SHA1 of ""File""" & vbNewLine & _
"/sha256 Calculate the SHA256 of ""File""" & vbNewLine & _
vbNewLine & _
"If no switch is provided, all three hashes are calculated." & vbNewLine)
WScript.Quit
End If
If WScript.Arguments.Named.Count = 0 Then
md5 = True
sha1 = True
sha256 = True
Else
If WScript.Arguments.Unnamed.Count < 1 Then
WScript.StdErr.WriteLine("Too few arguments.")
WScript.Quit 1
ElseIf WScript.Arguments.Named.Exists("md5") Then
md5 = True
ElseIf WScript.Arguments.Named.Exists("sha1") Then
sha1 = True
ElseIf WScript.Arguments.Named.Exists("sha256") Then
sha256 = True
Else
WScript.StdErr.WriteLine("Unknown hash type.")
WScript.Quit 1
End If
End If
FileIn = WScript.Arguments.Unnamed(0)
Set node = CreateObject("MSXML2.DOMDocument").createElement("bin")
node.dataType = "bin.hex"
Set stream = CreateObject("ADODB.Stream")
stream.Type = adTypeBinary
stream.Open
On Error Resume Next
stream.LoadFromFile FileIn
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot open file """ & FileIn & """.")
WScript.Quit 1
End If
On Error GoTo 0
buffer = stream.Read
If md5 Then
Set omd5 = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")
node.nodeTypedValue = omd5.ComputeHash_2((buffer))
WScript.StdOut.Write("md5: " & node.text & vbNewLine)
End If
If sha1 Then
Set osha1 = CreateObject("System.Security.Cryptography.SHA1CryptoServiceProvider")
node.nodeTypedValue = osha1.ComputeHash_2((buffer))
WScript.StdOut.Write("sha1: " & node.text & vbNewLine)
End If
If sha256 Then
Set osha256 = CreateObject("System.Security.Cryptography.SHA256Managed")
node.nodeTypedValue = osha256.ComputeHash_2((buffer))
WScript.StdOut.Write("sha256: " & node.text & vbNewLine)
End If
Option Explicit
Const adTypeBinary = 1, adTypeText = 2
Dim node, adostream
Dim HexFile, BinFile
If WScript.Arguments.Named.Exists("?") Then
WScript.StdOut.Write("Convert bin.hex encoded text file to binary file" & vbNewLine & _
vbNewLine & _
"Syntax: hex2bin HexFile [BinFile]" & vbNewLine & _
vbNewLine & _
"Note:" & vbNewLine & _
"If BinFile is omitted, the default name of BinFile is ""HexFile.bin""." & vbNewLine)
WScript.Quit
End If
If WScript.Arguments.Unnamed.Count < 1 Then
WScript.StdErr.WriteLine("Too few arguments.")
WScript.Quit 1
ElseIf WScript.Arguments.Unnamed.Count < 2 Then
HexFile = WScript.Arguments.Unnamed(0)
BinFile = HexFile & ".bin"
Else
HexFile = WScript.Arguments.Unnamed(0)
BinFile = WScript.Arguments.Unnamed(1)
End If
On Error Resume Next
Set node = CreateObject("MSXML2.DOMDocument").createElement("bin")
Set adostream = CreateObject("ADODB.Stream")
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot create COM objects.")
WScript.Quit 1
End If
node.dataType = "bin.hex"
adostream.Type = adTypeText
adostream.Charset = "ascii"
If err.number <> 0 Then
WScript.StdErr.WriteLine("Unknwon encoding specified.")
WScript.Quit 1
End If
adostream.Open
adostream.LoadFromFile HexFile
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot open """ & HexFile & """.")
WScript.Quit 1
End If
node.text = adostream.ReadText
adostream.Position = 0
adostream.SetEOS
adostream.Type = adTypeBinary
adostream.Write node.nodeTypedValue
adostream.SaveToFile BinFile
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot open """ & BinFile & """.")
WScript.Quit 1
End If
WScript.StdOut.WriteLine("""" & HexFile & """ was converted from bin.hex to binary and saved as """ & BinFile & """.")
Option Explicit
Const StdIn = 0
Const adTypeText = 2
Dim stream, fso
Dim charsetin, charsetout, str
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set stream = CreateObject("ADODB.Stream")
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot create COM objects.")
WScript.Quit 1
End If
On Error GoTo 0
If WScript.Arguments.Count = 0 Then
WScript.StdErr.WriteLine("Too few arguments.")
WScript.Quit 1
End If
If WScript.Arguments.Named.Exists("?") Then
WScript.StdOut.Write("Convert input to hz-gb-2312 or vice versa" & vbNewLine & _
vbNewLine & _
"Syntax: HZConvert /e|/d [/w] [[/f] /a|/u] [/s] String" & vbNewLine & _
vbNewLine & _
"/e Encode string as hz-gb-2312" & vbNewLine & _
"/d Decode string from hz-gb-2312" & vbNewLine & _
"/w Convert half-width characters to full-width after encoding or convert full-width" & vbNewLine & _
" characters to half-width before decoding" & vbNewLine & _
"/f Read input from stdin instead of from command line" & vbNewLine & _
"/a Read stdin as ANSI" & vbNewLine & _
"/u Read stdin as Unicode" & vbNewLine & _
"/s Escape input with ~{ at the beginning and ~} at the end" & vbNewLine & _
"String String to be converted" & vbNewLine & _
vbNewLine & _
"Note:" & vbNewLine & _
"/s is not compatible with /e" & vbNewLine & _
vbNewLine & _
"Examples:" & vbNewLine & _
"cscript HZConvert.vbs /e /c 母の母はハハハと笑う" & vbNewLine & _
"cscript HZConvert.vbs /d /f /a < hz.txt" & vbNewLine)
WScript.Quit
End If
If WScript.Arguments.Named.Exists("e") And WScript.Arguments.Named.Exists("d") Then
WScript.StdErr.WriteLine("Conflict switches /e and /d.")
WScript.Quit 1
ElseIf Not WScript.Arguments.Named.Exists("e") And Not WScript.Arguments.Named.Exists("d") Then
WScript.StdErr.WriteLine("Missing switches /e or /d.")
WScript.Quit 1
Else
If WScript.Arguments.Named.Exists("e") Then
charsetin = "hz-gb-2312"
charsetout = "ascii"
ElseIf WScript.Arguments.Named.Exists("d") Then
charsetin = "ascii"
charsetout = "hz-gb-2312"
End If
End If
If WScript.Arguments.Named.Exists("f") Then
If WScript.Arguments.Named.Exists("a") And WScript.Arguments.Named.Exists("u") Then
WScript.StdErr.WriteLine("Conflict switches /a and /u.")
WScript.Quit 1
ElseIf Not WScript.Arguments.Named.Exists("a") And Not WScript.Arguments.Named.Exists("u") Then
WScript.StdErr.WriteLine("Missing switch /a or /u.")
WScript.Quit 1
Else
If WScript.Arguments.Named.Exists("a") Then
str = fso.GetStandardStream(StdIn, False).ReadAll
ElseIf WScript.Arguments.Named.Exists("u") Then
str = fso.GetStandardStream(StdIn, True).ReadAll
End If
End If
Else
If WScript.Arguments.Unnamed.Count = 0 Then
WScript.StdErr.WriteLine("Missing parameter String.")
WScript.Quit 1
Else
str = WScript.Arguments.Unnamed.Item(0)
End If
End If
If WScript.Arguments.Named.Exists("s") Then
If Not WScript.Arguments.Named.Exists("e") Then
str = "~{" & str & "~}"
Else
WScript.StdErr.WriteLine("Conflict switches /s and /e.")
WScript.Quit 1
End If
End If
Dim arr, i, ascch
If WScript.Arguments.Named.Exists("d") And WScript.Arguments.Named.Exists("w") Then
ReDim arr(Len(str) - 1)
For i = 0 To Len(str) - 1
ascch = AscW(Mid(str, i + 1, 1))
If ascch = 12288 Then
ascch = 32
ElseIf ascch >= -255 And ascch <= -162 Then
ascch = ascch + 288
End If
arr(i) = ChrW(ascch)
Next
str = Join(arr, "")
End If
On Error Resume Next
stream.Charset = charsetin
stream.Type = adTypeText
stream.Open
stream.WriteText str
stream.Flush
stream.Position = 0
stream.Charset = charsetout
If err.number <> 0 Then
WScript.StdErr.WriteLine("Unknwon encoding specified.")
WScript.Quit 1
End If
str = stream.ReadText
On Error GoTo 0
If WScript.Arguments.Named.Exists("e") And WScript.Arguments.Named.Exists("w") Then
ReDim arr(Len(str) - 1)
For i = 0 To Len(str) - 1
ascch = AscW(Mid(str, i + 1, 1))
If ascch = 32 Then
ascch = 12288
ElseIf ascch >= 33 And ascch <= 126 Then
ascch = ascch + 65248
End If
arr(i) = ChrW(ascch)
Next
str = Join(arr, "")
End If
WScript.Echo str
Option Explicit
Const adTypeText = 2
Dim adostream, node
Dim Length, FileOut
On Error Resume Next
Set adostream = CreateObject("ADODB.Stream")
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot create COM object.")
WScript.Quit 1
End If
On Error GoTo 0
If WScript.Arguments.Named.Exists("?") Then
WScript.StdOut.Write("Generate fixed length key in bin.hex format." & vbNewLine & _
vbNewLine & _
"Syntax: KeyGen Length FileOut" & vbNewLine)
WScript.Quit 0
End If
If WScript.Arguments.Unnamed.Count < 2 Then
WScript.StdErr.WriteLine("Too few arguments.")
WScript.Quit 1
Else
On Error Resume Next
Length = CLng(WScript.Arguments.Unnamed(0))
If err.number <> 0 Then
WScript.StdErr.WriteLine("Invalid Length.")
WScript.Quit 1
End If
On Error GoTo 0
FileOut = WScript.Arguments.Unnamed(1)
End If
On Error Resume Next
adostream.Open
adostream.Type = adTypeText
adostream.Charset = "ascii"
If err.number <> 0 Then
WScript.StdErr.WriteLine("Unknwon encoding specified.")
WScript.Quit 1
End If
Dim arrKey(), i
ReDim arrKey(Length - 1)
Randomize
For i = 0 To UBound(arrKey)
arrKey(i) = Hex(Int(256 * Rnd))
If Len(arrKey(i)) = 1 Then arrKey(i) = "0" & arrKey(i)
Next
adostream.WriteText LCase(Join(arrKey, ""))
adostream.SaveToFile FileOut
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot open """ & FileOut & """.")
WScript.Quit 1
End If
WScript.StdOut.WriteLine("Key of " & Length & " bytes was generated and saved as """ & FileOut & """.")
Option Explicit
Const TristateTrue = -1, TristateFalse = 0
Const ForReading = 1, ForWriting = 2
Dim fso, fsIn, fsOut, Encoder
Dim IsUnicode, FileIn, FileOut
If WScript.Arguments.Named.Exists("?") Then
WScript.StdOut.Write("Encode vbs into vbe" & vbNewLine & _
vbNewLine & _
"Syntax: ScriptEncoder [/u] VBSFile" & vbNewLine)
WScript.Quit
End If
If WScript.Arguments.Named.Exists("u") Then
IsUnicode = True
Else
IsUnicode = False
End If
If WScript.Arguments.Unnamed.Count < 1 Then
WScript.StdErr.WriteLine("Too few arguments.")
WScript.Quit 1
End If
On Error Resume Next
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
Set Encoder = WScript.CreateObject("Scripting.Encoder")
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot create COM objects.")
WScript.Quit 1
End If
On Error GoTo 0
FileIn = WScript.Arguments.Unnamed(0)
If fso.GetExtensionName(FileIn) <> "vbs" Then
WScript.StdErr.WriteLine("Unsupported script type.")
WScript.Quit 1
End If
FileOut = fso.BuildPath(fso.GetParentFolderName(FileIn), fso.GetBaseName(FileIn) & ".vbe")
On Error Resume Next
If IsUnicode Then
Set fsIn = fso.OpenTextFile(FileIn, ForReading, False, TristateTrue)
Else
Set fsIn = fso.OpenTextFile(FileIn, ForReading, False, TristateFalse)
End If
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot open file """ & FileIn & """.")
WScript.Quit 1
End If
Set fsOut = fso.OpenTextFile(FileOut, ForWriting, True)
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot open file """ & FileOut & """.")
WScript.Quit 1
End If
fsOut.Write(Encoder.EncodeScriptFile(".vbs", fsIn.ReadAll, 0, "VBScript"))
If err.number <> 0 Then
WScript.StdErr.WriteLine("Failed to encode """ & FileIn & """.")
WScript.Quit 1
End If
On Error GoTo 0
fsIn.Close
fsOut.Close
WScript.StdOut.WriteLine("""" & FileIn & """ was encoded and saved as """ & FileOut & """.")
Option Explicit
Const StdIn = 0
Const adTypeText = 2
Dim stream, fso
Dim charsetin, charsetout, str
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set stream = CreateObject("ADODB.Stream")
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot create COM objects.")
WScript.Quit 1
End If
On Error GoTo 0
If WScript.Arguments.Count = 0 Then
WScript.StdErr.WriteLine("Too few arguments.")
WScript.Quit 1
End If
If WScript.Arguments.Named.Exists("?") Then
WScript.StdOut.Write("Convert input to UTF-7 or vice versa" & vbNewLine & _
vbNewLine & _
"Syntax: UTF7Convert /e|/d [/w] [[/f] /a|/u] [/s] String" & vbNewLine & _
vbNewLine & _
"/e Encode string as UTF-7" & vbNewLine & _
"/d Decode string from UTF-7" & vbNewLine & _
"/w Convert half-width characters to full-width after encoding or convert full-width" & vbNewLine & _
" characters to half-width before decoding" & vbNewLine & _
"/f Read input from stdin instead of from command line" & vbNewLine & _
"/a Read stdin as ANSI" & vbNewLine & _
"/u Read stdin as Unicode" & vbNewLine & _
"/s Escape input with + at the beginning and - at the end" & vbNewLine & _
"String String to be converted" & vbNewLine & _
vbNewLine & _
"Note:" & vbNewLine & _
"/s is not compatible with /e" & vbNewLine & _
vbNewLine & _
"Examples:" & vbNewLine & _
"cscript UTF7Convert.vbs /e /c 母の母はハハハと笑う" & vbNewLine & _
"cscript UTF7Convert.vbs /d /f /a < utf7.txt" & vbNewLine)
WScript.Quit
End If
If WScript.Arguments.Named.Exists("e") And WScript.Arguments.Named.Exists("d") Then
WScript.StdErr.WriteLine("Conflict switches /e and /d.")
WScript.Quit 1
ElseIf Not WScript.Arguments.Named.Exists("e") And Not WScript.Arguments.Named.Exists("d") Then
WScript.StdErr.WriteLine("Missing switches /e or /d.")
WScript.Quit 1
Else
If WScript.Arguments.Named.Exists("e") Then
charsetin = "utf-7"
charsetout = "ascii"
ElseIf WScript.Arguments.Named.Exists("d") Then
charsetin = "ascii"
charsetout = "utf-7"
End If
End If
If WScript.Arguments.Named.Exists("f") Then
If WScript.Arguments.Named.Exists("a") And WScript.Arguments.Named.Exists("u") Then
WScript.StdErr.WriteLine("Conflict switches /a and /u.")
WScript.Quit 1
ElseIf Not WScript.Arguments.Named.Exists("a") And Not WScript.Arguments.Named.Exists("u") Then
WScript.StdErr.WriteLine("Missing switch /a or /u.")
WScript.Quit 1
Else
If WScript.Arguments.Named.Exists("a") Then
str = fso.GetStandardStream(StdIn, False).ReadAll
ElseIf WScript.Arguments.Named.Exists("u") Then
str = fso.GetStandardStream(StdIn, True).ReadAll
End If
End If
Else
If WScript.Arguments.Unnamed.Count = 0 Then
WScript.StdErr.WriteLine("Missing parameter String.")
WScript.Quit 1
Else
str = WScript.Arguments.Unnamed.Item(0)
End If
End If
If WScript.Arguments.Named.Exists("s") Then
If Not WScript.Arguments.Named.Exists("e") Then
str = "+" & str & "-"
Else
WScript.StdErr.WriteLine("Conflict switches /s and /e.")
WScript.Quit 1
End If
End If
Dim arr, i, ascch
If WScript.Arguments.Named.Exists("d") And WScript.Arguments.Named.Exists("w") Then
ReDim arr(Len(str) - 1)
For i = 0 To Len(str) - 1
ascch = AscW(Mid(str, i + 1, 1))
If ascch = 12288 Then
ascch = 32
ElseIf ascch >= -255 And ascch <= -162 Then
ascch = ascch + 288
End If
arr(i) = ChrW(ascch)
Next
str = Join(arr, "")
End If
On Error Resume Next
stream.Charset = charsetin
stream.Type = adTypeText
stream.Open
stream.WriteText str
stream.Flush
stream.Position = 0
stream.Charset = charsetout
If err.number <> 0 Then
WScript.StdErr.WriteLine("Unknwon encoding specified.")
WScript.Quit 1
End If
str = stream.ReadText
On Error GoTo 0
If WScript.Arguments.Named.Exists("e") And WScript.Arguments.Named.Exists("w") Then
ReDim arr(Len(str) - 1)
For i = 0 To Len(str) - 1
ascch = AscW(Mid(str, i + 1, 1))
If ascch = 32 Then
ascch = 12288
ElseIf ascch >= 33 And ascch <= 126 Then
ascch = ascch + 65248
End If
arr(i) = ChrW(ascch)
Next
str = Join(arr, "")
End If
WScript.StdOut.WriteLine(str)
Option Explicit
Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2
Dim node, stream
Dim Key, FileIn, FileOut, BufferIn, BufferOut
If WScript.Arguments.Named.Exists("?") Then
WScript.StdOut.Write("Xor file with key" & vbNewLine & _
vbNewLine & _
"Syntax: XorCrypt Key FileIn FileOut" & _
vbNewLine & _
"Note: Key must be a Byte" & vbNewLine)
WScript.Quit
End If
If WScript.Arguments.Unnamed.Count < 3 Then
WScript.StdErr.WriteLine("Too few arguments.")
WScript.Quit 1
End If
On Error Resume Next
Key = CByte(WScript.Arguments.Unnamed(0))
If err.number <> 0 Then
WScript.StdErr.WriteLine("Invalid parameter Key.")
WScript.Quit 1
End If
On Error GoTo 0
FileIn = WScript.Arguments.Unnamed(1)
FileOut = WScript.Arguments.Unnamed(2)
On Error Resume Next
Set node = CreateObject("MSXML2.DOMDocument").createElement("binary")
Set stream = CreateObject("ADODB.Stream")
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot create COM objects.")
WScript.Quit 1
End If
On Error GoTo 0
node.DataType = "bin.hex"
stream.Type = adTypeBinary
stream.Open
On Error Resume Next
stream.LoadFromFile FileIn
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot open """ & FileIn & """.")
WScript.Quit 1
End If
On Error GoTo 0
node.nodeTypedValue = stream.Read
BufferIn = node.text
stream.Position = 0
stream.SetEOS
Dim i, hexbyteFileIn, hexbyteFileOut
Dim arr()
ReDim arr(Len(BufferIn) / 2 - 1)
For i = 0 To UBound(arr)
hexbyteFileIn = Mid(BufferIn, i * 2 + 1, 2)
hexbyteFileOut = LCase(Hex((CByte("&h" & hexbyteFileIn)) Xor Key))
If Len(hexbyteFileOut) = 1 Then hexbyteFileOut = "0" & hexbyteFileOut
arr(i) = hexbyteFileOut
Next
BufferOut = Join(arr, "")
node.text = BufferOut
stream.Write node.nodeTypedValue
On Error Resume Next
stream.SaveToFile FileOut, adSaveCreateOverWrite
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot open """ & FileOut & """.")
WScript.Quit 1
End If
On Error GoTo 0
WScript.StdOut.WriteLine("""" & FileIn & """ was xor-ed with key " & Key & " and saved as """ & FileOut & """.")
Option Explicit
Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2
Dim node, stream
Dim FileKey, FileIn, FileOut, BufferKey, BufferIn, BufferOut
If WScript.Arguments.Named.Exists("?") Then
WScript.StdOut.Write("Xor file with key file" & vbNewLine & _
vbNewLine & _
"Syntax: XorKeyCrypt KeyFile FileIn FileOut" & vbNewLine)
WScript.Quit
End If
If WScript.Arguments.Unnamed.Count < 3 Then
WScript.StdErr.WriteLine("Too few arguments.")
WScript.Quit 1
End If
FileKey = WScript.Arguments.Unnamed(0)
FileIn = WScript.Arguments.Unnamed(1)
FileOut = WScript.Arguments.Unnamed(2)
On Error Resume Next
Set node = CreateObject("MSXML2.DOMDocument").createElement("Binary")
Set stream = CreateObject("ADODB.Stream")
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot create COM objects.")
WScript.Quit 1
End If
On Error GoTo 0
node.DataType = "bin.hex"
stream.Type = adTypeBinary
stream.Open
On Error Resume Next
stream.LoadFromFile FileKey
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot open """ & FileKey & """.")
WScript.Quit 1
End If
On Error GoTo 0
node.nodeTypedValue = stream.Read
BufferKey = node.text
stream.Position = 0
stream.SetEOS
On Error Resume Next
stream.LoadFromFile FileIn
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot open """ & FileIn & """.")
WScript.Quit 1
End If
On Error GoTo 0
node.nodeTypedValue = stream.Read
BufferIn = node.text
stream.Position = 0
stream.SetEOS
Dim i, hexbyteFileIn, hexbyteFileOut
Dim arrKey(), arrFile()
ReDim arrKey(Len(BufferKey) / 2 - 1)
For i = 0 To UBound(arrKey)
arrKey(i) = CByte("&H" & Mid(BufferKey, i * 2 + 1, 2))
Next
ReDim arrFile(Len(BufferIn) / 2 - 1)
For i = 0 To UBound(arrFile)
hexbyteFileIn = Mid(BufferIn, i * 2 + 1, 2)
hexbyteFileOut = LCase(Hex(CByte("&H" & Mid(BufferIn, i * 2 + 1, 2)) Xor arrKey(i Mod (UBound(arrKey) + 1))))
If Len(hexbyteFileOut) = 1 Then hexbyteFileOut = "0" & hexbyteFileOut
arrFile(i) = hexbyteFileOut
Next
BufferOut = Join(arrFile, "")
node.text = BufferOut
stream.Write node.nodeTypedValue
stream.SaveToFile FileOut, adSaveCreateOverWrite
If err.number <> 0 Then
WScript.StdErr.WriteLine("Cannot open """ & FileOut & """.")
WScript.Quit 1
End If
On Error GoTo 0
WScript.Echo("""" & FileIn & """ was xor-ed with key file """ & FileKey & """ and saved as """ & FileOut & """.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment