解码vbscript时出现奇怪的字符

时间:2014-03-27 00:15:30

标签: vbscript decode encode

我正在使用此代码对我的vbscript进行编码

'encrypt.vbs 
Set WshShell = CreateObject("WScript.Shell")
strCurDir    = WshShell.CurrentDirectory
 set x = WScript.CreateObject("WScript.Shell")
 Set objFSO = CreateObject("Scripting.FileSystemObject")
 txt = "set root = getobject(" & """winmgmts:{impersonationlevel=impersonate}!\\.\root\cimv2""" & ")"                    '  set root = getobject("winmgmts:{impersonationlevel=impersonate}!\\.\root\cimv2")
 a = encode(txt) 
 strFiley = strCurDir & "\enc4.txt"
 Set objFile = objFSO.CreateTextFile(strFiley,2,True)
objFile.Write(a)

 function encode(s) 
    For i = 1 To Len(s) 
       newtxt = Mid(s, i, 1) 
       newtxt = Chr(Asc(newtxt)+3) 
       coded = coded & newtxt 
    Next 
    encode = coded 
 End Function 

和解密的代码

     'decrypt.vbs 
 set x = WScript.createobject("wscript.shell") 
txt = "vhw#urrw#@#jhwremhfw+%zlqpjpwv=~lpshuvrqdwlrqohyho@lpshuvrqdwh€$__1_urrw_flpy5%," ' Encrypted string
 msgbox encode(txt)   
 function encode(s) 
    For i = 1 To Len(s) 
       newtxt = Mid(s, i, 1) 
       newtxt = Chr(Asc(newtxt)-3) 
       coded = coded & newtxt 
    Next 
    encode = coded 
 End Function 

实际上我不只是编写完整的脚本本节,而是出现问题“}!\\.\

解释我的问题可能更好解释图像:

http://www.x88x.com/lives/13958790471.jpg

那么如何纠正错误的字符?

1 个答案:

答案 0 :(得分:1)

Ansi (单字节)字符与 Unicode (双字节)字符混合在一起存在根本性差异。 您操作 Ansi 字符的二进制值并将结果写入 Unicode 文件。然后,读取此 Unicode 文件,即可获得 Unicode 字符。 但是,"翻译"没有内在的功能。 Ansi和Unicode之间的字符(反之)在VBScript中......

如果有必要,而不是" Scripting.FileSystemObject"对象,使用" ADODB.Stream"写入和读取二进制数据的对象(此外, AutomatedChaos 是正确的 - 避免这种情况......)

下一个脚本显示所有差异和"相似之处" Ansi与Unicode:

Option Explicit
'On Error Resume Next
On Error GoTo 0

Dim strResult: strResult = Wscript.ScriptName & vbNewLine
Dim ii, jj, strAux
Const booHexOut = True

strResult = strResult & vbTab & "Ansi # Unicode" & vbNewLine
jj = 0
For ii = 0 To 255
  strAux = Chr( ii)
  If AscB( Midb( strAux, 1, 1)) = ii And AscB( Midb( strAux, 2, 1)) = 0 Then
  Else 
    If booHexOut Then
      strResult = strResult & strAux & vbTab & CStr( ii) & vbTab & Hex2( ii) _
        & " # " & Hex2( AscB( Midb( strAux, 1, 1))) _
        & ", " & Hex2( AscB( Midb( strAux, 2, 1))) & vbTab 
    Else
      strResult = strResult & strAux & vbTab & CStr( ii) _
        & " # " & CStr( AscB( Midb( strAux, 1, 1))) _
        & ", " & CStr( AscB( Midb( strAux, 2, 1))) & vbTab
    End If 
    jj = jj + 1
    If jj Mod 3 = 0 Then strResult = strResult & vbNewLine 
  End If
Next
strResult = strResult & vbNewLine

strResult = strResult & vbNewLine & vbTab & "Ansi = Unicode" & vbNewLine
jj = 0
For ii = 128 To 255
  strAux = Chr( ii)
  If AscB( Midb( strAux, 1, 1)) = ii And AscB( Midb( strAux, 2, 1)) = 0 Then
    If booHexOut Then
      strResult = strResult & strAux & vbTab & CStr( ii) & vbTab & Hex2( ii) _
        & " = " & Hex2( AscB( Midb( strAux, 1, 1))) _
        & ", " & Hex2( AscB( Midb( strAux, 2, 1))) & vbTab 
    Else
      strResult = strResult & strAux & vbTab & CStr( ii) _
        & " = " & CStr( AscB( Midb( strAux, 1, 1))) _
        & ", " & CStr( AscB( Midb( strAux, 2, 1))) & vbTab 
    End If 
    jj = jj + 1
    If jj Mod 3 = 0 Then strResult = strResult & vbNewLine 
  Else 
  End If
Next
strResult = strResult & vbNewLine

Wscript.Echo strResult
Wscript.Quit

Function Hex2( byVal nmbr)
  Hex2 = Right( "00" & Hex( nmbr), 2)
End Function