我想将以二进制存储的密码转换为普通的ASCII格式,以便我可以读取它。我需要一个VBscript,脚本也应该返回这个去密码
例如:加密二进制密码:00110001 00110010 00110011 00110100
去密码原始密码:1234
我试过这个
'Binary contains the binary password
dim S
For I = 1 To LenB(Binary)
S = S & Chr(AscB(MidB(Binary, I, 1)))
Next
MSGBOX S
但输出是
0
如何实现这一目标。请帮忙!!
答案 0 :(得分:4)
如果要处理字节数组,则必须先知道字符编码,然后才能将其转换为字符串。没有这些知识,字节将被转换为错误的字符。
ADODB.Stream
object可以处理字节数组。这是一个功能:
Const adTypeBinary = 1
Const adTypeText = 2
Const adModeReadWrite = 3
Function BytesToString(bytes, charset)
With CreateObject("ADODB.Stream")
.Mode = adModeReadWrite
.Type = adTypeBinary
.Open
.Write bytes
.Position = 0
.Type = adTypeText
.Charset = charset
BytesToString = .ReadText
End With
End Function
以下是如何使用它:
MsgBox BytesToString(binary, "Windows-1252")
为了完整起见,这是相反的操作:
Function StringToBytes(str, charset)
With CreateObject("ADODB.Stream")
.Mode = adModeReadWrite
.Type = adTypeText
.Charset = charset
.Open
.WriteText str
.Position = 0
.Type = adTypeBinary
StringToBytes = .Read
End With
End Function
由于您的输入似乎是字符串,如"00110001 00110010 00110011 00110100"
,因此这是一个将其转换为字节数组的函数,然后您可以使用上面显示的BytesToString()
:
Function BinaryStringToBytes(binaryStr)
Dim b, n, i, l
l = GetLocale
SetLocale 1031
With CreateObject("ADODB.Stream")
.Mode = adModeReadWrite
.Charset = "Windows-1252"
.Type = adTypeText
.Open
For Each b In Split(binaryStr, " ")
If Len(b) <> 8 Or Replace(Replace(b, "0", ""), "1", "") <> "" Then
' invalid procedure call or argument
Err.Raise 5, "BinaryStringToBytes", _
"Only stings of 8-blocks of 0s and 1s, " & _
"separated by a single space are accepted."
End If
n = 0
For i = 0 To 7
n = n + Mid(b, 8 - i, 1) * 2^i
Next
.WriteText Chr(n)
Next
.Position = 0
.Type = adTypeBinary
BinaryStringToBytes = .Read
End With
SetLocale l
End Function
用法
Dim input, output
input = "00110001 00110010 00110011 00110100"
output = BytesToString(BinaryStringToBytes(input), "Windows-1252")
MsgBox output ' -> "1234"
更重要的是,它可以正确处理多字节编码:
input = "00110001 00110010 00110011 00110100 11000011 10100100"
output = BytesToString(BinaryStringToBytes(input), "UTF-8")
MsgBox output ' -> "1234ä"
答案 1 :(得分:2)
试试这段代码;)
代码:
function BinaryToString(bin)
dim next_char
dim result
dim i
dim ascii
For i = 1 To Len(bin) + 18 Step 8
next_char = Mid(bin, i, 8)
ascii = BinaryToLong(next_char)
result = result & Chr(ascii)
Next
BinaryToString=result
end function
Function BinaryToLong(binary_value)
Dim hex_result
Dim nibble_num
Dim nibble_value
Dim factor
Dim bit
binary_value = UCase(Trim(binary_value))
If Left(binary_value, 2) = "&B" Then
binary_value = Mid(binary_value, 3)
End If
binary_value = Replace(binary_value, " ", "")
binary_value = Right(String(32, "0") & binary_value, 32)
For nibble_num = 7 To 0 Step -1
factor = 1
nibble_value = 0
For bit = 3 To 0 Step -1
If Mid(binary_value,1 + nibble_num * 4 + bit, 1) = "1" Then
nibble_value = nibble_value + factor
End If
factor = factor * 2
Next 'bit
hex_result = Hex(nibble_value) & hex_result
Next 'nibble_num
BinaryToLong = CLng("&H" & hex_result)
End Function
用法:
response.Write(BinaryToString("00110001001100100011001100110100"))
不要忘记起飞&#34; &#34;二进制字符串中的空格
答案 2 :(得分:0)
如果我是对的,你所追求的只是将二进制数转换为十进制数(例如0100 - > 4)?
dim binary, n, s
binary= "00110001"
For s = 1 To Len(binary)
n = n + (Mid(binary, Len(binary) - s + 1, 1) * (2 ^ (s - 1)))
Next 's
WScript.Echo binary & " = " & n
输出
00110001 = 49
答案 3 :(得分:-3)
有很多方法。
如果它是二进制寄存器值,那么来自帮助(你确实读过它,没有你)
RegRead方法返回以下五种类型的值。
Type Description In the Form of
REG_SZ
A string
A string
REG_DWORD
A number
An integer
REG_BINARY
A binary value
A VBArray of integers
REG_EXPAND_SZ
An expandable string (e.g., "%windir%\\calc.exe")
A string
REG_MULTI_SZ
An array of strings
A VBArray of strings
如果是一个字符串,则在空格上分割(给出一个字符串数组)。最低有效位是2 ^ 0,2 ^ 1,...,2 ^ 7。
<小时/> EDIT
存储密码的正常方法,不是唯一的方法,就是将其转储到注册表中。
读它会给你一个数组,而不是一个标量变量。所以...
第二种方法处理将其存储在文件中的情况。